From Normally open to Normally closed.

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

From Normally open to Normally closed.

Post by mevero » Tue Jan 04, 2022 11:23 am

In my new project I want to use the same program about 2 buttons only now "when you pursed the button it goes from closed to open".
The program is ( in code) is attached "Voor NC.jpg.
Attachments
Voor NC.jpg
Voor NC.jpg (101.25 KiB) Viewed 11284 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: From Normally open to Normally closed.

Post by pythoncoder » Tue Jan 04, 2022 1:57 pm

There is a lot wrong with the way you're doing this. Firstly the debounce routine will terminate far more quickly than the bouncing. Secondly, in practice the ISR will be called multiple times, once on each bounce. Interrupts are best used for very fast signals: by contrast pushbuttons only need to respond relatively slowly.

The best way to deal with contact bounce is with uasyncio - official docs. See also this tutorial. The follwing doc describes a Pushbutton class which works with normally open or normally closed buttons connected to V+ or Gnd.
Peter Hinch
Index to my micropython libraries.

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

Re: From Normally open to Normally closed.

Post by mevero » Fri Jan 07, 2022 3:28 pm

Please would you be so kind to write the compete code, my skills are not so good to find-out to program it myself.
Thanks for your help.
The code at this moment print more times "wit" en "Zwart".

from machine import Pin
import uasyncio

led = Pin(5, Pin.OUT)
but1 = Pin(4, Pin.IN, Pin.PULL_UP)
but2 = Pin(14, Pin.IN, Pin.PULL_UP)


#uasyncio.run(main (but1), (but2))

def debounce(pin):
prev = None
for _ in range(350):
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 d:
led.value(not led.value())
print('wit')

def but2_callback(pin):
d = debounce(pin)

if d == None:
return
elif d:
led.value(not led.value())
print('Zwart')

but1.irq(trigger=Pin.IRQ_RISING, handler=but1_callback)
but2.irq(trigger=Pin.IRQ_RISING, handler=but2_callback)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: From Normally open to Normally closed.

Post by pythoncoder » Sat Jan 08, 2022 9:58 am

This should toggle the LED each time the button is pressed. It will work with a normally open or normally closed button provided that, when the code starts, the button is not pressed.

You will need to copy the primitives directory with contents from here to your device.

Code: Select all

from machine import Pin
from primitives.pushbutton import Pushbutton
import uasyncio as asyncio

# Toggle an LED (callback)
def toggle(pin):
    pin(not pin())

async def main():
    pin = Pin(4, Pin.IN, Pin.PULL_UP)
    led = Pin(5, Pin.OUT)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (led,))  # Define callback when button pressed
    while True:
        await asyncio.sleep_ms(100)  # Can run any asynchronous code here

try:
    asyncio.run(main())
finally:
    asyncio.new_event_loop()
I suggest you read the primitives doc and uasyncio tutorial.
Peter Hinch
Index to my micropython libraries.

Post Reply