import machine
import utime
# Define the GPIO pins for the traffic light LEDs
RED_LED_PIN = 0 # GP 0
YELLOW_LED_PIN = 2 # GP 2
GREEN_LED_PIN = 8 # GP 8
# 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)
utime.sleep(2) # Red light for 2 seconds
# Red and yellow lights (transition)
turn_on_led(YELLOW_LED_PIN)
utime.sleep(1) # Red and yellow lights for 1 seconds
# Green light
turn_on_led(GREEN_LED_PIN)
utime.sleep(2) # Green light for 2 seconds
# Yellow light
turn_on_led(YELLOW_LED_PIN)
utime.sleep(1) # Yellow light for 1 seconds