import time
import board
from digitalio import DigitalInOut, Direction, Pull
from cd4511 import CD4511
# Shared BCD pins
bcd_tens = [board.GP2, board.GP3, board.GP4, board.GP5]
bcd_ones = [board.GP6, board.GP7, board.GP8, board.GP9]
# Create digit drivers
tens_display = CD4511(bcd_tens, board.GP0)
ones_display = CD4511(bcd_ones, board.GP1)
# --- Setup for Buttons ---
btn_up = DigitalInOut(board.GP14)
btn_up.direction = Direction.INPUT
btn_up.pull = Pull.UP
btn_down = DigitalInOut(board.GP15)
btn_down.direction = Direction.INPUT
btn_down.pull = Pull.UP
count = 0
def update_display(val):
tens = val // 10
ones = val % 10
tens_display.display(tens)
ones_display.display(ones)
update_display(count)
while True:
# --- Count Up Toggle ---
if not btn_up.value:
time.sleep(0.2) # Debounce press
while not btn_up.value: pass # Wait for button release
while count < 99:
count += 1
update_display(count)
# Check if button is pressed again to stop
if not btn_up.value:
time.sleep(0.2) # Debounce
break
time.sleep(0.5)
# --- Count Down Toggle ---
if not btn_down.value:
time.sleep(0.2) # Debounce press
while not btn_down.value: pass # Wait for button release
while count > 0:
count -= 1
update_display(count)
# Check if button is pressed again to stop
if not btn_down.value:
time.sleep(0.2) # Debounce
break
time.sleep(0.5)