import machine
import utime
#Define Led Pins
red_led = machine.Pin(13, machine.Pin.OUT)
yellow_led = machine.Pin(9, machine.Pin.OUT)
green_led = machine.Pin(5, machine.Pin.OUT)
def red_state():
red_led.value(1)
yellow_led(0)
green_led(0)
def yellow_state():
red_led.value(0)
yellow_led.value(1)
green_led.value(0)
def green_state():
red_led.value(0)
yellow_led.value(0)
green_led.value(1)
# State Handlers
light_state = [
# State & Time
(red_state, 5000),
(yellow_state, 2500),
(green_state, 5000),
]
def traffic_light():
state = 0
while True:
#
current_light_time = light_state[state]
current_light = current_light_time[0]
current_duration_time = current_light_time[1]
current_light()
utime.sleep_ms(current_duration_time)
state = (state + 1) % len(light_state)
traffic_light()