Press & hold functionality with Arduino

Arduino Technology

From the MAKE Flickr pool

Flickr member jmsaltzman coded some extra functionality into a basic momentary pushbutton –

To get more functionality out of a single button and to add a “hidden” mode, I extended button debouncing logic to allow for click as well as press+hold. The green LED toggles when the button is clicked, and the red LED toggles on press and hold. In this example, the hold time is 2 sec.

Of course this technique comes in very handy when your project is running low on available pins. Arduino compatible code is available in the comments of his blog entry.

10 thoughts on “Press & hold functionality with Arduino

  1. SlapYak says:

    I used something like this in a project I was working on. Though I think this could be compacted further. If you use a simple counter ‘while’ or ‘for’ loop, you can have the led turn on, or the function occur right when the threshold between a ‘press’ and a ‘hold’ is crossed.

    Attach this function to an interrupt pin

    int ButtonCheck(){
    count = 0;
    if (count > 100){ /button held
    return 2; /button held
    else if count > 0
    return 1; /button pressed

    1. Zero says:

      Look up triggers and releases, it’s a few simple binary operator operations and you only need one variable to calculate states per register. That way you will get a single hit trigger that will empty after one cycle, and if the button is still depressed after the trigger has been de-flagged you will be holding. You can either count the holds or just wait for the release, then calculate the time between the trigger and release and compare that to a timer. Done, most likely with less code and certainly cleaner. I use it so much on micro controllers (Renesas or Atmel only! No PIC for me!) I have very cleanly worked out in a library I include in basically all of my projects.

      1. says:

        Zero, curious if you have a code example folks could check out? I couldn’t find anything searching so just built it from scratch; didn’t know what to search for. The code is actually pretty simple and fast, but I like commenting excessively (see? I’m commenting right now…:).

        SlapYak: That could work for some programs but I prefer not to use delay() at all– I like my code to loop as fast as possible. With a frequency counter function added, this simple sketch cycles through its loop() at between 70KHz and 90KHz.

      2. SlapYak says:

        I am unfamiliar with triggers and releases. I have no formal coding training or anything.

        After a quick google search I was unable to find anything explaining the concept or application. Could you show me via code how the triggers and realeases work?

        -As a note – I used delay in that program because quick cycling is not an issue for the project, it is an RGB lighting setup and the button presses are only for programming.

  2. justDIY says:

    In Proton basic (for the PIC micro), I used a while loop to achieve more than one “press and hold” for a button… you’ll want to have some means of alerting the user they are achieving these different states … perhaps an led turns on when state 2 is achieved, and starts to blink once state 3 is achieved.

    I don’t have the exact code handy but something like this:

    buttonpress: ‘ jump here using proton’s built-in button debounce routine
    counter = 0
    while portc.1 = 1
    counter = counter + 1
    if counter > 200 then goto longpress1
    delayms 10
    wend ‘ if user lets go before 2000 ms, consider it a short press

    ‘ short press code goes here
    some commands

    counter = 0
    while portc.1 = 1
    ‘ if portc.1 is still high, button is still held down
    counter = counter + 1
    if counter > 300 then goto longpress2
    delayms 10
    wend ‘ if user lets go before 5000 ms elpases, consider it a medium press

    ‘ medium press code goes here
    some commands

    ‘ button was held down for 5 seconds, power down or something

Comments are closed.