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
Debouncing with IRQ
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Debouncing with IRQ
Several possibilities:
1. Set a global flag variable in the callback function and check for this in the conditional of a while loop:
2. Use a mutable for the callback arg and change that within the callback:
3. Or the same principle using a class as the callback arg and setting/checking an attribute on it.
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
Code: Select all
flag = [False]
def cb(flag):
flag[0] = True
sw = DebouncedSwitch(pin, cb, flag)
while not flag[0]:
do_something()
-
- Posts: 1
- Joined: Tue Oct 30, 2018 1:36 am
Re: Debouncing with IRQ
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
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
Hello MicroPython loverdhylands wrote: ↑Fri May 20, 2016 10:12 pmTo 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.
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
p0.irq(handler=None)
Re: Debouncing with IRQ
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