-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHCSR04 Range Testing
More file actions
60 lines (51 loc) · 1.29 KB
/
HCSR04 Range Testing
File metadata and controls
60 lines (51 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
int cm = 0;
int leftLow = 50;
int leftHigh = 70;
int rightLow = 100;
int rightHigh = 110;
int leftLED = 4;
int rightLED = 2;
//Returns the time taken for the echo round trip to use in distance measurements
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
}
void loop()
{
//Calculate the distance based on the round trip time
cm = 0.01723 * readUltrasonicDistance(7, 6);
//Print distance to serial
Serial.print(cm);
Serial.println("cm");
if (cm >= leftLow && cm <= leftHigh)
{
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, LOW);
}
else if (cm >= rightLow && cm <= rightHigh)
{
digitalWrite(leftLED, LOW);
digitalWrite(rightLED, HIGH);
}
else
{
digitalWrite(leftLED, LOW);
digitalWrite(rightLED, LOW);
}
delay(100); // Wait for 100 millisecond(s)
}