int redLed = 12; // pin for red LED
int yellowLed = 8; // pin for yellow LED
int greenLed = 7; // pin for green LED
int switchPin = 2; // pin for the switch
int buttonState; // variable to store the button state
void setup() {
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT_PULLUP); // Use internal pull-up resistor
}
void loop() {
buttonState = digitalRead(switchPin);
if (buttonState == HIGH) { // Button not pressed
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
delay(5000); // Wait 5 seconds
} else { // Button pressed
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, HIGH);
digitalWrite(greenLed, LOW);
delay(5000);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, HIGH);
delay(5000);
}
}