YELLOW_LED_PIN = 5 # pin for yellow LED
GREEN_LED_PIN = 9 # pin for green LED
# Initialize the GPIO pins for the LEDs
red_led = machine.Pin(RED_LED_PIN, machine.Pin.OUT)
yellow_led = machine.Pin(YELLOW_LED_PIN, machine.Pin.OUT)
green_led = machine.Pin(GREEN_LED_PIN, machine.Pin.OUT)
# Function to turn on the specified LED and turn off the others
def turn_on_led(led_pin):
red_led.value(led_pin == RED_LED_PIN)
yellow_led.value(led_pin == YELLOW_LED_PIN)
green_led.value(led_pin == GREEN_LED_PIN)
# Traffic light sequence
while True:
# Red Light
turn_on_led(RED_LED_PIN)
time.sleep(5)
# Red and yellow light (transition)
turn_on_led(YELLOW_LED_PIN)
time.sleep(2) # Red and yellow light for 2 seconds
# Green Light
turn_on_led(GREEN_LED_PIN)
time.sleep(5)
# Yellow Light
turn_on_led(YELLOW_LED_PIN)
time.sleep(2) # Yellow light for 2 seconds