from machine import Pin
import time
counter_pin = Pin(4, Pin.IN, Pin.PULL_UP)
counter = 0
def read_counter():
global counter
button_state = counter_pin.value()
if button_state == 0: # Button is pressed (assuming pull-up resistor)
time.sleep(0.05) # Delay for debouncing
if counter_pin.value() == 0: # Check button state again
counter += 1
print("Button pressed. Current counter value:", counter)
else:
print("Button released. Current counter value:", counter)
# Main loop where we periodically check the counter value
while True:
read_counter()
time.sleep(0.2) # Short delay to prevent CPU overload