import machine
import time
class BotonDebounce:
def __init__(self, pin_num, callback= None):
self.pin = machine.Pin(pin_num, machine.Pin.IN, machine.Pin.PULL_UP)
self.callback = callback
self.ultimo_tiempo = 0
self.debounce_tiempo = 200
self.estado_anterior = True
self.pin.irq(trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING,
handler=self._isr_boton)
def _isr_boton(self, pin):
tiempo_actual = time.ticks_ms()
if time.ticks_diff(tiempo_actual , self.ultimo_tiempo) > self.debounce_tiempo:
estado_actual = self.pin.value()
if self.estado_anterior and not estado_actual:
if self.callback:
self.callback()
self.estado_anterior = estado_actual
self.ultimo_tiempo = tiempo_actual
def boton_presionado ():
print ("Boton presionado correctamente!")
boton = BotonDebounce(0, boton_presionado)
while True:
print("Programa ejecutandose...")
time.sleep(1)