# 6led pattern display
from machine import Pin
import time
directions = {
"north": {"red": 5, "yellow": 2, "green": 4},
"south": {"red": 15, "yellow": 13, "green": 12},
"east": {"red": 22, "yellow": 26, "green": 27},
"west": {"red": 25, "yellow": 32, "green": 33},
}
red_time = 3
yellow_time = 1
green_time = 5
def turn_on_led(direction, color):
for d, pins in directions.items():
for led_color, pin in pins.items():
led = Pin(pin, Pin.OUT)
if d == direction and led_color == color:
led.value(0)
else:
led.value(1)
def traffic_light_sequence():
current_direction_index = 0
directions_list = list(directions.keys())
while True:
current_direction = directions_list[current_direction_index]
# Turn off all lights except for the current direction
for led_color in directions[current_direction]:
turn_on_led(current_direction, led_color)
# Turn on red
turn_on_led(current_direction, "red")
time.sleep(red_time)
# Turn on yellow
turn_on_led(current_direction, "yellow")
time.sleep(yellow_time)
# Turn on green
turn_on_led(current_direction, "green")
time.sleep(green_time)
# Move to the next direction
current_direction_index = (current_direction_index + 1) % len(directions_list)
traffic_light_sequence()