Press & hold functionality with Arduino


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.


Recent Entries

Comments

Oldest comments listed first.

Posted by: SlapYak on May 15, 2009 at 6:02 AM

Compacted version

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;
while(digitalRead(But)==HIGH){
delay(10);
count++;
}
if (count > 100){ /button held
return 2; /button held
else if count > 0
return 1; /button pressed
}
}


Posted by: Zero on May 15, 2009 at 6:14 AM

uh, triggers!?

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.


Posted by: jmsaltzman.myopenid.com on May 15, 2009 at 9:34 AM

cool! example?

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.


Posted by: SlapYak on May 15, 2009 at 10:50 AM

Please inform me.

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.


Posted by: justDIY on May 15, 2009 at 12:03 PM

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
return

longpress1:
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
return

longpress2:
' button was held down for 5 seconds, power down or something
return



Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television
Holiday Gift Guides from MAKE
Gifts for Dads
Science and Chemistry
Gifts Under $20
More guides: Santa Claus Machines, Geek Toys for Grown Up Girls & Boys


Check out all of the episodes of Make: television

Alex Rider Dream Gadget Contest
Make: Science Room

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter




    Maker SHED

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Subscribe to MAKE Magazine!

    Make: Online authors!

    Gareth BranwynGareth Branwyn
    Senior Editor


    Phillip TorronePhillip Torrone
    Senior Editor
    | AIM | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Current Podcast

    itunesdl.gif Weekend Project: Beetlebot Simple robot from your parts bin that avoids obstacles. Thanks go to Jerome Demers for the original article in MAKE, Volume 12. To download the Beetlebot video, click here or subscribe in iTunes. Check out the complete Beetlebot article... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog