import time
import board
import digitalio # CHANGED: import for switch input
from cd4511 import CD4511
# Shared BCD pins (A, B, C, D)
bcd_tens = (board.GP2, board.GP3, board.GP4, board.GP5)
bcd_ones = (board.GP6, board.GP7, board.GP8, board.GP9)
# Create two digit drivers
tens_display = CD4511(bcd_tens, board.GP0)
ones_display = CD4511(bcd_ones, board.GP1)
# =====================================================
# Switch setup (UP / DOWN)
# =====================================================
sw_up = digitalio.DigitalInOut(board.GP15)
sw_up.direction = digitalio.Direction.INPUT
sw_up.pull = digitalio.Pull.UP # active LOW
sw_down = digitalio.DigitalInOut(board.GP16)
sw_down.direction = digitalio.Direction.INPUT
sw_down.pull = digitalio.Pull.UP # active LOW
count = 0
while True:
# =================================================
# Read button states
# =================================================
up_pressed = sw_up.value # active LOW
down_pressed = sw_down.value # active LOW
# =================================================
# CHANGED: UP button (edge detection)
# =================================================
if up_pressed == False :
count = (count + 1) % 100
print("UP →", count)
time.sleep(0.2) # debounce
# =================================================
# CHANGED: DOWN button (edge detection)
# =================================================
if down_pressed == False :
count = (count - 1) % 100
print("DOWN →", count)
time.sleep(0.2) # debounce
# =================================================
# Display update (UNCHANGED)
# =================================================
tens = count // 10
ones = count % 10
tens_display.display(tens)
ones_display.display(ones)
print("Displaying:", count)
time.sleep(0.05)