You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
885 B
Python
42 lines
885 B
Python
import time
|
|
from machine import Timer, Pin
|
|
|
|
|
|
def debounce(pin, callback, delay_ms=50):
|
|
def _debounce_handler(timer):
|
|
if pin.value() == 0: # Assuming active low button
|
|
callback()
|
|
|
|
debounce_timer = Timer(-1)
|
|
|
|
def _pin_handler(pin):
|
|
debounce_timer.init(
|
|
mode=Timer.ONE_SHOT, period=delay_ms, callback=_debounce_handler
|
|
)
|
|
|
|
return _pin_handler
|
|
|
|
|
|
counter = 0
|
|
|
|
|
|
def button_pressed():
|
|
print("Button pressed!")
|
|
counter = 0
|
|
|
|
|
|
button_pin = Pin(14, Pin.IN, Pin.PULL_UP)
|
|
button_handler = debounce(button_pin, button_pressed)
|
|
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
|
|
|
|
|
|
while True:
|
|
if counter <= 10:
|
|
print("Press the button")
|
|
elif counter > 10 and counter < 20:
|
|
print("Come on....Press the button.")
|
|
else:
|
|
print("PRESS THE BUTTON!")
|
|
time.sleep(5)
|
|
counter += 1
|