Page 1 of 1

Push Button Hold (1s) Little Help

Posted: Fri Sep 02, 2011 7:40 pm
by Jordy101091
Hi, All

Can somebody help me to figure out on how to make something like this.
To Go out of a menu screen I would like to make the user hold a button for several seconds before turning back the menu will exit.
If the user presses this button quick the program will return to the top of the menu, this menu will not exit.

I hope somebody can figure this out.
I have tryed it but no succes.

Regards Jordy Kleian

Re: Push Button Hold (1s) Little Help

Posted: Sun Sep 04, 2011 12:56 am
by Spanish_dude
Hi,

You just need to check your input, if it's high for let's say 200ms then it's a short pulse and if it's still on after 700ms it's a long pulse.

Code: Select all

char counter, pulse;

counter = 0;
while ((portb & 0x01) == 1) // check portb pin 0
{
    delay_ms(20); // delay 20ms
    counter++;  // increase counter
}

if (counter <= 10) // 200ms = 20ms * 10
{
    pulse = 0; // short pulse
}
else if (counter >= 35) // 700ms = 20ms * 35
{
    pulse = 1; // long pulse
}
Hope this helps.

Nicolas

EDIT : You just need to change the two if-statements to increase or decrease the time the user needs to push the button. I did it for 200ms and 700ms but you can change that pretty easily.