Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MEND
electrical
Commits
cba89982
Commit
cba89982
authored
Jan 26, 2021
by
sonaraju
Browse files
Add haptic code file
parent
efc96575
Changes
1
Hide whitespace changes
Inline
Side-by-side
haptic.ino
0 → 100644
View file @
cba89982
#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
drv
.
begin
();
// I2C trigger by sending 'go' command
drv
.
setMode
(
DRV2605_MODE_INTTRIG
);
// default, internal trigger when sending GO command
drv
.
selectLibrary
(
1
);
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
);
delayMicroseconds
(
2
);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite
(
trigPin
,
HIGH
);
delayMicroseconds
(
10
);
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
);
Serial
.
println
(
distance
);
delay
(
distance
*
15
);
digitalWrite
(
ledPin
,
LOW
);
drv
.
go
();
delay
(
distance
*
15
);
}
else
{
digitalWrite
(
ledPin
,
LOW
);
// turn the LED off by making the voltage LOW
delay
(
300
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment