-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster.ino
More file actions
86 lines (79 loc) · 1.81 KB
/
Copy pathMaster.ino
File metadata and controls
86 lines (79 loc) · 1.81 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Port: COM6
#include <Wire.h>
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
int toggle = 0; //Determines which distance sensor to activate
void setup()
{
Wire.begin();
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
Serial.begin(9600);
}
void loop()
{
int count = 0;
int distance;
long duration;
while (toggle == 0) //Light 1 is active
{
digitalWrite(trigPin1, LOW);
delayMicroseconds(10);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = duration*0.034/2;
if (distance < 10 || distance > 1000)
{
count++;
}
else
{
count = 0;
}
if (count >= 60)
{
toggle = 1;
}
Serial.println(distance); //debug, but the program breaks if these print statements aren't here
delay(20);
}
Wire.beginTransmission(2); //Toggle the toggle variable
Wire.write(toggle);
Wire.endTransmission();
Serial.print("Toggle" + toggle);
count = 0;
while (toggle == 1) //Light 2 is active
{
digitalWrite(trigPin2, LOW);
delayMicroseconds(10);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = duration*0.034/2;
if (distance < 10 || distance > 1000)
{
count++;
}
else
{
count = 0;
}
if (count >= 70)
{
toggle = 0;
}
Serial.println(distance); //debug
delay(20);
}
Wire.beginTransmission(2); //Toggle the toggle variable
Wire.write(toggle);
Wire.endTransmission();
Serial.print("Toggle" + toggle);
}