from machine import Pin, Timer # Import necessary classes from the machine module
import time # Import the time module
# Initialize global variables
button = Pin(32, Pin.IN, Pin.PULL_UP) # Initialize pin 32 as a pull-up input
counter = 0 # Counter to keep track of button presses
irq_isenable = True # Flag to indicate whether IRQ is enabled
def isr(pin): # ISR function to handle button presses
global counter, irq_isenable # Access global variables
if irq_isenable: # Check if IRQ is enabled
print(f'Button pressed, IRQ disabled, starting timer')
counter += 1 # Increment the counter
print('Button pressed count:', counter)
timer.init(mode=Timer.ONE_SHOT, period=200, callback=enable_irq) # Start a one-shot timer
print(f'Timer start {timer}')
irq_isenable = False # Disable IRQ
def enable_irq(timer): # Function to re-enable IRQ
global irq_isenable # Access global variable
irq_isenable = True # Re-enable IRQ
print('Timer expired, IRQ re-enabled')
# Attach ISR
button.irq(trigger=Pin.IRQ_FALLING, handler=isr) # Attach ISR to handle falling edge triggers
# Set up the one-shot timer
timer = Timer(0) # Initialize a Timer object
print("Program Start") # Print program start message
# Run an infinite loop to keep the program running
while True:
time.sleep(1) # Sleep to reduce CPU usage