//Not Working
// Define the pins for the traffic light LEDs
const int redPin = 2; // Red LED
const int yellowPin = 4; // Yellow LED
const int greenPin = 5; // Green LED
// Define the pin for the push button
const int buttonPin = 18;
// Define the state of the traffic light
enum TrafficLightState {
RED,
RED_YELLOW,
GREEN,
YELLOW
};
TrafficLightState currentState = RED;
void setup() {
// Initialize the LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
// Initialize the push button pin as an input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Initial state: Red light is ON
digitalWrite(redPin, HIGH);
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Change the traffic light state
changeTrafficLightState();
delay(1000); // Debounce delay
}
// Update the traffic light based on the current state
updateTrafficLight();
}
void changeTrafficLightState() {
switch (currentState) {
case RED:
currentState = GREEN;
break;
case GREEN:
currentState = YELLOW;
break;
case YELLOW:
currentState = RED;
break;
default:
break;
}
}
void updateTrafficLight() {
// Turn off all LEDs
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
// Update LEDs based on the current state
switch (currentState) {
case RED:
digitalWrite(redPin, HIGH);
break;
case GREEN:
digitalWrite(greenPin, HIGH);
break;
case YELLOW:
digitalWrite(yellowPin, HIGH);
break;
default:
break;
}
}