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)
Two buttons with bounce nearly work but...
Re: Two buttons with bounce nearly work but...
Not commenting on your code, reposting it with code tags so it's legible:
Code: Select all
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)
Re: Two buttons with bounce nearly work but...
Thanks for your answer, I hope this is what your asking for ??
I saw that on line it looks not as I put it there.
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)
I saw that on line it looks not as I put it there.
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)
- Attachments
-
- Button with layout.jpg (189 KiB) Viewed 8366 times
Problem solved !
May the button ore a wire was not good. IT WORKS NOW !!
I can make now a lot of things Thinking of An alarm system
Thanks for the help !!
I can make now a lot of things Thinking of An alarm system
Thanks for the help !!