#include <LiquidCrystal.h>
LiquidCrystal LCD(32, 33, 34, 35, 36, 37);
const int TRIG_PIN[] = {2, 4, 6, 8, 10, 12, 14, 16};
const int ECHO_PIN[] = {3, 5, 7, 9, 11, 13, 15, 17};
const int NUM_SENSORS = 8;
const int MAX_DISTANCE_CM = 4; // Maximum distance for car detection
// Define constants for LED pins
const int RED_LED_PIN[] = {18, 22, 25, 28};
const int YELLOW_LED_PIN[] = {19, 23, 26, 29};
const int GREEN_LED_PIN[] = {20, 24, 27, 30};
const int NUM_ROADS = 4;
// Define constants for traffic light timings
const int NORMAL_GREEN_DURATION = 2000;
const int FIRST_DETECTED_GREEN_DURATION = 4000;
const int BOTH_GREEN_DURATION = 6000;
const int YELLOW_DURATION = 1000;
// Variable to store the time the green LED is on
int greenTime = 0;
// Function to read distance from ultrasonic sensor
int readDistance(int sensorIndex) {
digitalWrite(TRIG_PIN[sensorIndex], LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN[sensorIndex], HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN[sensorIndex], LOW);
// Convert pulse duration to distance in cm
return pulseIn(ECHO_PIN[sensorIndex], HIGH) / 58;
}
void setup() {
// Initialize ultrasonic sensor pins
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(TRIG_PIN[i], OUTPUT);
pinMode(ECHO_PIN[i], INPUT);
}
// Initialize LED pins
for (int i = 0; i < NUM_ROADS; i++) {
pinMode(RED_LED_PIN[i], OUTPUT);
pinMode(YELLOW_LED_PIN[i], OUTPUT);
pinMode(GREEN_LED_PIN[i], OUTPUT);
}
LCD.begin(16,2);
}
void loop() {
// Loop through each road
for (int currentRoad = 0; currentRoad < NUM_ROADS; currentRoad++) {
// Turn off all lights
for (int i = 0; i < NUM_ROADS; i++) {
digitalWrite(RED_LED_PIN[i], HIGH);
digitalWrite(YELLOW_LED_PIN[i], LOW);
digitalWrite(GREEN_LED_PIN[i], LOW);
}
// Read distances from both sensors on the current road
int firstSensorDistance = readDistance(currentRoad * 2);
int secondSensorDistance = readDistance(currentRoad * 2 + 1);
// Determine the green light duration based on sensor detection
int greenDuration;
if (firstSensorDistance < MAX_DISTANCE_CM && secondSensorDistance < MAX_DISTANCE_CM) {
// Both sensors detect a car
greenDuration = BOTH_GREEN_DURATION;
} else if (firstSensorDistance < MAX_DISTANCE_CM) {
// Only the first sensor detects a car
greenDuration = FIRST_DETECTED_GREEN_DURATION;
} else if (secondSensorDistance < MAX_DISTANCE_CM) {
// Only the second sensor detects a car
greenDuration = NORMAL_GREEN_DURATION;
} else {
// No sensor detects a car
greenDuration = NORMAL_GREEN_DURATION;
}
// Set the lights according to the current state
digitalWrite(RED_LED_PIN[currentRoad], LOW);
digitalWrite(GREEN_LED_PIN[currentRoad], HIGH);
// Update greenTime
greenTime = greenDuration / 1000; // Convert to seconds
LCD.clear();
for (int countdown = greenTime; countdown >= 1; countdown--) {
LCD.print("Road ");
LCD.print(currentRoad + 1);
LCD.setCursor(0,1);
LCD.print(countdown);
delay(1000);
LCD.clear();
}
digitalWrite(GREEN_LED_PIN[currentRoad], LOW);
digitalWrite(YELLOW_LED_PIN[currentRoad], HIGH);
delay(YELLOW_DURATION);
digitalWrite(YELLOW_LED_PIN[currentRoad], LOW);
digitalWrite(RED_LED_PIN[currentRoad], HIGH);
}
}