Two buttons with bounce nearly work but...

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
mevero
Posts: 13
Joined: Mon Aug 30, 2021 12:59 pm

Two buttons with bounce nearly work but...

Post by mevero » 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)

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Two buttons with bounce nearly work but...

Post by scruss » Wed Dec 15, 2021 4:51 pm

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)

mevero
Posts: 13
Joined: Mon Aug 30, 2021 12:59 pm

Re: Two buttons with bounce nearly work but...

Post by mevero » Thu Dec 16, 2021 12:55 pm

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)
Attachments
Button with layout.jpg
Button with layout.jpg (189 KiB) Viewed 8267 times

mevero
Posts: 13
Joined: Mon Aug 30, 2021 12:59 pm

Problem solved !

Post by mevero » Thu Dec 16, 2021 6:31 pm

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 !!

Post Reply