# Counting the seconds a button keep being pressed.

Status
Not open for further replies.

#### alphacat

##### New Member
Hello,

I defined one of the GPIO's of the microcontroller as tristate input, and its connected to an Active-low switch.
When the switch is being pressed, the GPIO's voltage turns from High to Low, and when the switch is released, the GPIO's voltage turns back to High.

So far, I didnt care for how long the switch was pressed, but now i was asked that when the switch is pressed for 3 seconds (or more), then I'll execute a ceratin command.

Do you have any suggestions of how doing it?
I thought that when the switch is pressed, i'll check the GPIO's voltage every 100mSec and if it remains LOW for 30 '100mSec checks' (meaning for 3 seconds), then I'll execute the command.

Last edited:

#### alphacat

##### New Member
Alright I started working on it and i have a question please.
The demands are:
* When the button is pressed once for a breif moment (a regular pressing), it perfroms X.
* When the button is pressed once for 3 seconds (or more), it performs Y.
* When the button is pressed for a duration which is somewhere in between, it does nothing.

My question is, how long does a regular pressing last?
What I wanna do is that:
1. A pressing happens
2. after Z time I check if the button is still pressed (= if the GPIO's voltage is still LOW).
3. If after Z time it isnt pressed no more, I consider it a regular press and perform X.
4. If after Z time its still pressed I keep checking the GPIO's voltage every 100msec, and only if its being pressed for 3sec, I perform Y.
5. If its not pressed for 3 sec (but was still pressed after Z time), I do nothing.

My question is, What is Z?

Last edited:

#### blueroomelectronics

##### Well-Known Member
That's like asking how long is a piece of string.

#### alphacat

##### New Member
Well, I dont want the PRESSING_TIME to be too short since in that case, I will consider it as if the user wanted to press the button for 3 seconds but his finger slipped, so i'll do nothing.

On the other hand, I dont want the PRESSING_TIME to be too long, since if there're two button pressings that happen one after another it might drive the system crazy if the system hasnt completed the first pressing.

Last edited:

#### AtomSoft

##### Well-Known Member
I say delay around 20ms should be enough for a fast press and a slow press around 100-300ms.

It depends tho. what is it for? I ask because if it was a keyboard then its way faster since people tend to type pretty quick(me).

If its normal button like a PC on/off where you can press it and it starts shutting down or whatever or you can hold it for 6-7 seconds and it cuts off power completely

Last edited:

#### blueroomelectronics

##### Well-Known Member
Keyboards are often scanned for make (closed) debounce 20ms & break (open)

#### AtomSoft

##### Well-Known Member
I figure you dont have to work about a normal press. Just worry about the 3 second press.

I say if a press is longer than 600ms then its being held down.

#### alphacat

##### New Member
Alright, thank you very much for helping out.
I'll go for 100ms for a start and see how it works out.

Thanks friends.

#### be80be

##### Well-Known Member
A good switch will bounce for about 5mS most people press the button that fast too so most will use a 10mS debounce per key press. What you want is hard code to write button down low button up high and the pic can catch it in a blink of the eye. That's why most use two buttons.And if you search net you'll not find much on how to use one button for more then one process
1 press do X
2 presses do Y // Y can be a long hold down on the button
If Y or X false
wait 3sec and go back and check agin

Last edited:

#### alphacat

##### New Member
Thanks.
I cant wait 3 sec and then go back to check the GPIO's voltage since I need to continuously check that the button keeps being pressed during these 3sec.
Currently I decided to check every 100msec during these 3sec, that the button is still being pressed.

#### AtomSoft

##### Well-Known Member
if you check every 100ms for 3 seconds(3000ms) then you can setup a timeVar to increment every 100ms and if the value is 3000/100 =30 then its been held down the entire time.

You can also simple have it stop when the button goes low. but if you do it by using a variable and counting you can achieve more like...

1 second the var would be 10
2 seconds the var would be 20
3 seconds the var would be 30

Then you can check to see where the value is in between and have different function easy

#### be80be

##### Well-Known Member
All you need is three counters
one for short keypress say 10ms if checked and still low 10 times roll a one into the 100ms and repeat for the 1000ms counter. that would give you three states lol or like atom said you hadn't posted lol

Last edited:

#### AtomSoft

##### Well-Known Member
you can do it in 1 variable like i said.
Here is a sample i just wrote lol hope you understand it
Code:
unsigned char time;
char x;

for(x=0;x<30;x++){
delay100ms();
if(pin==1)  //Active high.... I assume using a pulldown
time++;
else
break;
}

//3 seconds or button was released
if(time >=26){
//approx 3 seconds
}
if(time < 26){
if(time >= 16){
//approx 2 seconds
}
}
if (time < 16){
if(time >= 6){
//approx 1 second
}
}
if(time < 6){
//Normal button press
}

Last edited:

#### be80be

##### Well-Known Member
Atom your getting to good for you pants Code looks good just one thing
Active-low switch.

Last edited:

#### AtomSoft

##### Well-Known Member
paints?

Thanks for the complement

#### AtomSoft

##### Well-Known Member
Fixed:
Code:
unsigned char time;
char x;

for(x=0;x<30;x++){
delay100ms();
if(pin==0)  //Active low.... I assume using a pullup
time++;
else
break;
}

//3 seconds or button was released
if(time >=26){
//approx 3 seconds
}
if(time < 26){
if(time >= 16){
//approx 2 seconds
}
}
if (time < 16){
if(time >= 6){
//approx 1 second
}
}
if(time < 6){
//Normal button press
}

Last edited:

#### AtomSoft

##### Well-Known Member
1 question that may help the OP .....

Do you save power when using a PULL-DOWN verses a PULL-UP button?

#### be80be

##### Well-Known Member
I never said i could spell pants. lol

#### AtomSoft

##### Well-Known Member
heh lol. i have some questions to ask you so check your PM in about 2 minutes ok

#### be80be

##### Well-Known Member
That's a killer most TUTORIALS use high going low switches which wast power. I like the low going high. pull them switches down and read them for high is best.

Status
Not open for further replies.