from machine import Pin
import utime
# Define the segments for the 7-segment display
segments = [Pin(i, Pin.OUT) for i in range(7)]
# Define the digit patterns for 0-9
digit_patterns = [
[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, 0, 0, 1, 1] # 9
]
# Function to display a digit on the 7-segment display
def display_digit(digit):
pattern = digit_patterns[digit]
for segment, value in zip(segments, pattern):
segment.value(value)
# Define the buttons
button_increment = Pin(14, Pin.IN, Pin.PULL_DOWN)
button_reset = Pin(15, Pin.IN, Pin.PULL_DOWN)
# Initialize click count
click_count = 0
# Debounce delay
debounce_delay = 200
while True:
# Check if the increment button is pressed
if button_increment.value() == 1:
click_count += 1
if click_count > 9:
click_count = 0
display_digit(click_count)
utime.sleep_ms(debounce_delay) # Debounce delay
# Check if the reset button is pressed
if button_reset.value() == 1:
click_count = 0
display_digit(click_count)
utime.sleep_ms(debounce_delay) # Debounce delay
utime.sleep_ms(10) # Short delay to avoid busy-waiting