Differentiate Double & Single Click of a button

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Mrfittingin
Posts: 3
Joined: Wed May 27, 2020 7:33 pm

Differentiate Double & Single Click of a button

Post by Mrfittingin » Wed May 27, 2020 7:48 pm

Hey guys
I have started to learn MicroPython recently, there is one problem I struggled for a while, I have been searching for solutions online and still have no clew what to do. Please help, Thanks.
This is the code, I have used aswitch.py module, I wish to achieve when I am double click the button, only the Yellow LED lights up( the Blue LED has no reaction while there is a double click of the button). and When this is long press, only the Green LED lights up. ( the Blue LED has no reaction while there is a long press of the button)
How should I change the Code to reach this function?

Code: Select all

from pyb import LED
from machine import Pin
import uasyncio as asyncio
from aswitch import Pushbutton

def toggle(led):
    led.toggle()

async def my_app():
    await asyncio.sleep(600)

pin = Pin('X1', Pin.IN, Pin.PULL_UP)  # Pushbutton to gnd
blue = LED(4)
yellow = LED(3)
green = LED(2)

pb = Pushbutton(pin)

pb.press_func(toggle, (blue,))  # Note how function and args are passed
pb.double_func(toggle, (yellow,))  # Note how function and args are passed
pb.long_func(toggle, (green,))  # Note how function and args are passed

loop = asyncio.get_event_loop()
loop.run_until_complete(my_app())  # Run main application code

kevinkk525
Posts: 732
Joined: Sat Feb 03, 2018 7:02 pm

Re: Differentiate Double & Single Click of a button

Post by kevinkk525 » Wed May 27, 2020 9:18 pm

pb = Pushbutton(pin, suppress=True)

Should solve your problem.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mrfittingin
Posts: 3
Joined: Wed May 27, 2020 7:33 pm

Re: Differentiate Double & Single Click of a button

Post by Mrfittingin » Thu May 28, 2020 12:17 pm

Hey Kiven

Thank you for you help, I have added pb = Pushbutton(pin, suppress=True) into my code, but it did not work as I expected, the blue light always light up when the first click was pushed of the double click (same for the long press as well).

I wonder if there is any delay function or time frame interval function to let the full signal to go through, then start to process it?

I pasted the code down below.

Thank you!

Code: Select all

from pyb import LED
from machine import Pin
import uasyncio as asyncio
from aswitch import Pushbutton

def toggle(led):
    led.toggle()

async def my_app():
    await asyncio.sleep(600)

pin = Pin('X1', Pin.IN, Pin.PULL_UP)  # Pushbutton to gnd
blue = LED(4)
yellow = LED(3)
green = LED(2)

pb = Pushbutton(pin, suppress=True)


pb.double_func(toggle, (yellow,))  # Note how function and args are passed
pb.long_func(toggle, (green,))  # Note how function and args are passed

pb.press_func(toggle, (blue,))  # Note how function and args are passed

loop = asyncio.get_event_loop()
loop.run_until_complete(my_app())  # Run main application code

User avatar
pythoncoder
Posts: 4242
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Differentiate Double & Single Click of a button

Post by pythoncoder » Fri May 29, 2020 10:20 am

@Mrfittingin I think you have misunderstood the docs.

For a callback which occurs only on a single short press, you need to assign it to release_func and set suppress=True. As you have found, press_func still occurs on initial button press; this is by design. The reasoning is explained in the above doc. Note that response is necessarily delayed: this is also explained in the doc.
Peter Hinch

kevinkk525
Posts: 732
Joined: Sat Feb 03, 2018 7:02 pm

Re: Differentiate Double & Single Click of a button

Post by kevinkk525 » Fri May 29, 2020 10:37 am

oh yes, thanks @pythoncoder. I missed the detail about having to use the release_func
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Mrfittingin
Posts: 3
Joined: Wed May 27, 2020 7:33 pm

Re: Differentiate Double & Single Click of a button

Post by Mrfittingin » Mon Jun 01, 2020 2:24 pm

Thank you so much for your help, problem solved!

Post Reply