PDA

View Full Version : Timing the pressing and releasing of a PushButton ?



serandre
- 29th March 2006, 01:34
Hello !

I need a code example that calculate how much time a push-button is keeping pressed and the time elapsed between it was pressed and released.

I want to make a single push-button a multi-purpose entry system, as like the system used in the Cel phones (one key to ABC, another to DEF, etc...)

Since I use most the 12F675 in the projects because I need a small pcb, I end have only one input available and I don't want to use a PIC of more than 8 pins.

I created a RGB controller with LCD and need to complete the system only this type of timing, bellow is how I made it:

GP0-1......... 2 Wire LCD controller
GP2-4-5...... PWM for the RGB leds
GP3............ Input

Actually I'm using the GP3 as a input to change the Pattern selection of the controller. But I need to change also the Speed, but using the same input.

Let's say if keeping the input low (pressed) for 500ms then I can use it to change speed, if less then 500ms then is a single push and I can use it to change the pattern.

I somebody knows how to handle this with a code. It can be in any language, I use JAL, but any code will help me.

Thanks a lot!

Melanie
- 29th March 2006, 03:50
ButtonPress var GPIO.3
Time var Word

Time=0
While ButtonPress=0
Time=Time+1
Pause 10
Wend

For as long as the Button remains pressed, variable Time will increment by one every ten milliseconds. On exit it will contain the value of how long the Button was held in 10mS steps. Time can be a BYTE instead of a WORD. Changing the PAUSE value will naturally change the period step count pro rata.

serandre
- 29th March 2006, 04:10
ButtonPress var GPIO.3
Time var Word

Time=0
While ButtonPress=0
Time=Time+1
Pause 10
Wend



This code will save my project. And so clean and simple... :-)

Thanks a lot Melanie !

Melanie
- 29th March 2006, 08:11
Just one last comment... if you have a crappy switch subject to bounce, use a larger PAUSE step of say 25 or 50mS and that will eliminate it (personally I tend to use 75 or even 100mS).

Ioannis
- 30th March 2006, 13:43
A note to auto exit after say 500 ms:

Within the while loop put an If time>500 then exit1

So the user does not need to hold the button more than the necessary time.

Ioannis