// Pin Definitions for Lane 1
const int buttonPin1 = 2; // Button for Lane 1
const int redLightPin1 = 3; // Red light for Lane 1
const int yellowLightPin1 = 4; // Yellow light for Lane 1
const int greenLightPin1 = 5; // Green light for Lane 1
const int irSensorPin1 = 10; // IR Sensor for Lane 1
const int potPin1 = A0; // Potentiometer for Lane 1
// Pin Definitions for Lane 2
const int buttonPin2 = 6; // Button for Lane 2
const int redLightPin2 = 7; // Red light for Lane 2
const int yellowLightPin2 = 8; // Yellow light for Lane 2
const int greenLightPin2 = 9; // Green light for Lane 2
const int irSensorPin2 = 11; // IR Sensor for Lane 2
const int potPin2 = A1; // Potentiometer for Lane 2
// Variables to store button states
int buttonState1 = HIGH;
int lastButtonState1 = HIGH;
int buttonState2 = HIGH;
int lastButtonState2 = HIGH;
// Variables for debouncing
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
const unsigned long debounceDelay = 100; // 100ms debounce time
// Traffic light timing variables
unsigned long yellowDuration = 2000; // Yellow light duration
unsigned long redDuration = 5000; // Default red light duration
void setup() {
// Lane 1 Setup
pinMode(buttonPin1, INPUT_PULLUP); // Button for Lane 1 with pull-up
pinMode(redLightPin1, OUTPUT); // Red light for Lane 1
pinMode(yellowLightPin1, OUTPUT); // Yellow light for Lane 1
pinMode(greenLightPin1, OUTPUT); // Green light for Lane 1
pinMode(irSensorPin1, INPUT); // IR sensor for Lane 1
pinMode(potPin1, INPUT); // Potentiometer for Lane 1
// Lane 2 Setup
pinMode(buttonPin2, INPUT_PULLUP); // Button for Lane 2 with pull-up
pinMode(redLightPin2, OUTPUT); // Red light for Lane 2
pinMode(yellowLightPin2, OUTPUT); // Yellow light for Lane 2
pinMode(greenLightPin2, OUTPUT); // Green light for Lane 2
pinMode(irSensorPin2, INPUT); // IR sensor for Lane 2
pinMode(potPin2, INPUT); // Potentiometer for Lane 2
// Initialize lights (all red initially)
digitalWrite(redLightPin1, HIGH);
digitalWrite(yellowLightPin1, LOW);
digitalWrite(greenLightPin1, LOW);
digitalWrite(redLightPin2, HIGH);
digitalWrite(yellowLightPin2, LOW);
digitalWrite(greenLightPin2, LOW);
// Start serial communication for debugging
Serial.begin(9600);
// Test LEDs at startup
Serial.println("Testing LEDs...");
testLights();
}
void loop() {
// Handle button presses with debounce logic
handleButtonPress(buttonPin1, buttonState1, lastButtonState1, lastDebounceTime1, handleEmergencyLane1, 1);
handleButtonPress(buttonPin2, buttonState2, lastButtonState2, lastDebounceTime2, handleEmergencyLane2, 2);
// Vehicle detection and dynamic traffic light control
controlTrafficLights();
}
// Function to handle debounced button presses
void handleButtonPress(int buttonPin, int &buttonState, int &lastButtonState, unsigned long &lastDebounceTime, void (*emergencyHandler)(), int lane) {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
Serial.print("Lane ");
Serial.print(lane);
Serial.println(": Emergency Button Pressed!");
emergencyHandler();
}
}
}
lastButtonState = reading;
}
// Function to control traffic lights based on vehicle detection
void controlTrafficLights() {
bool vehicleDetected1 = digitalRead(irSensorPin1) == LOW; // Assuming LOW indicates vehicle detection
bool vehicleDetected2 = digitalRead(irSensorPin2) == LOW;
// Read potentiometer values and map them to a range (3000ms to 10000ms)
unsigned long greenDuration1 = map(analogRead(potPin1), 0, 1023, 3000, 10000);
unsigned long greenDuration2 = map(analogRead(potPin2), 0, 1023, 3000, 10000);
if (vehicleDetected1 && !vehicleDetected2) {
Serial.println("Lane 1: Higher traffic detected, extending green light duration.");
setTrafficLight(greenLightPin1, yellowLightPin1, redLightPin1, greenDuration1);
setTrafficLight(redLightPin2, yellowLightPin2, greenLightPin2, redDuration);
} else if (vehicleDetected2 && !vehicleDetected1) {
Serial.println("Lane 2: Higher traffic detected, extending green light duration.");
setTrafficLight(greenLightPin2, yellowLightPin2, redLightPin2, greenDuration2);
setTrafficLight(redLightPin1, yellowLightPin1, greenLightPin1, redDuration);
} else if (vehicleDetected1 && vehicleDetected2) {
Serial.println("Both lanes: Vehicles detected, adjusting green light duration.");
setTrafficLight(greenLightPin1, yellowLightPin1, redLightPin1, greenDuration1);
setTrafficLight(greenLightPin2, yellowLightPin2, redLightPin2, greenDuration2);
} else {
Serial.println("Equal or no traffic detected, default timing.");
setTrafficLight(greenLightPin1, yellowLightPin1, redLightPin1, greenDuration1);
setTrafficLight(greenLightPin2, yellowLightPin2, redLightPin2, greenDuration2);
}
}
// Function to set traffic light timing for a lane
void setTrafficLight(int greenPin, int yellowPin, int redPin, unsigned long greenTime) {
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
delay(greenTime);
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, HIGH);
delay(yellowDuration);
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, HIGH);
}
// Function to handle emergency for Lane 1
void handleEmergencyLane1() {
Serial.println("Lane 1: Turning on green light...");
digitalWrite(greenLightPin1, HIGH);
digitalWrite(redLightPin1, LOW);
digitalWrite(yellowLightPin1, LOW);
// Keep green light on for 5 seconds
delay(5000);
Serial.println("Lane 1: Resetting to red light...");
digitalWrite(greenLightPin1, LOW);
digitalWrite(redLightPin1, HIGH);
digitalWrite(yellowLightPin1, LOW);
}
// Function to handle emergency for Lane 2
void handleEmergencyLane2() {
Serial.println("Lane 2: Turning on green light...");
digitalWrite(greenLightPin2, HIGH);
digitalWrite(redLightPin2, LOW);
digitalWrite(yellowLightPin2, LOW);
// Keep green light on for 5 seconds
delay(5000);
Serial.println("Lane 2: Resetting to red light...");
digitalWrite(greenLightPin2, LOW);
digitalWrite(redLightPin2, HIGH);
digitalWrite(yellowLightPin2, LOW);
}
// Function to test LEDs during setup
void testLights() {
digitalWrite(redLightPin1, HIGH);
delay(500);
digitalWrite(yellowLightPin1, HIGH);
delay(500);
digitalWrite(greenLightPin1, HIGH);
delay(500);
digitalWrite(redLightPin1, LOW);
digitalWrite(yellowLightPin1, LOW);
digitalWrite(greenLightPin1, LOW);
digitalWrite(redLightPin2, HIGH);
delay(500);
digitalWrite(yellowLightPin2, HIGH);
delay(500);
digitalWrite(greenLightPin2, HIGH);
delay(500);
digitalWrite(redLightPin2, LOW);
digitalWrite(yellowLightPin2, LOW);
digitalWrite(greenLightPin2, LOW);
Serial.println("LED Test Complete.");
}