-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic_light.c
369 lines (369 loc) · 7.53 KB
/
traffic_light.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "LedControl.h"
//定义向南红绿灯针脚
int south_red = 8;
int south_yellow = 9;
int south_green = 10;
//通过时间变化
int num_time = 1;
int num_stop = 0;
//定义向北红绿灯针脚
int north_red = 5;
int north_yellow = 6;
int north_green = 7;
//定义调整通过时间按钮针脚
int time_button=4;
//定义急停按钮针脚
int stop_button=3;
//定义蜂鸣器针脚
int buzzer=2;
//定义8×8点阵针脚
int CLK = 11;
int CS = 12;
int DIN = 13;
LedControl lc=LedControl(DIN,CLK,CS,1);
//向北指示箭头点阵屏样式
byte north[8]={
B00010000,
B00110000,
B01110000,
B11111111,
B11111111,
B01110000,
B00110000,
B00010000,};
//向南指示箭头点阵屏样式
byte south[8]={
B00001000,
B00001100,
B00001110,
B11111111,
B11111111,
B00001110,
B00001100,
B00001000,};
//倒计时3点阵屏样式
byte L_3[8]={
B00000000,
B01000010,
B11010011,
B11011011,
B11011011,
B01111110,
B00110100,
B00000000,};
//倒计时2点阵屏样式
byte L_2[8]={
B10000100,
B11000110,
B11000011,
B11100011,
B11110011,
B11011110,
B11001100,
B10000000,};
//倒计时1点阵屏样式
byte L_1[8]={
B00000000,
B10000100,
B10000110,
B11111111,
B11111111,
B10000000,
B10000000,
B00000000,};
//紧急停止点阵屏样式
byte stop[8]={
B10000001,
B01000010,
B00100100,
B00011000,
B00011000,
B00100100,
B01000010,
B10000001,};
//黑屏点阵屏样式
byte black[8]={
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,};
void setup(){
//设置南向红绿灯针脚模式为输出
pinMode(south_red, OUTPUT);
pinMode(south_yellow, OUTPUT);
pinMode(south_green, OUTPUT);
//设置北向红绿灯针脚模式为输出
pinMode(north_red, OUTPUT);
pinMode(north_yellow, OUTPUT);
pinMode(north_green, OUTPUT);
//设置调整通过时间按钮针脚模式为输入
pinMode(time_button, INPUT);
//设置急停按钮针脚模式为输入
pinMode(stop_button, INPUT);
//设置蜂鸣器针脚模式为输出
pinMode(buzzer, OUTPUT);
}
void loop(){
int val_time=digitalRead(time_button);
//if判断按键是否按下
if(val_time==0)
{
delay(50);
if(val_time==0)
{
num_time++;
//wihile判断当按键被按住时暂停程序
while(digitalRead(time_button)==0)
{
delay(50);
}
}
}
if(num_time==1)
{
//红绿灯10S切换方向
go_10s();
}
if(num_time==2)
{
//红绿灯10S切换方向
go_15s();
}
if(num_time==3)
{
//红绿灯10S切换方向
go_20s();
}
if(num_time==4)
{
//红绿灯10S切换方向
go_10s();
//返回10S切换方向状态
num_time=1;
}
if(num_time==5)
{
//不进行任何操作
}
}
//当向南为绿灯时各信号灯状态
void south_go(){
LED_south();
digitalWrite(north_red, HIGH);
digitalWrite(south_red, LOW);
digitalWrite(south_green, HIGH);
digitalWrite(north_green, LOW);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
}
//当向北为绿灯时各信号灯状态
void north_go(){
LED_north();
digitalWrite(north_red, LOW);
digitalWrite(south_red, HIGH);
digitalWrite(south_green, LOW);
digitalWrite(north_green, HIGH);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
}
//黄灯闪烁三秒+倒计时
void yellow_flush(){
LED_3();
digitalWrite(south_yellow, HIGH);
digitalWrite(north_yellow, HIGH);
flag_test();
delay(500);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
flag_test();
delay(500);
LED_2();
digitalWrite(south_yellow, HIGH);
digitalWrite(north_yellow, HIGH);
flag_test();
delay(500);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
flag_test();
delay(500);
LED_1();
digitalWrite(south_yellow, HIGH);
digitalWrite(north_yellow, HIGH);
flag_test();
delay(500);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
flag_test();
delay(500);
}
//向北指示箭头显示函数
void LED_north(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,north[row]);
}
//向南指示箭头显示函数
void LED_south(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,south[row]);
}
//倒计时3显示函数
void LED_3(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,L_3[row]);
}
//倒计时2显示函数
void LED_2(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,L_2[row]);
}
//倒计时1显示函数
void LED_1(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,L_1[row]);
}
//紧急停止显示函数
void LED_stop(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,stop[row]);
}
//黑屏显示函数
void LED_black(){
lc.shutdown(0,false);
lc.setIntensity(0,3);
lc.clearDisplay(0);
for (int row=0;row<8;row++)
lc.setRow(0,row,black[row]);
}
//红绿灯10S切换方向
void go_10s (){
int delay_time;
south_go();
for(delay_time=0;delay_time<=7;delay_time++)
{
LED_south();
flag_test();
delay(1000);
}
yellow_flush();
north_go();
for(delay_time=0;delay_time<=7;delay_time++)
{
LED_north();
flag_test();
delay(1000);
}
yellow_flush();
}
//红绿灯15S切换方向
void go_15s (){
int delay_time;
south_go();
yellow_flush();
for(delay_time=0;delay_time<=12;delay_time++)
{
LED_south();
flag_test();
delay(1000);
}
north_go();
for(delay_time=0;delay_time<=12;delay_time++)
{
LED_north();
flag_test();
delay(1000);
}
yellow_flush();
}
//红绿灯20S切换方向
void go_20s (){
int delay_time;
south_go();
for(delay_time=0;delay_time<=17;delay_time++)
{
LED_south();
flag_test();
delay(1000);
}
yellow_flush();
north_go();
for(delay_time=0;delay_time<=17;delay_time++)
{
LED_north();
flag_test();
delay(1000);
}
yellow_flush();
}
//紧急停止状态
void all_stop(){
num_stop=1;
digitalWrite(north_red, HIGH);
digitalWrite(south_red, HIGH);
digitalWrite(south_green, LOW);
digitalWrite(north_green, LOW);
digitalWrite(south_yellow, LOW);
digitalWrite(north_yellow, LOW);
digitalWrite(buzzer, HIGH);
LED_stop();
delay(500);
digitalWrite(buzzer, LOW);
LED_black();
delay(500);
}
void flag_test(){
int val_stop=digitalRead(stop_button);
//if判断按键是否按下
if(val_stop==0)
{
delay(50);
if(val_stop==0)
{
num_stop++;
//wihile判断当按键被按住时暂停程序
while(digitalRead(stop_button)==0)
{
delay(50);
}
}
}
if(num_stop==0)
{
//不进行任何操作
}
if(num_stop==1)
{
//使交通灯控制模块不进行任何操作
num_time=5;
//设置为紧急停止状态
all_stop();
}
if(num_stop>=2)
{
//关闭紧急停止状态
num_stop=0;
//设置为10S模式
num_time=1;
}
}