-
Notifications
You must be signed in to change notification settings - Fork 0
/
trafficlights.ino
272 lines (226 loc) · 7.85 KB
/
trafficlights.ino
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const int trigPin1 = 27;
const int echoPin1 = 25;
const int trigPin2 = 14;
const int echoPin2 = 26;
const int trigPin3 = 16;
const int echoPin3 = 33;
const int red3 = 19;
const int red2 = 18;
const int red1 = 15;
const int green1 = 5;
const int green2 = 22;
const int green3 = 21;
//define sound speed in cm/uS
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long checker = 1;
long duration1 = 100000000, duration2 = 100000000, duration3 = 100000000;
float distanceCm1 = 1000, distanceCm2 = 1000, distanceCm3 = 1000;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT);
pinMode(red3, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green3, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(green1, OUTPUT);
}
void loop() {
if (checker == 1) {
// Clears the trigPin
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin3, LOW);
// delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin2, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// delayMicroseconds(10);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(10);
// digitalWrite(trigPin3, LOW);
// delayMicroseconds(10);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
delayMicroseconds(2);
// duration2 = pulseIn(echoPin2, HIGH);
// delayMicroseconds(2);
// duration3 = pulseIn(echoPin3, HIGH);
// delayMicroseconds(2);
// Calculate the distance
distanceCm1 = duration1 * SOUND_SPEED / 2;
distanceCm2 = duration2 * SOUND_SPEED / 2;
distanceCm3 = duration3 * SOUND_SPEED / 2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm) of first ultra sonic sensor: ");
Serial.println(distanceCm1);
Serial.print("Distance (cm) of second ultra sonic sensor: ");
Serial.println(distanceCm2);
Serial.print("Distance (cm) of third ultra sonic sensor: ");
Serial.println(distanceCm3);
delayMicroseconds(1000);
digitalWrite(red1, HIGH);
digitalWrite(green2, LOW);
digitalWrite(green3, HIGH);
digitalWrite(red2, HIGH);
digitalWrite(red3, LOW);
digitalWrite(green1, LOW);
}
if (distanceCm1 <= 16 && distanceCm2 >= 16 && distanceCm3 >= 16) {
// Clears the trigPin
// digitalWrite(trigPin1, LOW);
// delayMicroseconds(2);
digitalWrite(red2, HIGH);
digitalWrite(red3, HIGH);
digitalWrite(green1, HIGH);
digitalWrite(red1, LOW);
digitalWrite(green2, LOW);
digitalWrite(green3, LOW);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
// digitalWrite(trigPin1, HIGH);
// delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
// digitalWrite(trigPin1, LOW);
// delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
delayMicroseconds(10);
// digitalWrite(trigPin3, LOW);
// delayMicroseconds(10);
// Reads the echoPin, returns the sound wave travel time in microseconds
// duration1 = pulseIn(echoPin1, HIGH);
// delayMicroseconds(2);
duration2 = pulseIn(echoPin2, HIGH);
delayMicroseconds(2);
// duration3 = pulseIn(echoPin3, HIGH);
// delayMicroseconds(2);
// Calculate the distance
delay(2000);
distanceCm2 = duration2 * SOUND_SPEED / 2;
distanceCm3 = duration3 * SOUND_SPEED / 2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm) of first ultra sonic sensor: ");
Serial.println(distanceCm1);
Serial.print("Distance (cm) of second ultra sonic sensor: ");
Serial.println(distanceCm2);
Serial.print("Distance (cm) of third ultra sonic sensor: ");
Serial.println(distanceCm3);
checker=0;
}
if (distanceCm1 <= 16 && distanceCm2 <= 16 && distanceCm3 >= 16) {
// Clears the trigPin
// digitalWrite(trigPin1, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(2);
digitalWrite(red3, HIGH);
digitalWrite(green1, HIGH);
digitalWrite(green2, HIGH);
digitalWrite(green3, LOW);
digitalWrite(red1, LOW);
digitalWrite(red2, LOW);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2000);
// Sets the trigPin on HIGH state for 10 micro seconds
// digitalWrite(trigPin1, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin2, HIGH);
// delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
delay(5000);
// digitalWrite(trigPin1, LOW);
// delayMicroseconds(10);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
delayMicroseconds(10);
// // Reads the echoPin, returns the sound wave travel time in microseconds
// duration1 = pulseIn(echoPin1, HIGH);
// delayMicroseconds(2);
// duration2 = pulseIn(echoPin2, HIGH);
// delayMicroseconds(2);
duration3 = pulseIn(echoPin3, HIGH);
delayMicroseconds(2);
// Calculate the distance
distanceCm1 = duration1 * SOUND_SPEED / 2;
distanceCm2 = duration2 * SOUND_SPEED / 2;
distanceCm3 = duration3 * SOUND_SPEED / 2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm) of first ultra sonic sensor: ");
Serial.println(distanceCm1);
Serial.print("Distance (cm) of second ultra sonic sensor: ");
Serial.println(distanceCm2);
Serial.print("Distance (cm) of third ultra sonic sensor: ");
Serial.println(distanceCm3);
// delayMicroseconds(3000);
}
if (distanceCm1 <= 16 && distanceCm2 <= 16 && distanceCm3 <= 16) {
// Clears the trigPin
digitalWrite(red1, LOW);
digitalWrite(green2, HIGH);
digitalWrite(green3, HIGH);
digitalWrite(red2, LOW);
digitalWrite(red3, LOW);
digitalWrite(green1, HIGH);
delay(2000);
digitalWrite(trigPin1, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin2, HIGH);
// delayMicroseconds(2);
// digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
delayMicroseconds(10);
// digitalWrite(trigPin2, LOW);
// delayMicroseconds(10);
// digitalWrite(trigPin3, LOW);
// delayMicroseconds(10);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
delayMicroseconds(2);
// duration2 = pulseIn(echoPin2, HIGH);
// delayMicroseconds(2);
// duration3 = pulseIn(echoPin3, HIGH);
// delayMicroseconds(2);
// Calculate the distance
distanceCm1 = duration1 * SOUND_SPEED / 2;
distanceCm2 = duration2 * SOUND_SPEED / 2;
distanceCm3 = duration3 * SOUND_SPEED / 2;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm) of first ultra sonic sensor: ");
Serial.println(distanceCm1);
Serial.print("Distance (cm) of second ultra sonic sensor: ");
Serial.println(distanceCm2);
Serial.print("Distance (cm) of third ultra sonic sensor: ");
Serial.println(distanceCm3);
checker = 0;
}
}