Two buttons with bounce nearly work but...
Posted: Wed Dec 15, 2021 9:36 am
Found this on the internet for one button it works fine file starts. So i duplicate the lines, running gives NO error but only one button works. Program starts with exec(open('doorbell.py').read())
On both entrance when I change the buttons. This is the program: Do you know a solution ??
from machine import Pin
led = Pin(5, Pin.OUT)
but1 = Pin(4, Pin.IN, Pin.PULL_UP)
but2 = Pin(14, Pin.IN, Pin.PULL_UP)
def debounce(pin):
prev = None
for _ in range(32):
current_value = pin.value()
if prev != None and prev != current_value:
return None
prev = current_value
return prev
def but1_callback(pin):
d = debounce(pin)
if d == None:
return
elif not d:
led.value(not led.value())
print('WIT')
def but2_callback(pin):
d = debounce(pin)
if d == None:
return
elif not d:
led.value(not led.value())
print('zwart')
but1.irq(trigger=Pin.IRQ_FALLING, handler=but1_callback)
but2.irq(trigger=Pin.IRQ_FALLING, handler=but2_callback)
On both entrance when I change the buttons. This is the program: Do you know a solution ??
from machine import Pin
led = Pin(5, Pin.OUT)
but1 = Pin(4, Pin.IN, Pin.PULL_UP)
but2 = Pin(14, Pin.IN, Pin.PULL_UP)
def debounce(pin):
prev = None
for _ in range(32):
current_value = pin.value()
if prev != None and prev != current_value:
return None
prev = current_value
return prev
def but1_callback(pin):
d = debounce(pin)
if d == None:
return
elif not d:
led.value(not led.value())
print('WIT')
def but2_callback(pin):
d = debounce(pin)
if d == None:
return
elif not d:
led.value(not led.value())
print('zwart')
but1.irq(trigger=Pin.IRQ_FALLING, handler=but1_callback)
but2.irq(trigger=Pin.IRQ_FALLING, handler=but2_callback)