import machine
import utime
# Segments for a-g
segments = [
machine.Pin(0, machine.Pin.OUT), # a
machine.Pin(1, machine.Pin.OUT), # b
machine.Pin(2, machine.Pin.OUT), # c
machine.Pin(3, machine.Pin.OUT), # d
machine.Pin(4, machine.Pin.OUT), # e
machine.Pin(5, machine.Pin.OUT), # f
machine.Pin(6, machine.Pin.OUT) # g
]
# Two digit pins for common-cathode
digit_pins = [
machine.Pin(7, machine.Pin.OUT), # Left digit
machine.Pin(8, machine.Pin.OUT) # Right digit
]
# number_map for common-cathode
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
]
def display_digit(digit_value, digit_index):
# Turn both digits off first
for dp in digit_pins:
dp.value(1) # 1 = off (common-cathode)
# Update segments for the digit
pattern = number_map[digit_value]
for i, seg_pin in enumerate(segments):
seg_pin.value(pattern[i]) # 1 = on, 0 = off, for common-cathode
# Turn on the selected digit (by pulling cathode low)
digit_pins[digit_index].value(0)
while True:
for number in range(100):
start = utime.ticks_ms()
while utime.ticks_ms() - start < 1000: # hold the number for 1 second
tens = number // 10
ones = number % 10
display_digit(tens, 0)
utime.sleep_ms(5)
display_digit(ones, 1)
utime.sleep_ms(5)