-
Notifications
You must be signed in to change notification settings - Fork 0
/
1875- Smart Traffic Management System for Emergency vehicle.ino
88 lines (73 loc) · 2.06 KB
/
1875- Smart Traffic Management System for Emergency vehicle.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
#include <SPI.h>
#include <MFRC522.h>
// Define pins for RFID
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
// Define pins for LEDs (for a 4-way intersection)
#define RED_LED_1 4
#define YELLOW_LED_1 3
#define GREEN_LED_1 2
bool emergencyDetected = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize RFID
SPI.begin();
rfid.PCD_Init();
// Initialize LED pins for all directions
pinMode(RED_LED_1, OUTPUT);
pinMode(YELLOW_LED_1, OUTPUT);
pinMode(GREEN_LED_1, OUTPUT);
}
void loop() {
// Check for RFID Tag
if (emergencyDetected){
Serial.println("Emergency Vehicle Detected via RFID");
manageTrafficForEmergency();
rfid.PICC_HaltA();
}
// If no emergency is detected, maintain the normal traffic light sequence
if (!emergencyDetected) {
normalTrafficLight();
}
}
void manageTrafficForEmergency() {
stopAllDirections();
delay(7000); // Red light for 7 seconds to allow emergency vehicle to pass
// Clear emergency state and return to normal operation
emergencyDetected = false;
}
void test1(){
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.println("Emergency Vehicle Detected via RFID");
emergencyDetected = true;
rfid.PICC_HaltA();
}
}
void stopAllDirections() {
// Turn off all green and yellow LEDs, turn on all red LEDs
digitalWrite(GREEN_LED_1, LOW);
digitalWrite(YELLOW_LED_1, LOW);
digitalWrite(RED_LED_1, HIGH);
}
void normalTrafficLight() {
digitalWrite(GREEN_LED_1, LOW);
digitalWrite(YELLOW_LED_1, LOW);
digitalWrite(RED_LED_1, HIGH);
delay(3000);
test1();
if (emergencyDetected) return;
digitalWrite(GREEN_LED_1, LOW);
digitalWrite(YELLOW_LED_1, HIGH);
digitalWrite(RED_LED_1, LOW);
delay(2000);
test1();
if (emergencyDetected) return;
digitalWrite(GREEN_LED_1, HIGH);
digitalWrite(YELLOW_LED_1, LOW);
digitalWrite(RED_LED_1, LOW);
delay(3000);
test1();
if (emergencyDetected) return;
}