PDA

View Full Version : Button press and press & hold how to ?



GrandPa
- 20th August 2007, 21:58
I would like to know what is the best way to code a program to call two different subroutines depending if I either press OR "press and hold" a button.

I want to call "STOP" subroutine evey time a button is pressed, and I also want to call "PARK" subroutine if the button is hold for a few seconds.


Any suggestion?

J-P

Robson
- 21st August 2007, 00:07
Hello grandpa,

Maybe this can be good for you.



Button VAR PORTB.0 ' Your Input switch
Loop VAR WORD

InputButton:
IF Button = 1 THEN Check4Button
GOTO InputButton

Check4Button:
FOR Loop = 0 TO 1000 ' 1 Second
PAUSE 1
IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
NEXT Loop
IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
GOTO InputButton

Stop:
' Here is your code

Park:
' Here is your other code

END


regards Rob

Johan
- 21st August 2007, 04:16
Hi Grandpa,

As you use push switch, don't forget to debounce it

Johan

sayzer
- 21st August 2007, 08:18
Hello grandpa,

Maybe this can be good for you.



Button VAR PORTB.0 ' Your Input switch
Loop VAR WORD

InputButton:
IF Button = 1 THEN Check4Button
GOTO InputButton

Check4Button:
FOR Loop = 0 TO 1000 ' 1 Second
PAUSE 1
IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
NEXT Loop
IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
GOTO InputButton

Stop:
' Here is your code

Park:
' Here is your other code

END


regards Rob



Something to pay attention for:


Button and Stop are reserved words =>> they are commands.

You may want to rename them as Btn and Stp.

============================

Robson
- 21st August 2007, 08:29
Oh of course. How I forget it ;-x
Maybe it was too late 4 me ...


Btn VAR PORTB.0 ' Your Input switch
LoopTime VAR WORD

InputButton:
PAUSE 20 ' Button delay or use DEFINE BUTTON_PAUSE before
IF Btn = 1 THEN Check4Button
GOTO InputButton

Check4Button:
FOR LoopTime = 0 TO 1000 ' 1 Second
PAUSE 1
IF Btn = 0 THEN Stp ' If button was released before 1 Sec = short keypress
NEXT LoopTime
IF Btn = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
GOTO InputButton

Stp:
' Here is your code

Park:
' Here is your other code

END

weirdjim
- 21st August 2007, 18:00
Check4Button:
FOR Loop = 0 TO 1000 ' 1 Second
PAUSE 1
IF Button = 0 THEN Stop ' If button was released before 1 Sec = short keypress
NEXT Loop
IF Button = 1 THEN Park ' If button was hold longer than 1 Sec = long keypress
GOTO InputButton

Stop:
' Here is your code

Park:
' Here is your other code

END
[/code]

regards Rob


I'm a real newbie at the PIC, but my BASIC is fairly decent. What is the reason for the GOTO InputButton command at the end of the FOR-NEXT loop? The button will be either zero (in which case you GOTO Stp) or one (in which case you GOTO Prk). How would the code ever get to that GOTO InputButton command? Or is that a belt-and-suspenders command on the off chance that the button is released during the last millisecond of the loop?

Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

Jim

peterdeco1
- 21st August 2007, 19:07
Hello Everyone. I use a similar system on a voice record circuit. The record button works as a record button but if pushed and held without making a recording, it resets the voice record chip to a preset address at the beginning of memory. Snippets of code below. Perhaps you could use this system for your circuit.

'PORTB.0 IS THE RECORD BUTTON, 0 = PUSHED, 1 = RELEASED
'PORTB.1 IS MESSAGE BUTTON, 0 = PUSHED, 1 = RELEASED

IF PORTB.0 = 0 AND PORTB.1 = 0 THEN RECORD 'RECORD MESSAGE
IF PORTB.0 = 0 AND PORTB.1 = 1 THEN RESETMESSAGES 'RECORD BUTTON PUSHED ALONE

RECORD:
DOES THE RECORD FUNCTION

RESETMESSAGES:
IF PORTB.0 = 0 AND PORTB.1 = 0 THEN RECORD 'CHECK IF BOTH ARE PUSHED BEFORE 3 SECONDS
LET RESETTIMER = RESETTIMER + 1
Pause 100
IF RESETTIMER >= 30 Then MAKESOUND 'MAKES BEEP AFTER 3 SECONDS AND CONTINUES PROGRAM
IF PORTB.0 = 0 THEN RESETMESSAGES
IF PORTB.0 = 1 THEN START 'BUTTON RELEASED GOTO START

Robson
- 21st August 2007, 20:11
I'm a real newbie at the PIC, but my BASIC is fairly decent. What is the reason for the GOTO InputButton command at the end of the FOR-NEXT loop? The button will be either zero (in which case you GOTO Stp) or one (in which case you GOTO Prk). How would the code ever get to that GOTO InputButton command? Or is that a belt-and-suspenders command on the off chance that the button is released during the last millisecond of the loop?

Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

Jim

1. GOTO InputButton is for start at the beginning after the button was detected as pressed. Going to Loop the program
2. If the button was released before the timer ends at 1 second means -> short press of the button.
3. ELSE long press of button.
4. Insert a pause of 20ms -50ms. My best results measured with 20ms for Button debounce.
If you have a complex matrix of columns and rows with double action feature like this, your code will slow down. I also prefer to use for next loops with Pause 1, while time critical projects and Interrupt routines.
If you insert
PAUSE 2000 is it impossible to jump to the Interrupt routine, when your pause is active.

Delay2: ' Now itīs possible to interrupt during the PAUSE and jump to your Interrupt location
FOR x = 0 TO 1000
PAUSE 1
NEXT x
RETURN

I mean that are my techniques, everyone should code like he likes it.

GrandPa
- 21st August 2007, 21:09
Hi Robson,

you said: "PAUSE 20 ' Button delay or use DEFINE BUTTON_PAUSE before"

I tought before that DEFINE BUTTON_PAUSE was used only by the BUTTON command. Now, from what you wrote is that correct to say that BUTTON_PAUSE will put a debounce delay on all PIC inputs pins?

J-P

Johan
- 22nd August 2007, 03:37
Somebody said something about switch debounce. Is there a standard debounce code snippet for pushbutton switches? A 50 millisecond delay waiting for the sucker to stop clanging around?

Jim


You can use software debounce ( with Button command ) , but if you dont want to change the code, just put the switch in parallel with a resitor and capacitor, something like this:


sw
-------_-------
---! !------
--vvv----||----!
R C