import machine as mc
from utime import sleep, sleep_ms
print("Hello, Pi Pico!")
push_bt = mc.Pin(16, mc.Pin.IN, mc.Pin.PULL_UP)
led = mc.Pin(15, mc.Pin.OUT)
# global value
button_pressed = False
def wait_pin_change(pin):
# wait for pin to change value
# it needs to be stable for a continuous 20ms
cur_value = pin.value()
active = 0
while active < 5:
if pin.value() != cur_value:
active += 1
else:
active = 0
sleep_ms(1)
# Interrupt Service Routine for Button Pressed Events - with no debounce
def button_pressed(change):
global button_pressed
button_pressed = True
# here is how we associate the falling value on the input pin with the callback function
push_bt.irq(handler=button_pressed, trigger=mc.Pin.IRQ_FALLING)
while True:
if (button_pressed == True):
print("button_pressed")
wait_pin_change(push_bt)
button_pressed = False
if (led.value() == 1):
led.off()
else:
led.on()
sleep(0.2)