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.
From Normally open to Normally closed.
From Normally open to Normally closed.
- Attachments
-
- Voor NC.jpg (101.25 KiB) Viewed 11287 times
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: From Normally open to Normally closed.
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.
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.
Index to my micropython libraries.
Re: From Normally open to Normally closed.
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)
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: From Normally open to Normally closed.
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.
I suggest you read the primitives doc and uasyncio tutorial.
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()
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.