import machine
import utime
# GPIO pin assignments
BUTTON_A_PIN = 8
BUTTON_B_PIN = 12
# Seven-segment display pin assignments for each digit
increment_button = machine.Pin(12, machine.Pin.IN)
# Define the GPIO pins for the 7-segment display segments
display_pins2 = [6, 7, 18, 19, 20, 5, 4]
display_pins1 = [2, 3, 22, 26, 27, 1, 0]
digits = {
0: [1, 1, 1, 1, 1, 1, 0],
1: [0, 1, 1, 0, 0, 0, 0],
2: [1, 1, 0, 1, 1, 0, 1],
3: [1, 1, 1, 1, 0, 0, 1],
4: [0, 1, 1, 0, 0, 1, 1],
5: [1, 0, 1, 1, 0, 1, 1],
6: [1, 0, 1, 1, 1, 1, 1],
7: [1, 1, 1, 0, 0, 0, 0],
8: [1, 1, 1, 1, 1, 1, 1],
9: [1, 1, 1, 1, 0, 1, 1],
}
# Function to display a digit on the 7-segment display
def display_digit1(digit):
for pin, state in zip(display_pins1, digits[digit]):
machine.Pin(pin, machine.Pin.OUT).value(state)
def display_digit2(digit):
for pin, state in zip(display_pins2, digits[digit]):
machine.Pin(pin, machine.Pin.OUT).value(state)
# Initial digit value
current_digit1 = 0
current_digit2 = 0
display_digit1(current_digit1)
display_digit2(current_digit2)
# Variable to store the previous state of the increment button
prev_increment_button_state = increment_button.value()
# Main loop
while True:
# Check for the rising edge of the increment button
if increment_button.value() == 1 and prev_increment_button_state == 0:
print(increment_button.value())
if current_digit2 == 9:
current_digit1 = (current_digit1 + 1)
current_digit2 = 0
display_digit1(current_digit1)
display_digit2(current_digit2)
utime.sleep(0.2) # Add a short delay for button debounce
elif current_digit1 == 9 and current_digit2 == 9:
current_digit1 = 0
current_digit2 = 0
else:
current_digit2 = (current_digit2 + 1)
display_digit1(current_digit1)
display_digit2(current_digit2)
# Update the previous state of the increment button
prev_increment_button_state = increment_button.value()
# Check if the decrement button is pressed
utime.sleep(0.1) # Adjust the delay based on your needs