[SOLVED] Custom (external) Button

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
vince
Posts: 15
Joined: Wed May 06, 2020 9:23 am

[SOLVED] Custom (external) Button

Post by vince » Wed Sep 02, 2020 1:30 pm

Hello everybody!
NEWBIE question: I want to set up an external button. I should behave exactly like the USR button (I would like to trigger a function when the button is pressed).

Here is the code I use to with the USR button:

Code: Select all

sw = pyb.Switch()
sw.callback(switchup)
If I try to copy this code to my external button, I get the following error:

Code: Select all

AttributeError: 'Pin' object has no attribute 'callback'
Is there a good tutorial around? Basically I just want to replace the USR button by an external one, nothing else. How to attribute a callback to the external button?

Thank you in advance!

Vince
Last edited by vince on Tue Sep 15, 2020 1:50 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Custom (external) Button

Post by Roberthh » Wed Sep 02, 2020 1:55 pm

You have to use the pin.irq() method to register a callback. See: http://docs.micropython.org/en/latest/l ... achine.Pin

vince
Posts: 15
Joined: Wed May 06, 2020 9:23 am

Re: Custom (external) Button

Post by vince » Wed Sep 02, 2020 3:45 pm

Hi Roberthh,
thank you very much for the hint!

I read the doc, but I don't really get it how to implement it? Here is what I did, bit somehow it doesn't work (I want to trigger the function switchup())

Code: Select all

sw_up = Pin('Y12', Pin.IN, Pin.PULL_UP)
sw_up.irq(handler=switchup(), trigger=Pin.IRQ_RISING)
Or did I misunderstand something?
Last edited by vince on Thu Sep 03, 2020 10:40 am, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Custom (external) Button

Post by Roberthh » Wed Sep 02, 2020 4:04 pm

You have to omit the brackets after switchup. Because with brackets it switchup is called and the result is assigned as a handler, which is wrong.

Code: Select all

sw_up = Pin('Y12', Pin.IN, Pin.PULL_UP)
sw_up.irq(handler=switchup, trigger=Pin.IRQ_RISING)
Edit:
handler is an optional function to be called when the interrupt triggers. The handler must take exactly one argument which is the Pin instance.

vince
Posts: 15
Joined: Wed May 06, 2020 9:23 am

Re: Custom (external) Button

Post by vince » Thu Sep 03, 2020 11:01 am

Hello,

thank you very much. Unfortunately I get now the following error:

Code: Select all

TypeError: function takes 0 positional arguments but 1 were given
.. I I still don't know how to do it. I think I miss something... and I don't find any proper tutorial/example to figure it out by myself. Could you give me a hand on this? Here is my complete code.

Code: Select all

i = 1
def switchup():
    global i 
    # raise or reset i    
    if i < 9:    
        i = i+1
    else:
        i = 1


# trigger function with USR switch
sw = pyb.Switch()
sw.callback(switchup)


# IN Pins -> This should replace USR switch
sw_up = Pin('Y12', Pin.IN, Pin.PULL_UP)
sw_up.irq(handler=switchup, trigger=Pin.IRQ_RISING)

I want to replace "sw" (USR button) by "sw_up" (external Button attached between Y12 and GND).
On each button press, "i" should increase (or be reset). This works fine with "sw", but not with "sw_up" because of this IRQ thing.

Thank you so much for the support!

All the best

Vince
Last edited by vince on Thu Sep 03, 2020 12:28 pm, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Custom (external) Button

Post by jimmo » Thu Sep 03, 2020 11:44 am

For a pin irq, the callback function needs to take a single argument (which will be the pin that fired the irq).

Code: Select all

def pin_switchup(pin):
  switchup()
  
sw_up.irq(handler=pin_callback, trigger....)
You could use a lambda as a wrapper here too

Code: Select all

sw_up.irq(handler=lambda pin: switchup(), trigger...)

vince
Posts: 15
Joined: Wed May 06, 2020 9:23 am

Re: Custom (external) Button

Post by vince » Thu Sep 03, 2020 1:31 pm

Hi jimmo,

yes it works!! thank you very much.

but now I get the following problem: on each press, the pin_switchup(pin) function is called many times.. I would like to call it only one. Is there a simple trick to do this?

Thanks in advance,

vince

vince
Posts: 15
Joined: Wed May 06, 2020 9:23 am

Re: Custom (external) Button

Post by vince » Thu Sep 03, 2020 3:10 pm

I found a solution. Maybe not the best, but it works:

Code: Select all

btn_reset = 0
def pin_switchup(pin):
    global btn_reset
    if btn_reset == 0:
        btn_reset = 1
        switchup()
and later, I reset btn_reset = 0 in the main loop:

Code: Select all

while True:
    btn_reset = 0
    ... (functions and time sleeps)

Post Reply