-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrigger Ranges
More file actions
68 lines (56 loc) · 1.41 KB
/
Trigger Ranges
File metadata and controls
68 lines (56 loc) · 1.41 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
61
62
63
64
65
66
67
68
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
int distance = 0;
const int triggerPin = 2;
const int echoPin = 3;
const int leftLow = 125;
const int leftHigh = 135;
const int rightLow = 95;
const int rightHigh = 105;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
int counter;
void setup()
{
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
servoLeft.attach(9);
servoRight.attach(10);
}
void loop()
{
distance = 0;
distance = 0.01723 * readUltrasonicDistance(triggerPin, echoPin);
if (distance >= leftLow && distance <= leftHigh)
{
Serial.print("Left triggered: ");
Serial.println(distance);
servoLeft.write(90);
delay(2000);
}
else if (distance >= rightLow && distance <= rightHigh)
{
Serial.print("Right triggered: ");
Serial.println(distance);
servoRight.write(90);
delay(2000);
}
else
{
Serial.println("No trigger");
servoLeft.write(0);
servoRight.write(0);
}
delay(1000); // Wait for 1 sec
}