import machine
import time
# Define the GPIO pins for the traffic light LEDs
RED_LED_PIN = 1 # GP0
YELLOW_LED_PIN = 5 # GP1
GREEN_LED_PIN = 9 # GP2
# 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 light for 5 seconds
# 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) # GREEN light for 5 seconds
# Yellow light (transition)
turn_on_led(YELLOW_LED_PIN)
time.sleep(2) # YELLOW light for 2 seconds