// Define pin numbers for traffic lights
int red1 = 2;
int yellow1 = 3;
int green1 = 4;
int red2 = 5;
int yellow2 = 6;
int green2 = 7;
int red3 = 8;
int yellow3 = 9;
int green3 = 10;
int red4 = 11;
int yellow4 = 12;
int green4 = 13;
// Define pin numbers for emergency switches
int emergencySwitch1Pin = 18; // Corresponds to interrupt 5
int emergencySwitch2Pin = 19; // Corresponds to interrupt 4
int emergencySwitch3Pin = 20; // Corresponds to interrupt 3
int emergencySwitch4Pin = 21; // Corresponds to interrupt 2
volatile bool emergency1 = false;
volatile bool emergency2 = false;
volatile bool emergency3 = false;
volatile bool emergency4 = false;
void setup()
{
// Set pin modes for traffic lights
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT);
}
// Set pin modes for emergency switches
pinMode(emergencySwitch1Pin, INPUT_PULLUP);
pinMode(emergencySwitch2Pin, INPUT_PULLUP);
pinMode(emergencySwitch3Pin, INPUT_PULLUP);
pinMode(emergencySwitch4Pin, INPUT_PULLUP);
// Attach interrupts for emergency switches
attachInterrupt(digitalPinToInterrupt(emergencySwitch1Pin), handleEmergency1, RISING);
attachInterrupt(digitalPinToInterrupt(emergencySwitch2Pin), handleEmergency2, RISING);
attachInterrupt(digitalPinToInterrupt(emergencySwitch3Pin), handleEmergency3, RISING);
attachInterrupt(digitalPinToInterrupt(emergencySwitch4Pin), handleEmergency4, RISING);
}
void loop()
{
if (emergency1) {
emergency1 = false;
handleEmergency(green1);
} else if (emergency2) {
emergency2 = false;
handleEmergency(green2);
} else if (emergency3) {
emergency3 = false;
handleEmergency(green3);
} else if (emergency4) {
emergency4 = false;
handleEmergency(green4);
} else {
// If no emergency, operate traffic lights normally
normalOperation();
}
}
// Interrupt service routines (ISRs)
void handleEmergency1() {
emergency1 = true;
}
void handleEmergency2() {
emergency2 = true;
}
void handleEmergency3() {
emergency3 = true;
}
void handleEmergency4() {
emergency4 = true;
}
// Function to handle traffic lights during an emergency for a specific direction
void handleEmergency(int emergencyGreenPin)
{
// Turn off all lights
allLightsOff();
// Prioritize specific direction (green light)
digitalWrite(emergencyGreenPin, HIGH);
delay(10000); // Emergency vehicle passage time
digitalWrite(emergencyGreenPin, LOW);
}
// Function to operate traffic lights normally
void normalOperation()
{
direction(red1, yellow1, green1, red2, yellow2, green2, red3, yellow3, green3, red4, yellow4, green4);
direction(red2, yellow2, green2, red1, yellow1, green1, red3, yellow3, green3, red4, yellow4, green4);
direction(red3, yellow3, green3, red1, yellow1, green1, red2, yellow2, green2, red4, yellow4, green4);
direction(red4, yellow4, green4, red1, yellow1, green1, red2, yellow2, green2, red3, yellow3, green3);
}
// Function to turn off all lights
void allLightsOff()
{
for (int i = 2; i <= 13; i++) {
digitalWrite(i, LOW);
}
}
void direction(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l)
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(h, LOW);
digitalWrite(i, LOW);
digitalWrite(j, HIGH);
digitalWrite(k, LOW);
digitalWrite(l, LOW);
delay(5000);
digitalWrite(c, LOW);
digitalWrite(b, HIGH);
delay(3000);
}