import time #Import time library for delays (timing control)
import board #Access Raspberry Pi Pico GPIO pins
import digitalio #Control digital input/output pins
# -------------------
#SET PARAMETERS
#-----------------
y=60 #Maximum count value (stopwatch resets after 60)
interval = 0.2 # Time delay between counts (speed of counting)
#------------------
#BUTTON INPUTS
#-------------------
sw1=digitallo.DigitalInout (board.GP15) #Start button (SNI)
swl.direction = digitalio. Direction.INPUT #Set as INPUT
sw1.pull = digitalio.Pull.UP #Enable pull-up (pressed = LOW)
sw2 = digitalio.Digitalinout (board.GP16) #Reset button (SN2)
sw2.direction = digitalio. Direction. INPUT #Set as INPUT
sw2.pull = digitalio. Pull.UP #Enable pull-up (pressed LOW)
#--------------
#SHARED BCD DATA LINES (DO-D3)
#--------------
A = digitalio.DigitalinOut(board.GP2) #BCD bit #MSB
B = digitalio.DigitalInOut(board.GP3) #BCD bit 1
C = digitalio.DigitalInOut(board.GP4) #BCD bit 2
D = digitalio. DigitalInOut(board.GP5) #BCD bit 3 #LSB
#Set all BCD pins as OUTPUT
for pin in (A, B, C, DJ:
pin.direction digitalio.Direction.OUTPUT
bcd pins [A, B, C, D] # Store BCD pins for Beasy looping
#--------------------
#LATCH ENABLE PINS (CD4511 control)
#--------------------
s72 = digitalio DigitalInOut(board. GP0) # Controls ones display
s72.direction = digitalio. Direction OUTPUT
s71 = digitalio Digitalinout (board.GP1) # Controls TENS display
s71.direction = digitalio. Direction.OUTPUT
#----------------
#BCD FUNCTIONS
#----------------
def send_bcd(value):
#Send 4-bit binary value to CD4511 decoder
for i in range(4):
bcd_pins[i].value = (value >> 1) & 1 # Extract cach bit
def latch(pin):
#Send pulse to latch data into 7-segnent display
pin.value=0 #Enable latch (active low)
time.sleep(0.001) #Small delay for hardware stability
pin.value = 1 #Disable latch
def display_number(num):
#Split number Into tens and ones digits
tens = num //10
ones = num % 10
#Send tens digit to left display
send_bcd (tens)
latch (s72)
#Send ones digit to right display
send_bcd(ones)
latch (s71)
def display_off():
#Turn off all display segments
for pin in bed_pins:
pin.value = O #Clear ECD inputs
#Turn OFF both displays
latch (s72)
latch(s71)
#-------------------
#HAIN VARIABLES
#-------------------
count = 0 #Current stopwatch value
running = false #State of stopwatch (False stopped, True rumming)
#Initial display OFF
display_off()
#-------------------
#MAIN LOOP
#-------------------
while True:
#Always update display with current count
display_number(count)
#------------------
#START BUTTON (SW1)
#-------------------
if not swl.value: #Button pressed (LON signal)
running = True #Start counting
print("START STOPWATCH")
time.sleep(0.3) # Debounce delay
#----------------------
#RESET BUTTON (SW2)
#-----------------------
if not sw2.value:#Button pressed
running False # Stop counting
count = 0 #Reset value to e
print("RESET -> 00")
time.sleep(0.3)# Debounce delay
#-----------------
#COUNTING PROCESS
#-----------------
if running: #Only count when stopwatch is running
time.sleep(interval) #Wait before increasing count
count += 1 #Increase count by 1
#--------------------
MAX VALUE CONDITION
#--------------------
if count > y: #If count exceeds max value
count =0 #Reset back to D
print("MAX reached -> RESET")
#Display formatted output (always 2 digits)
print("Time: {:02d}", format(count))