-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.ino
282 lines (251 loc) · 6.96 KB
/
project.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
#include<TimerOne.h>
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_1_PIN 22 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
#define SS_2_PIN 23 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
#define SS_3_PIN 24
#define SS_4_PIN 25
#define NR_OF_READERS 4
byte ssPins[] = {SS_1_PIN, SS_2_PIN,SS_3_PIN,SS_4_PIN};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
int signal1[] = {46,43,42,47};// Red,yellow,green,blue
int signal2[] = {40,39,38,41};
int signal3[] = {11,12,13,10};
int signal4[] = {7,8,9,6};
int i;
int redDelay = 5000;
int blueDelay = 4500;
int yellowDelay = 3000;
uint8_t reader=60;
volatile int triggerpin1 = 29;
volatile int echopin1 = 28;
volatile int triggerpin2 = 31;
volatile int echopin2 = 30;
volatile int triggerpin3 = 33;
volatile int echopin3 = 32;
volatile int triggerpin4 = 35;
volatile int echopin4 = 34;
volatile long time; // Variable for storing the time traveled
volatile int S1, S2, S3, S4; // Variables for storing the distance covered
int t = 7; // distance under which it will look for vehicles.
void setup(){
Serial.begin(9600);
SPI.begin();
Timer1.initialize(700000); //Begin using the timer. This function must be called first. "microseconds" is the period of time the timer takes.
Timer1.attachInterrupt(softInterr); //Run a function each time the timer period finishes.
// Declaring LED pins as output
for(int i=0; i<4; i++){
pinMode(signal1[i], OUTPUT);
pinMode(signal2[i], OUTPUT);
pinMode(signal3[i], OUTPUT);
pinMode(signal4[i], OUTPUT);
}
// Declaring ultrasonic sensor pins as output
pinMode(triggerpin1, OUTPUT);
pinMode(echopin1, INPUT);
pinMode(triggerpin2, OUTPUT);
pinMode(echopin2, INPUT);
pinMode(triggerpin3, OUTPUT);
pinMode(echopin3, INPUT);
pinMode(triggerpin4, OUTPUT);
pinMode(echopin4, INPUT);
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN);} // Init each MFRC522 card
}
void loop()
{
// If there are vehicles at signal 1
if(S1<t)
{
signal1Function();
}
// If there are vehicles at signal 2
if(S2<t)
{
signal2Function();
}
// If there are vehicles at signal 3
if(S3<t)
{
signal3Function();
}
// If there are vehicles at signal 4
if(S4<t)
{
signal4Function();
}
}
// This is interrupt function and it will run each time the timer period finishes. The timer period is set at 100 milli seconds.
void softInterr()
{
for (reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Rfid reader "));
Serial.print(reader);
Serial.print(F(" detected.Calling "));
if(reader==0){signal1Function();blue1();}
if(reader==1){signal2Function();blue2();}
if(reader==2){signal3Function();blue3();}
if(reader==3){signal4Function();blue4();}
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
// Reading from first ultrasonic sensor
digitalWrite(triggerpin1, LOW);
delayMicroseconds(2);
digitalWrite(triggerpin1, HIGH);
delayMicroseconds(10);
digitalWrite(triggerpin1, LOW);
time = pulseIn(echopin1, HIGH);
S1= time*0.034/2;
// Reading from second ultrasonic sensor
digitalWrite(triggerpin2, LOW);
delayMicroseconds(2);
digitalWrite(triggerpin2, HIGH);
delayMicroseconds(10);
digitalWrite(triggerpin2, LOW);
time = pulseIn(echopin2, HIGH);
S2= time*0.034/2;
// Reading from third ultrasonic sensor
digitalWrite(triggerpin3, LOW);
delayMicroseconds(2);
digitalWrite(triggerpin3, HIGH);
delayMicroseconds(10);
digitalWrite(triggerpin3, LOW);
time = pulseIn(echopin3, HIGH);
S3= time*0.034/2;
// Reading from fourth ultrasonic sensor
digitalWrite(triggerpin4, LOW);
delayMicroseconds(2);
digitalWrite(triggerpin4, HIGH);
delayMicroseconds(10);
digitalWrite(triggerpin4, LOW);
time = pulseIn(echopin4, HIGH);
S4= time*0.034/2;
}
void signal1Function()
{
Serial.println("signal 1");
low();
// Make RED LED LOW and make Green HIGH for 5 seconds
digitalWrite(signal1[0], LOW);
digitalWrite(signal1[2], HIGH);
delay(redDelay);
// if there are vehicels at other signals
if(S2<t || S3<t || S4<t)
{
// Make Green LED LOW and make yellow LED HIGH for 2 seconds
digitalWrite(signal1[2], LOW);
digitalWrite(signal1[1], HIGH);
delay(yellowDelay);
}
}
void signal2Function()
{
Serial.println("signal 2");
low();
digitalWrite(signal2[0], LOW);
digitalWrite(signal2[2], HIGH);
delay(redDelay);
if(S1<t || S3<t || S4<t)
{
digitalWrite(signal2[2], LOW);
digitalWrite(signal2[1], HIGH);
delay(yellowDelay);
}
}
void signal3Function()
{
Serial.println("signal 3");
low();
digitalWrite(signal3[0], LOW);
digitalWrite(signal3[2], HIGH);
delay(redDelay);
if(S1<t || S2<t || S4<t)
{
digitalWrite(signal3[2], LOW);
digitalWrite(signal3[1], HIGH);
delay(yellowDelay);
}
}
void signal4Function()
{
Serial.println("signal 4");
low();
digitalWrite(signal4[0], LOW);
digitalWrite(signal4[2], HIGH);
delay(redDelay);
if(S1<t || S2<t || S3<t)
{
digitalWrite(signal4[2], LOW);
digitalWrite(signal4[1], HIGH);
delay(yellowDelay);
}
}
// Function to make all LED's LOW except RED one's.
void low()
{
for(int i=1; i<3; i++)
{
digitalWrite(signal1[i], LOW);
digitalWrite(signal2[i], LOW);
digitalWrite(signal3[i], LOW);
digitalWrite(signal4[i], LOW);
}
for(int i=0; i<1; i++)
{
digitalWrite(signal1[i], HIGH);
digitalWrite(signal2[i], HIGH);
digitalWrite(signal3[i], HIGH);
digitalWrite(signal4[i], HIGH);
}
}
void blue1()
{
for(i=0;i<20;i++){
digitalWrite(signal1[3], LOW);
delay(blueDelay);
digitalWrite(signal1[3], HIGH);
delay(blueDelay);
digitalWrite(signal1[3], LOW);
}
Serial.print(F("blue1 "));
}
void blue2()
{
for(i=0;i<20;i++){
digitalWrite(signal2[3], LOW);
delay(blueDelay);
digitalWrite(signal2[3], HIGH);
delay(blueDelay);
digitalWrite(signal2[3], LOW);
}
Serial.print(F("blue2 "));
}
void blue3()
{
for(i=0;i<20;i++){
digitalWrite(signal3[3], LOW);
delay(blueDelay);
digitalWrite(signal3[3], HIGH);
delay(blueDelay);
digitalWrite(signal3[3], LOW);
}
Serial.print(F("blue3 "));
}
void blue4()
{
for(i=0;i<20;i++){
digitalWrite(signal4[3], LOW);
delay(blueDelay);
digitalWrite(signal4[3], HIGH);
delay(blueDelay);
digitalWrite(signal4[3], LOW);
}
Serial.print(F("blue4 "));
}