import time
from machine import Pin
drukknopPin = 18
ledPin = 20
Controledrukknop = Pin(drukknopPin, Pin.IN, Pin.PULL_DOWN)
rodeLed = Pin(ledPin, Pin.OUT)
rodeLed.off()
teller = 0
laatste_change_ms = 0
DEBOUNCE_MS = 40 # 30..80ms is typisch ok
def interruptroutine(pin):
global teller, laatste_change_ms
nu = time.ticks_ms()
if time.ticks_diff(nu, laatste_change_ms) < DEBOUNCE_MS:
return
laatste_change_ms = nu
toestand = pin.value() # 0 = los, 1 = ingedrukt (pull-down)
rodeLed.value(toestand) # LED volgt toestand
if toestand == 1: # alleen tellen bij echte "indruk"
teller += 1
print("ingedrukt", teller)
Controledrukknop.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=interruptroutine)
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
rodeLed.off()