8 years, 9 months ago.

Short press and long press on one button

I have two actions defined on one button, one action for "Pressed" and one action for "PressedHeld", this works. But when I do a long press the short press action always triggers first, then the long-press action triggers. I would like to get the long-press action only when a button is long-pressed, is this possible with this library ? Thanks !

Question relating to:

InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold. debounce, DigitalIn, InterruptIn

1 Answer

8 years, 9 months ago.

Use attach_asserted_held for the long press and attach_deasserted for the short press.

You will only get the short press notification when they let go of the button rather than almost as soon as it's pressed but you can't know it's a short press until then anyway.

Accepted Answer

Hi Andy

I have a similar problem.

I have set up a button "sw2" the following way:

sw2.attach_deasserted( &sw2Frequency );

sw2.attach_asserted_held( &sw2TransceiveMode_held );

sw2.setSamplesTillAssert( 1 );

sw2.setSamplesTillHeld( 100 ); Held press for 20*100ms = 2 seconds

sw2.setAssertValue( 0 );

sw2.setSampleFrequency( 20000 );

What I want is:

- when sw2 is pushed and quickly released, sw2Frequency function is called.

- when sw2 is pushed and held for 2 seconds, sw2TransceiveMode_held function is called.

What I get is:

- when sw2 is pushed and quickly released, sw2Frequency function is called. (as I expect)

- when sw2 is pushed and held for 2 seconds, sw2TransceiveMode_held function is called. As I expect, EXCEPT when the button is released again, sw2Frequency function gets called.

How can I avoid calling sw2Frequency when releasing after a held press?

Thank you!

posted by Mathias Johnsen 20 Sep 2015