-
Notifications
You must be signed in to change notification settings - Fork 5
/
Traffic_signal.ino
188 lines (177 loc) · 4.4 KB
/
Traffic_signal.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
//First of all, we define the pins where we have
//connected the LEDs.
int red_1=13;
int orange_1=12;
int green_1=11;
int red_2=10;
int orange_2=9;
int green_2=8;
int red_3=7;
int orange_3=6;
int green_3=5;
int red_4=4;
int orange_4=3;
int green_4=2;
int LEDState=0;
int LEDPin=1;
int ButtonPin=0;
int ButtonNew;
int ButtonOld=1;
void direction_1_green(void)//green LED of direction 1 will turn ON
{
digitalWrite(red_1,LOW);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,HIGH);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void direction_2_orange(void)//orange LED of direction 2 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,LOW);
digitalWrite(orange_2,HIGH);
digitalWrite(green_2,LOW);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void direction_2_green(void)//green LED of direction 2 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,LOW);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,HIGH);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void direction_3_orange(void)//orange LED of direction 3 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,LOW);
digitalWrite(orange_3,HIGH);
digitalWrite(green_3,LOW);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void direction_3_green(void)//green LED of direction 3 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,LOW);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,HIGH);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void direction_4_orange(void)//orange LED of direction 4 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,LOW);
digitalWrite(orange_4,HIGH);
digitalWrite(green_4,LOW);
}
void direction_4_green(void)//green LED of direction 4 will turn ON
{
digitalWrite(red_1,HIGH);
digitalWrite(orange_1,LOW);
digitalWrite(green_1,LOW);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,LOW);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,HIGH);
}
void direction_1_orange(void)//orange LED of direction 1 will turn ON
{
digitalWrite(red_1,LOW);
digitalWrite(orange_1,HIGH);
digitalWrite(green_1,LOW);
digitalWrite(red_2,HIGH);
digitalWrite(orange_2,LOW);
digitalWrite(green_2,LOW);
digitalWrite(red_3,HIGH);
digitalWrite(orange_3,LOW);
digitalWrite(green_3,LOW);
digitalWrite(red_4,HIGH);
digitalWrite(orange_4,LOW);
digitalWrite(green_4,LOW);
}
void pedestrian(void){
ButtonNew=digitalRead(ButtonPin);
if (ButtonOld==0 && ButtonNew==1){
if (LEDState==0){
digitalWrite(LEDPin,HIGH);
LEDState=1;
}
else{
digitalWrite(LEDPin,LOW);
LEDState=0;
}
}
}
void setup() { // Declaring all the LED's as output
for(int i=1;i<=13;i++){
pinMode(i,OUTPUT);
}
pinMode(ButtonPin,INPUT);
}
void loop() //In the loop function, we controlled the signal one by one to control the flow of traffic.
{
direction_1_green();
delay(5000);
direction_2_orange();
delay(3000);
direction_2_green();
delay(5000);
direction_3_orange();
delay(3000);
direction_3_green();
delay(5000);
direction_4_orange();
delay(3000);
direction_4_green();
delay(5000);
direction_1_orange();
delay(3000);
}