Page 3 of 3

Re: Debouncing with IRQ

Posted: Sun Mar 04, 2018 3:07 pm
by dubaleeiro
Now I see... thank you very much !

what would be the best way to break the while True loop throught this interrupt? Based on the Pin.value() or does the DebouncedSwitch object has some debounced Pin state?

Thanks a lot for you patience and guidance :D

Re: Debouncing with IRQ

Posted: Sun Mar 04, 2018 6:39 pm
by SpotlightKid
Several possibilities:

1. Set a global flag variable in the callback function and check for this in the conditional of a while loop:

Code: Select all

flag = False

def cb(dummy):
    global flag
    flag = True

sw = DebouncedSwitch(pin, cb, "dummy")

while not flag:
    do_something()

flag = False
2. Use a mutable for the callback arg and change that within the callback:

Code: Select all

flag = [False]

def cb(flag):
    flag[0] = True

sw = DebouncedSwitch(pin, cb, flag)

while not flag[0]:
    do_something()
3. Or the same principle using a class as the callback arg and setting/checking an attribute on it.

Re: Debouncing with IRQ

Posted: Tue Oct 30, 2018 1:49 am
by alexholman
Any suggestions for adapting this code to work from the analog input with a threshold?

On a NodeMCU I've gotten it working a simple push button, but I'd like to adapt it to trigger from a piezo sensor. Wiring it the piezo to the input pin works, but requires hammering on the piezo, and I know from experience that it can be way more sensitive wired to an analog input with a trigger threshold set. Reading the ADC with machine.ADC(0) is easy enough, but when I try to pass the resulting object to DebounceSwitch I get an error for missing .irq method. I see that the incoming sw object needs a 'callback' method or defaults to .irq. Any suggestions for what my callback for an ADC object would be and how to set it?

Thanks

Re: Debouncing with IRQ

Posted: Mon Mar 30, 2020 7:05 pm
by Armen
dhylands wrote:
Fri May 20, 2016 10:12 pm
To debounce with an IRQ, the normal flow would go something like this:

1 - Setup pin to generate IRQ on an approriate edge
2 - In IRQ handler, disable the IRQ and start a debounce timer
3 - When the timer expires, examine the state of the pin, re-enable the edge IRQ and then do whatever callbacks are required.

You want to disable the IRQ because one switch transition will often generate multiple edges.

The disadvantge of the IRQ method is that the callback is executed in IRQ context.

I often have a main loop that runs on regular intervals (i.e. every 10 msec) so I tend to use the count method. The counting method could introduce some latency (up to 10 msec if you're running in realtime, more if you're not), but I've never really needed the "instant" reaction that requires an interrupt to be used. I'm happy to implement it when needed, but it adds considerable complexity, which means more potential failure points in your code, etc.
Hello MicroPython lover
can you help me for debounce problem with Esp8622 (ESP-01)
I try test push key with
p0.irq(trigger=Pin.IRQ_FALLING, handler=callback)
http://docs.micropython.org/en/v1.9.3/e ... /pins.html
it's several call : print('pin change', p)
how can edit IRQ handler and disable IRQ?
Thanks for your step by step help
Armen

Re: Debouncing with IRQ

Posted: Tue Mar 31, 2020 1:29 am
by jimmo
Armen wrote:
Mon Mar 30, 2020 7:05 pm
how can edit IRQ handler and disable IRQ?
p0.irq(handler=None)

Re: Debouncing with IRQ

Posted: Tue Mar 31, 2020 4:28 pm
by Armen
jimmo wrote:
Tue Mar 31, 2020 1:29 am
Armen wrote:
Mon Mar 30, 2020 7:05 pm
how can edit IRQ handler and disable IRQ?
p0.irq(handler=None)
Thank for reply
but how can put it inside of handler?

(special thanks from dhylands for his useful posts)
Who can help me for change IRQ handler to resolve this problem in this example
Please Continue to ESP8622: viewtopic.php?f=16&t=8074
Thanks Armen