Press and hold PBS to switch on

Hello,

Posted a thread a few weeks ago that led to some good advice surrounding pressing and holding a push button to trigger another piece of code.

The debounce function was suggested as a plausible way to achieve this outcome - However, I am a complete electronics newb and couldn't get it to work!!!

Having spoken to my tutor he mentioned that the debounce feature was mainly useful in minimising the amount of lines of code and that wasn't really an issue for me.
He suggested writing a basic code that essentially used delays to "do the job" so basically using if statements with delays.

So what I am asking is does anyone know how (without any technical business) that you can add a press and hold function to an input (I need the button to be pressed for 5 seconds before activating the next piece of code).

Any help would be great.

Cheers.

Tom

One implementation would be, once you sense the button press the first time, delay 5 seconds, then check the state of the button and see if it's "still" pressed. That's a very simple minded solution, and it won't correctly deal with situations where the user pressed the button (thus getting you into the delay portion of the code), then lifted their finger, then 4.9 seconds later they pushed the button again.

Tastewar -

:astonished:

That is a very good idea. I will adapt the code to do that, see how it goes and let you know how I get on.

It worked! Thanks for your advice - that's the kind of simple outcome I was looking for.

Now I just need to wire in an LED to go on when the PBS is pressed and bingo! Project done.

Thanks guys :grin:

You're welcome. Though I confess I was secretly hoping that wasn't going to be a good enough solution :slight_smile: