// Define pin numbers for ultrasonic sensors
const int trigPins[] = {11, 24, 42};
const int echoPins[] = {12, 23, 41};
// Define pin numbers for PIR sensors
const int pirPins[] = {13, 22, 40};
// Define pin numbers for traffic lights
const int trafficRedPins[] = {10, 25, 43};
const int trafficGreenPins[] = {9, 26, 44};
const int trafficYellowPins[] = {8, 27, 45};
// Define pin numbers for pedestrian lights
const int pedestrianGreenPins[] = {6, 29, 47};
const int pedestrianRedPins[] = {7, 28, 46};
// Timing constants
const unsigned long greenTime = 10000; // 10 seconds
const unsigned long yellowTime = 2000; // 2 seconds
const unsigned long pedestrianCrossTime = 8000; // 8 seconds
void setup() {
// Initialize pins
for (int i = 0; i < 3; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
pinMode(pirPins[i], INPUT);
pinMode(trafficRedPins[i], OUTPUT);
pinMode(trafficYellowPins[i], OUTPUT);
pinMode(trafficGreenPins[i], OUTPUT);
pinMode(pedestrianGreenPins[i], OUTPUT);
pinMode(pedestrianRedPins[i], OUTPUT);
}
// Start with default lights (all red for cars, all red for pedestrians)
for (int i = 0; i < 3; i++) {
digitalWrite(trafficRedPins[i], HIGH);
digitalWrite(pedestrianRedPins[i], HIGH);
}
}
void loop() {
// Check for vehicles and pedestrians in each direction
for (int i = 0; i < 3; i++) {
bool vehicleDetected = isVehicleDetected(i);
if (vehicleDetected) {
controlTrafficLights(i);
} else {
bool pedestrianDetected = isPedestrianDetected(i);
if (pedestrianDetected) {
controlPedestrianLights(i);
} else {
defaultTrafficLightState(i);
}
}
}
delay(1000); // Short delay to avoid rapid switching
}
bool isVehicleDetected(int index) {
// Send a pulse to the ultrasonic sensor
digitalWrite(trigPins[index], LOW);
delayMicroseconds(2);
digitalWrite(trigPins[index], HIGH);
delayMicroseconds(10);
digitalWrite(trigPins[index], LOW);
// Read the pulse duration
long duration = pulseIn(echoPins[index], HIGH);
// Calculate the distance
float distance = duration * 0.034 / 2;
// Check if the distance is within the threshold (e.g., 30 cm)
return (distance < 30.0);
}
bool isPedestrianDetected(int index) {
// Read the PIR sensor, only detect if no vehicle is present
return digitalRead(pirPins[index]) == HIGH;
}
void controlTrafficLights(int index) {
// Vehicle detected sequence
digitalWrite(trafficGreenPins[index], HIGH);
digitalWrite(trafficYellowPins[index], LOW);
digitalWrite(trafficRedPins[index], LOW);
digitalWrite(pedestrianRedPins[index], HIGH);
digitalWrite(pedestrianGreenPins[index], LOW);
delay(greenTime);
digitalWrite(trafficGreenPins[index], LOW);
digitalWrite(trafficYellowPins[index], HIGH);
delay(yellowTime);
digitalWrite(trafficYellowPins[index], LOW);
digitalWrite(trafficRedPins[index], HIGH);
}
void controlPedestrianLights(int index) {
// Pedestrian detected sequence
digitalWrite(trafficRedPins[index], HIGH);
digitalWrite(trafficGreenPins[index], LOW);
digitalWrite(trafficYellowPins[index], LOW);
digitalWrite(pedestrianRedPins[index], LOW);
digitalWrite(pedestrianGreenPins[index], HIGH);
delay(pedestrianCrossTime);
digitalWrite(pedestrianGreenPins[index], LOW);
digitalWrite(pedestrianRedPins[index], HIGH);
}
void defaultTrafficLightState(int index) {
// Default state (no vehicle or pedestrian detected)
digitalWrite(trafficRedPins[index], HIGH);
digitalWrite(trafficGreenPins[index], LOW);
digitalWrite(trafficYellowPins[index], LOW);
digitalWrite(pedestrianRedPins[index], HIGH);
digitalWrite(pedestrianGreenPins[index], LOW);
}