// Define the pins for each traffic light
int trafficLight1[] = {4, 5, 6}; // {Red, Yellow, Green}
int trafficLight2[] = {7, 8, 9}; // {Red, Yellow, Green}
int trafficLight3[] = {A0, A1, A2}; // {Red, Yellow, Green}
int trafficLight4[] = {A3, A4, A5}; // {Red, Yellow, Green}
// Timings in milliseconds
const int yellowTime = 2000; // Yellow light duration
const int Time = 5000; // light duration for one cycle
// Ultrasonic sensor pins
const int trigPin1 = 3; // Trigger pin1
const int echoPin1 = 2; // Echo pin1
const int trigPin2 = 11; // Trigger pin2
const int echoPin2 = 10; // Echo pin2
long duration ;
int distance1, distance2;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set the trigger pin as an output and the echo pin as an input
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
// Initialize each pin as an output
for (int i = 0; i < 3; i++) {
pinMode(trafficLight1[i], OUTPUT);
pinMode(trafficLight2[i], OUTPUT);
pinMode(trafficLight3[i], OUTPUT);
pinMode(trafficLight4[i], OUTPUT);
}
}
void loop() {
// Measure distance using the ultrasonic sensor
measureDistance1();
measureDistance2();
// Print the distance on the serial monitor
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.println(" cm");
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.println(" cm");
if ((distance1 > 5 && distance2 > 5) || (distance1 <= 5 && distance2 <= 5)) {
// Traffic light 1 green, others red
setTrafficLight(trafficLight1, LOW, LOW, HIGH); // Green
setTrafficLight(trafficLight2, LOW, LOW, HIGH); // Green
setTrafficLight(trafficLight3, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight4, HIGH, LOW, LOW); // Red
delay(Time);
// Traffic light 1 yellow, others red
setTrafficLight(trafficLight1, LOW, HIGH, LOW); // Yellow
setTrafficLight(trafficLight2, LOW, HIGH, LOW); // Yellow
setTrafficLight(trafficLight3, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight4, HIGH, LOW, LOW); // Red
delay(yellowTime);
// Traffic light 1 red,
setTrafficLight(trafficLight1, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight2, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight3, LOW, LOW, HIGH); // GREEN
setTrafficLight(trafficLight4, LOW, LOW, HIGH); // GREEN
delay(Time);
// Traffic light 2 red, traffic light 3 green
setTrafficLight(trafficLight1, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight2, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight3, LOW, HIGH, LOW); // Yellow
setTrafficLight(trafficLight4, LOW, HIGH, LOW); // Yellow
delay(yellowTime);
}
if ((distance1 <= 5 && distance2 > 5))
{
// If a vehicle is detected (distance <= 5 cm), all lights stay red except traffic light 1
setTrafficLight(trafficLight1, LOW, LOW, HIGH); // Green
setTrafficLight(trafficLight2, LOW, LOW, HIGH); // Green
setTrafficLight(trafficLight3, HIGH, LOW, LOW); // Red
setTrafficLight(trafficLight4, HIGH, LOW, LOW); // Red
}
if ((distance1 > 5 && distance2 <= 5))
{
// If a vehicle is detected (distance <= 5 cm), all lights stay red except traffic light 1
setTrafficLight(trafficLight1, HIGH, LOW, LOW); // red
setTrafficLight(trafficLight2, HIGH, LOW, LOW); // red
setTrafficLight(trafficLight3, LOW, LOW, HIGH); // Green
setTrafficLight(trafficLight4, LOW, LOW, HIGH); // Green
}
}
void setTrafficLight(int light[], int red, int yellow, int green) {
digitalWrite(light[0], red);
digitalWrite(light[1], yellow);
digitalWrite(light[2], green);
}
void measureDistance1() {
// Clear the trigger pin
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 10 microseconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Read the echo pin and calculate the duration of the pulse
duration = pulseIn(echoPin1, HIGH);
// Calculate the distance in cm
distance1 = duration * 0.034 / 2;
}
void measureDistance2() {
// Clear the trigger pin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Set the trigger pin HIGH for 10 microseconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Read the echo pin and calculate the duration of the pulse
duration = pulseIn(echoPin2, HIGH);
// Calculate the distance in cm
distance2 = duration * 0.034 / 2;
}