import machine
import utime
# Define the GPIO pins for the traffic light LEDs
RED_LED_PIN = 28 # GP28
YELLOW_LED_PIN = 22 # GP22
GREEN_LED_PIN = 18 # GP18
# Define timers for the traffic light LEDs
RED_LED_TIMER = 10 # Red light for 6 seconds
YELLOW_LED_TIMER = 3 # Yellow light for 3 seconds
GREEN_LED_TIMER = 9 # Green light for 5 seconds
#GPIO pins for 7-segment display segments (a-g)
segments = [
machine.Pin(0, machine.Pin.OUT),
machine.Pin(1, machine.Pin.OUT),
machine.Pin(2, machine.Pin.OUT),
machine.Pin(3, machine.Pin.OUT),
machine.Pin(4, machine.Pin.OUT),
machine.Pin(5, machine.Pin.OUT),
machine.Pin(6, machine.Pin.OUT),
]
segments2 = [
machine.Pin(8, machine.Pin.OUT),
machine.Pin(9, machine.Pin.OUT),
machine.Pin(10, machine.Pin.OUT),
machine.Pin(11, machine.Pin.OUT),
machine.Pin(12, machine.Pin.OUT),
machine.Pin(13, machine.Pin.OUT),
machine.Pin(14, machine.Pin.OUT),
]
# 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)
# pin states for each digit to display numbers 0-9
number_map = [
[1, 1, 1, 1, 1, 1, 0], # 0
[0, 1, 1, 0, 0, 0, 0], # 1
[1, 1, 0, 1, 1, 0, 1], # 2
[1, 1, 1, 1, 0, 0, 1], # 3
[0, 1, 1, 0, 0, 1, 1], # 4
[1, 0, 1, 1, 0, 1, 1], # 5
[1, 0, 1, 1, 1, 1, 1], # 6
[1, 1, 1, 0, 0, 0, 0], # 7
[1, 1, 1, 1, 1, 1, 1], # 8
[1, 1, 1, 1, 0, 1, 1] # 9
]
# 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)
#function to display a specific number on the 7-segment display
def display_number(number, segment):
segments_values = number_map[number]
for i in range(len(segment)):
segment[i].value(not segments_values[i])
def led_timer(count_down):
first = True
count_downi = count_down
count_down2 = 0
if count_down > 9:
count_down2 = int(str(count_down)[0:1])
for number in range(count_down2, -1, -1):
display_number(number, segments2)
while count_downi > 9:
count_downi -= 10
if first == False:
count_downi = 9
first = False
for number in range(count_downi, -1, -1):
display_number(number, segments)
utime.sleep(1) # Delay between numbers
while True:
turn_on_led(RED_LED_PIN) # Red light
led_timer(RED_LED_TIMER)
turn_on_led(YELLOW_LED_PIN) # Yellow lights (transition)
led_timer(YELLOW_LED_TIMER)
turn_on_led(GREEN_LED_PIN) # Green light
led_timer(GREEN_LED_TIMER)
turn_on_led(YELLOW_LED_PIN) # Yellow light
led_timer(YELLOW_LED_TIMER)