PDA

View Full Version : Count button presses



plyrathrt
- 4th May 2009, 04:12
I have my project setup to check for button presses on a pin. Simple IF *** = 0 THEN ****

I would like to be able to count the button presses like if 3 button presses are made within a certain time, it would set a variable to 3. If pressed 5 times, set said variable to 5 and so on.

Right now my code is setup to just add to the current variable every time the button is pressed. The problem is that I have it flash an LED to confirm what setting it's on and since it immediately add's to the variable, then checks the variable, you can't just tap the button 4 quick times and jump to setting 4.

Any help would be great.

Acetronics2
- 4th May 2009, 14:23
Hi,

Only solution : , index on LCD or Bargraph or LED Display of the number !!!

Or use a "Rainbow led" ... and display with resistor's code !!!

Alain

peterdeco1
- 4th May 2009, 15:34
Hi. If I understand correctly, something like this should allow you to push the button numerous times while advancing your mode with each push. Unfortunately, I’m at work now & can’t test it. It looks to me like it should work.

START:
CLEAR 'ALL VARIABLES = 0
IF PORTA.0 = 1 THEN START 'BUTTON NOT PUSHED STAY HERE

MODE:
LET TIMER = 0 ‘RESET TIMER
LET MODENUMBER = (MODENUMBER + 1) 'ADVANCE MODE

WAITFORRELEASE:
PAUSE 25 'DEBOUNCE
IF PORTA.0 = 0 THEN WAITFORRELEASE 'BUTTON STILL PUSHED WAIT HERE

BUTTONTIMER:
IF PORTA.0 = 0 THEN MODE 'ADVANCE MODE IF BUTTON PUSHED AGAIN
LET TIMER = (TIMER + 1)
PAUSE 10
IF TIMER >= 500 THEN SIGNALMODE 'AFTER 5 SECONDS SIGNAL THE MODE NUMBER
GOTO BUTTONTIMER

SIGNALMODE:
HIGH PORTB.0 'LIGHT UP LED OR BEEP PIEZO BUZZER
PAUSE 250
LOW PORTB.0 'OFF LED OR SOUNDER
PAUSE 250
LET SIGNAL = (SIGNAL + 1)
IF SIGNAL < MODENUMBER THEN SIGNALMODE 'KEEP FLASHING OR BEEPING
GOTO (YOUR MAIN PROGRAM)

mister_e
- 4th May 2009, 15:46
If you have only 1 push button, you could use an internal timer/counter. This one will increase each time you press on the switch (may need some external debouncing). You could also use an interruptible pin, says INT0, and within your ISR, you increase your counter value and process the Debouncing.

A timer interrupt, or polling a timer overflow to check the Counter value, and you're all set.

plyrathrt
- 4th May 2009, 22:50
I will try the example above. If anyone else has any other suggestions please let me know.