import machine
import utime
# Define GPIO pins for common and LED pins of the two seven-segment displays
common_pins = [machine.Pin(pin, machine.Pin.OUT) for pin in [14, 15]]
led_pins = [machine.Pin(pin, machine.Pin.OUT) for pin in [0, 1, 2, 3, 4, 5, 6]]
# Define GPIO pins for the buttons
button1_pin = machine.Pin(26, machine.Pin.IN)
button2_pin = machine.Pin(27, machine.Pin.IN)
# Initialize variables
button1_state = button1_pin.value()
button2_state = button2_pin.value()
counter = 0
# Define seven-segment display patterns for numbers 0 to 9 for both digits
segment_patterns = [
# Patterns for the first digit
[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
# Patterns for the second digit
[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
]
# Function to perform software debounce
def debounce(pin):
state = pin.value()
utime.sleep_ms(5) # Adjust the delay based on your requirements
return pin.value() if pin.value() == state else state
# Function to display a digit on a specific display
def display_digit(digit, display_index):
for i, pin in enumerate(led_pins):
pin.value(segment_patterns[digit][i])
# Turn on the common pin for the selected display
common_pins[display_index].value(1)
# Optional: Add a small delay to avoid rapid repeated readings
utime.sleep_ms(1) # Adjust the delay based on your requirements
# Turn off the common pin for the selected display
common_pins[display_index].value(0)
while True:
# Debounce button states
button1_state_new = debounce(button1_pin)
button2_state_new = debounce(button2_pin)
# Check for button 1 press event
if button1_state == 0 and button1_state_new == 1:
counter = (counter + 1) % 100 # Ensure counter stays in the range 0 to 99
# Check for button 2 press event
if button2_state == 0 and button2_state_new == 1:
counter = (counter - 1) % 100 # Ensure counter stays in the range 0 to 99
# Check if both buttons are pressed, reset counter
if button1_state_new == 1 and button2_state_new == 1:
counter = 0
# Update button states
button1_state = button1_state_new
button2_state = button2_state_new
# Update seven-segment displays using time multiplexing
digit1 = counter // 10
digit2 = counter % 10
# Display the first digit
display_digit(digit1, 0)
# Display the second digit
display_digit(digit2, 1)
# Optional: Add a small delay to avoid rapid repeated readings
utime.sleep_ms(1)