Many electronic devices turn on/off, or more correctly on/sleep, by pressing a button and holding it for two or three seconds. I would like to put this ability into a project using an Adafruit Feather board. The sleep side is easy, I can detect a level and if it stays there for some time, run the code to put the processor to sleep. My question involves the wake up side. The chip wakes up via an interrupt.

The question then becomes, how do I generate the interrupt source only after the button has been held for the designated time? I've considered two options:

1) Use a level interrupt and an RC circuit to do the delay. 2) Have the processor wake up immediately and then implement logic to determine if it has been held "long enough" and go back to sleep if not.

Am I missing an easy option? Is there a general consensus on the best way to do this? It seems like such a normal thing that I'm surprised my searches haven't turned up any tutorials.

migrated from electronics.stackexchange.com Apr 5 at 18:35

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Can't you just wait for the button to be released, and then to go to sleep? You could even disable the interrupt while awake, on only turn it on, right before going to sleep. – Gerben Apr 5 at 20:40
  • The problem with that is that random bumps get treated as requests to sleep/wakeup. Press and hold for "long enough" is very desirable. – PhotoKevin Apr 7 at 15:22
  • Oh, you want to have press-and-hold, for wake-up as well. In that case, have the MCU wake up from sleep by the button; wait till the button is released; if it's less than 3 seconds, go back to sleep. – Gerben Apr 7 at 16:00

I would go for option 2.

You can wake up the controller immediately, but only the processor.

Then just start waiting for the "long enough" time that you defined, regulary checking the button state. If the button is still pushed, then initiate full wake up. If it is released, go back to sleep.

This is the easy option, requiring no extra hardware, and only a dozen of so of lines of code.

Another option, that exist and have a somewhat similar behaviour, is to have your processor wake up regulary on timer, and checking the state of the input. This is what is done for "radio controled" devices, for example (Where the "button" is a radio circuit that need to be switch on and off). Given a sufficiently long sleep between check, on average you will get the "long enough" delay.

This option is useful when you don't have available interrupt pins, or you need something more complex that a button as the input.

  • Waking up would up the power consumption by a lot. The wake up frequency would have to be, oh probably 3x the button press time to be good for a human. – PhotoKevin Apr 7 at 15:25
  • It would indeed not be the best option, but I wanted to include it for completness. As I said, if possible, go for the other one. – Kevin FONTAINE Apr 8 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.