from machine import Pin
import time
# Pin assignments
button_pin = Pin(15, Pin.IN, Pin.PULL_UP) # Push button
seg_pins = [Pin(i, Pin.OUT) for i in [2, 3, 4, 5, 6, 7, 8]] # 7-segment display pins
# Function to display a number on the 7-segment display
def display_number(num):
# Define the segment states for numbers 0-9
segment_states = [
(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
]
# Turn off all segments first
for pin in seg_pins:
pin.value(0)
# Set the segments for the number
if 0 <= num < len(segment_states):
for i in range(7):
seg_pins[i].value(segment_states[num][i])
# Main loop
count = 0
while True:
if not button_pin.value(): # Button is pressed
count += 1
if count > 9: # Limit to 9
count = 0
display_number(count)
time.sleep(0.1)