// Define the pin numbers for the traffic light LEDs
#define RED_PIN 2
#define YELLOW_PIN 3
#define GREEN_PIN 4
// Define the waiting time for each state in milliseconds
#define RED_TIME 20000
#define GREEN_TIME 20000
#define YELLOW_TIME 5000
#define TRANSISION_TIME 150
// Function to control the traffic light
void trafficLight() {
// Set the initial state of the traffic light to red
digitalWrite(RED_PIN, HIGH);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
// Infinite loop to continuously cycle through the traffic light states
while (true) {
// Delay for the specified time in the current state
delay(RED_TIME);
// Change the state from red to green
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
// Delay for the specified time in the current state
delay(TRANSISION_TIME);
digitalWrite(GREEN_PIN, HIGH);
// Delay for the specified time in the current state
delay(GREEN_TIME);
// Change the state from green to yellow
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
// Delay for the specified time in the current state
delay(TRANSISION_TIME);
digitalWrite(YELLOW_PIN, HIGH);
// Delay for the specified time in the current state
delay(YELLOW_TIME);
// Change the state from yellow to red
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
delay(TRANSISION_TIME);
// Delay for the specified time in the current state
digitalWrite(RED_PIN, HIGH);
}
}
void setup() {
// Set the pin modes for the traffic light LEDs
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
}
void loop() {
// Call the trafficLight function to start the traffic light program
trafficLight();
}