LoPy4 Interrupts for button



  • Hi,
    tried the example at https://docs.pycom.io/firmwareapi/pycom/machine/pin#methods

    from machine import Pin
    ​
    def pin_handler(arg):
       print("got an interrupt in pin %s" % (arg.id()))
    ​
    p_in = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
    p_in.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin_handler)
    

    but I get multiple responses. Is there a 'button' function or what's the correct way to debounce an input?

    Also, can I have several buttons?

    TIA Dave



  • @jcaron Yes, I got several each press, hence the query on de-bouncing.

    As for software de-bouncing, it's just that I try not to re-invent the wheel. e.g. I'm sure the micro:bit has a 'button' type for button A and B and since the Exp3 has a user button I guess I expected something from pycom.

    Hardware wise I tried a couple of different capacitors across the pushbutton (relying on the PULLUP) with little success. I may give it some more testing.

    Dave



  • i guess @jcaron have given you the answer.
    but if not. there are some software solutions to debounce inputs. best practice is to do this in hardware so it doesn't cost precious processing power.
    how do you have things wired up?
    and yes you can have more button's you can make a separate handler for them

    you get something like this:

    def pin10_handler(arg):
       print("got an interrupt in pin10 %s" % (arg.id()))
    
    def pin11_handler(arg):
       print("got an interrupt in pin11 %s" % (arg.id()))
    
    def pin12_handler(arg):
       print("got an interrupt in pin12 %s" % (arg.id()))
    ​
    p_in10 = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP)
    p_in10.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin10_handler)
    
    
    ​
    p_in11 = Pin('P11', mode=Pin.IN, pull=Pin.PULL_UP)
    p_in11.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin11_handler)
    
    
    ​
    p_in12 = Pin('P12', mode=Pin.IN, pull=Pin.PULL_UP)
    p_in12.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING, pin12_handler)
    

    I didn't test it but i gues it would work



  • @pybuggz At the very least, with this code, you'll get two calls, one when the button is pressed, another when it is released (due to IRQ_FALLING and IRQ_RISING being set).

    Do you receive more than that?


 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter