import machine
import time
incButton = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
decButton = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
resButton = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
a = machine.Pin(23, machine.Pin.OUT)
b = machine.Pin(22, machine.Pin.OUT)
c = machine.Pin(21, machine.Pin.OUT)
d = machine.Pin(19, machine.Pin.OUT)
e = machine.Pin(18, machine.Pin.OUT)
f = machine.Pin(5, machine.Pin.OUT)
g = machine.Pin(4, machine.Pin.OUT)
digit_patterns = [
[0, 0, 0, 0, 0, 0, 1], # Digit 0
[1, 0, 0, 1, 1, 1, 1], # Digit 1
[0, 0, 1, 0, 0, 1, 0], # Digit 2
[0, 0, 0, 0, 1, 1, 0], # Digit 3
[1, 0, 0, 1, 1, 0, 0], # Digit 4
[0, 1, 0, 0, 1, 0, 0], # Digit 5
[0, 1, 0, 0, 0, 0, 0], # Digit 6
[0, 0, 0, 1, 1, 1, 1], # Digit 7
[0, 0, 0, 0, 0, 0, 0], # Digit 8
[0, 0, 0, 1, 1, 0, 0] # Digit 9
]
def display_digit(digit):
if digit < 0 or digit > 9:
return # Invalid digit
pattern = digit_patterns[digit]
a.value(pattern[0])
b.value(pattern[1])
c.value(pattern[2])
d.value(pattern[3])
e.value(pattern[4])
f.value(pattern[5])
g.value(pattern[6])
counter = 0
while True:
incState = incButton.value()
decState = decButton.value()
resState = resButton.value()
if incState == 0:
counter += 1
if counter > 9:
counter = 0
time.sleep(0.2) # Debouncing delay
if decState == 0:
counter -= 1
if counter < 0:
counter = 9
time.sleep(0.2) # Debouncing delay
if resState == 0:
counter = 0
time.sleep(0.2) # Debouncing delay
display_digit(counter)