from machine import Pin,Timer
import time
# 7pins for digit leds
segment = [Pin(pin_num,Pin.OUT) for pin_num in range(7)]
# 4pins for control which digit
digit_enable = [Pin(pin_num,Pin.OUT) for pin_num in range(7,11)]
# three pins to control the triffic light leds
red = Pin(16,Pin.OUT)
yellow = Pin(17,Pin.OUT)
blue = Pin(18,Pin.OUT)
# timer object
t=Timer()
digits_list = [
[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
]
""" globle varible """
current_display_index = 0
display_value=0
def display(timer_int):
global current_display_index,display_value
# cal the digit for the current_display_index
digit=int((display_value) // (10 ** current_display_index)) % 10
print(digit,current_display_index)
display_digit(digit, current_display_index)
# update the current_display_index
current_display_index +=1
if current_display_index == 4:
current_display_index=0
def display_digit(digit,current_display_index):
# deselect all enble pins
for pin in digit_enable:
pin.value(1)
# show digit on segment
d_list=digits_list[digit]
for i in range(7):
segment[i].value(d_list[i])
# active current digit
digit_enable[current_display_index].value(0)
# enable timer
t.init(period=4 , mode=Timer.PERIODIC , callback=display)
while True:
# turn red on for 5s
for i in range(15,0,-1):
red.value(1)
blue.value(0)
display_value=i
time.sleep(0.25)
# turn yellow on for 3s
for i in range(12,0,-1):
yellow.value(1)
red.value(0)
display_value=i
time.sleep(0.25)
# turn blue on for 7s
for i in range(20,0,-1):
blue.value(1)
yellow.value(0)
display_value=i
time.sleep(0.25)