from machine import Pin
import time
# Define button pins
button_pins = [32, 33, 4] # Example GPIO pins
# Dictionary to store button states
button_states = {pin: False for pin in button_pins}
# Interrupt handler function
def button_handler(pin):
global button_states
button_states[pin] = not button_states[pin]
print(f"Button on pin {pin} {'pressed' if button_states[pin] else 'released'}")
# Initialize buttons and set up interrupts
buttons = []
for pin in button_pins:
button = Pin(pin, Pin.IN, Pin.PULL_UP)
button.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=lambda p: button_handler(p))
buttons.append(button)
# Main loop
try:
while True:
time.sleep(1) # Sleep to reduce CPU usage
except KeyboardInterrupt:
print("Program stopped")