-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_traffic_signal_system1.ino
288 lines (199 loc) · 6.27 KB
/
manual_traffic_signal_system1.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
int var = 0; // variable
//GREEN
int led_GREEN_left = 2;
int led_GREEN_down = 8;
int led_GREEN_right = 11;
int led_GREEN_up = 5;
//YELLOW
int led_YELLOW_left = A1;
int led_YELLOW_down = 7;
int led_YELLOW_right = 10;
int led_YELLOW_up = 4;
//RED
int led_RED_left = A0;
int led_RED_down = 6;
int led_RED_right = 9;
int led_RED_up = 3;
//BUTTONS
int button_left = 12;
int button_right = A2;
int counter =0;
//int left=0,right =0,up=0,down=0;
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
int state1 = HIGH; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0; // the last time the output pin was toggled
long debounce1 = 200; // the debounce time, increase if the output flickers
void setup() {
pinMode(led_RED_left, OUTPUT);
pinMode(led_YELLOW_left , OUTPUT);
pinMode(led_GREEN_left, OUTPUT);
pinMode(led_RED_down, OUTPUT);
pinMode(led_YELLOW_down, OUTPUT);
pinMode(led_GREEN_down, OUTPUT);
pinMode(led_RED_right, OUTPUT);
pinMode(led_YELLOW_right, OUTPUT);
pinMode(led_GREEN_right, OUTPUT);
pinMode(led_RED_up, OUTPUT);
pinMode(led_YELLOW_up, OUTPUT);
pinMode(led_GREEN_up, OUTPUT);
pinMode(button_left, INPUT);
pinMode(button_right, INPUT);
Serial.begin(9600);
digitalWrite(button_left,HIGH);
digitalWrite(button_right,HIGH);
digitalWrite(led_RED_left, HIGH);
digitalWrite(led_YELLOW_left , LOW);
digitalWrite(led_GREEN_left, LOW);
digitalWrite(led_RED_down, HIGH);
digitalWrite(led_YELLOW_down, LOW);
digitalWrite(led_GREEN_down, LOW);
digitalWrite(led_RED_right, HIGH);
digitalWrite(led_YELLOW_right, LOW);
digitalWrite(led_GREEN_right, LOW);
digitalWrite(led_RED_up, LOW);
digitalWrite(led_YELLOW_up, LOW);
digitalWrite(led_GREEN_up, HIGH);
}
void loop()
{
reading = digitalRead(button_left);
reading1 = digitalRead(button_right);
//
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH){
digitalWrite(led_YELLOW_up, HIGH);
digitalWrite(led_YELLOW_down, HIGH);
digitalWrite(led_YELLOW_right, HIGH);
digitalWrite(led_YELLOW_left, HIGH);
delay(100);
digitalWrite(led_YELLOW_up, LOW);
digitalWrite(led_YELLOW_down, LOW);
digitalWrite(led_YELLOW_right, LOW);
digitalWrite(led_YELLOW_left, LOW);
delay(100);
state = LOW;
}else{
digitalWrite(led_YELLOW_up, HIGH);
digitalWrite(led_YELLOW_down, HIGH);
digitalWrite(led_YELLOW_right, HIGH);
digitalWrite(led_YELLOW_left, HIGH);
delay(100);
digitalWrite(led_YELLOW_up, LOW);
digitalWrite(led_YELLOW_down, LOW);
digitalWrite(led_YELLOW_right, LOW);
digitalWrite(led_YELLOW_left, LOW);
delay(100);
state = HIGH;
}
time = millis();
}else if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
if (state1 == HIGH){
digitalWrite(led_YELLOW_up, HIGH);
digitalWrite(led_YELLOW_down, HIGH);
digitalWrite(led_YELLOW_right, HIGH);
digitalWrite(led_YELLOW_left, HIGH);
delay(100);
digitalWrite(led_YELLOW_up, LOW);
digitalWrite(led_YELLOW_down, LOW);
digitalWrite(led_YELLOW_right, LOW);
digitalWrite(led_YELLOW_left, LOW);
delay(100);
state1 = LOW;
}else{
digitalWrite(led_YELLOW_up, HIGH);
digitalWrite(led_YELLOW_down, HIGH);
digitalWrite(led_YELLOW_right, HIGH);
digitalWrite(led_YELLOW_left, HIGH);
delay(100);
digitalWrite(led_YELLOW_up, LOW);
digitalWrite(led_YELLOW_down, LOW);
digitalWrite(led_YELLOW_right, LOW);
digitalWrite(led_YELLOW_left, LOW);
delay(100);
state1 = HIGH;
}
time1 = millis();
}
if(state == LOW && state1 == LOW){
left_RED_t0_GREEN();
}else if(state == LOW && state1 == HIGH ) {
down_RED_t0_GREEN();
}else if(state == HIGH && state1 == LOW ) {
right_RED_t0_GREEN();
}else if(state == HIGH && state1 == HIGH ) {
up_RED_t0_GREEN();
}
previous = reading;
previous1 = reading1;
//
}
void left_GREEN_t0_RED(){
// left_RED=1;
digitalWrite(led_RED_left, HIGH);
// left_GREEN=0;
digitalWrite(led_GREEN_left,LOW);
}
void left_RED_t0_GREEN(){
// left_RED=0;
digitalWrite(led_RED_left,LOW);
up_GREEN_t0_RED();
down_GREEN_t0_RED();
right_GREEN_t0_RED();
// left_GREEN=1;
digitalWrite(led_GREEN_left,HIGH);
}
void right_GREEN_t0_RED(){
// right_RED=1;
digitalWrite(led_RED_right, HIGH);
// right_GREEN=0;
digitalWrite(led_GREEN_right,LOW);
}
void right_RED_t0_GREEN(){
// right_RED=0;
digitalWrite(led_RED_right, LOW);
up_GREEN_t0_RED();
down_GREEN_t0_RED();
left_GREEN_t0_RED();
// right_GREEN=1;
digitalWrite(led_GREEN_right, HIGH);
}
void up_GREEN_t0_RED(){
// up_RED=1;
digitalWrite(led_RED_up, HIGH);
// up_GREEN=0;
digitalWrite(led_GREEN_up, LOW);
}
void up_RED_t0_GREEN(){
// up_RED=0;
digitalWrite(led_RED_up, LOW);
right_GREEN_t0_RED();
down_GREEN_t0_RED();
left_GREEN_t0_RED();
// up_GREEN=1;
digitalWrite(led_GREEN_up, HIGH);
}
void down_GREEN_t0_RED(){
// down_RED=1;
digitalWrite(led_RED_down, HIGH);
// down_GREEN=0;
digitalWrite(led_GREEN_down, LOW);
}
void down_RED_t0_GREEN(){
// down_RED=0;
digitalWrite(led_RED_down, LOW);
up_GREEN_t0_RED();
right_GREEN_t0_RED();
left_GREEN_t0_RED();
// down_GREEN=1;
digitalWrite(led_GREEN_down, HIGH);
}