from machine import Pin
import time
switch_up_pin = 5
switch_down_pin = 4
segments = [12, 14, 27, 26, 25, 33, 32]
common_anode_pin = 13
digit_patterns = [
[0,0,0,0,0,0,1], #0
[1,0,0,1,1,1,1], #1
[0,0,1,0,0,1,0], #2
[0,0,0,0,1,1,0], #3
[1,0,0,1,1,0,0], #4
[0,1,0,0,1,0,0], #5
[0,1,0,0,0,0,0], #6
[0,0,0,1,1,1,1], #7
[0,0,0,0,0,0,0], #8
[0,0,0,0,1,0,0] #9
]
# Initialize pins
switch_up = Pin(switch_up_pin, Pin.IN, Pin.PULL_UP)
switch_down = Pin(switch_down_pin, Pin.IN, Pin.PULL_UP)
common_anode = Pin(common_anode_pin, Pin.OUT)
for segment_pin in segments:
Pin(segment_pin, Pin.OUT).off()
# Function to display a digit on the seven segment display
def display_digit(digit):
for i, segment_pin in enumerate(segments):
Pin(segment_pin, Pin.OUT).value(digit_patterns[digit][i])
# Function to increment the counter
def increment_counter():
global counter
counter = (counter + 1) % 10
# Function to decrement the counter
def decrement_counter():
global counter
counter = (counter - 1) % 10
# Initialize counter
counter = 0
# Main loop
while True:
if not switch_up.value():
increment_counter()
time.sleep(0.2)
if not switch_down.value():
decrement_counter()
time.sleep(0.2)
# Display current count
common_anode.on() # Common anode is active low
display_digit(counter)
time.sleep(0.01)
common_anode.off() # Turn off the display to avoid ghosting