Commit cba89982 authored by sonaraju's avatar sonaraju
Browse files

Add haptic code file

parent efc96575
#include <Adafruit_DRV2605.h>
Adafruit_DRV2605 drv;
// defines pins numbers
//A4 SCA
//A5 SLC
const int trigPin = 5;
const int echoPin = 4;
const int ledPin = 3;
unsigned long time_now = 0;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
// I2C trigger by sending 'go' command
drv.setMode(DRV2605_MODE_INTTRIG); // default, internal trigger when sending GO command
drv.setWaveform(1, 1); // strong click 100%, see datasheet part 11.2
drv.setWaveform(2, 0); // end of waveforms
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration*0.034/2;
if(distance < 50) {
digitalWrite(ledPin, HIGH);
delay(distance * 15);
digitalWrite(ledPin, LOW);
delay(distance * 15);
else {
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment