PDA

View Full Version : Button press or not



lerameur
- 23rd November 2010, 19:53
Hello,

I have an application where the user pushes a press button to start the menu and also uses the same push button to scoll through the values. It should work fine if the button is press for under a second. But is there anyway to control the lenght of the pull down time? Lets says the user keep pressing on the button for 10 seconds, its will probable go through all the menus as fast as it can !! is there any way to prevent this ???

thanks

K

aerostar
- 23rd November 2010, 21:27
When detecting the pushbutton press, just wait until you detect that it has been released, then continue.
Also suggest you add some de-bounce code to stop extra/false detections.

Archangel
- 24th November 2010, 00:10
Hello,

I have an application where the user pushes a press button to start the menu and also uses the same push button to scoll through the values. It should work fine if the button is press for under a second. But is there anyway to control the lenght of the pull down time? Lets says the user keep pressing on the button for 10 seconds, its will probable go through all the menus as fast as it can !! is there any way to prevent this ???

thanks

K
Hi lerameur, Ken?
I've gotten old, that's why I call my wife Dear, better than Hey You ! :D
To answer your question, yes force the menue to exit the subroutine after the last selection or allow it to repeat once then exit. Here is a link to one of Melanies programs involving button presses and multi function buttons, I am pretty sure she covered this topic a couple of times.
http://www.picbasic.co.uk/forum/showthread.php?t=3423
be sure to use the "Special" Google link to search INSIDE the forum.
http://www.google.com/custom?hl=en&cof=AH%3Aleft%3BS%3Ahttp%3A%2F%2Fwww.picbasic.co.u k%2Fforum%3BL%3Ahttp%3A%2F%2Fwww.crownhill.co.uk%2 Flogo.gif%3BLH%3A37%3BLW%3A174%3B&domains=picbasic.co.uk&q=&btnG=Search&sitesearch=
it really works a Treat.
Cheers, I hope this is helpful.
JS

lerameur
- 24th November 2010, 14:27
Hi,
I guess i have one more question about the while loop for a program fromMelanie:
Here is a snip if the code

While MyButton=0 'PORTB.0, zero when not pressed, 1 when pressed
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
Wend

The problem with this is that I have a battery charger Subroutine in there. Therefore the code looks like this.

While MyButton=0 'PORTB.0, zero when not pressed, 1 when pressed
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
Gosub Charge_Battery ' takes hours to charge
Wend

Therefore MyButton is going to increment very rarely. Almost never. In Melanie's code there is also the option of Longpress, which requires to hold the button for a few seconds. This is what I need, How do I incorporate this Longpress into my program.??
Here is Melanie's full code:
MyButton var PortB.0 ' Your Button can be anywhere
' Connect between PIC pin and Vss
' Use Weak Pull-Up or Resistor to Vdd

ButtonPress var BYTE ' Button Counter Variable

LongPress con 20 ' Change this value for desired SET
' function trip-point in 50mS steps
' Currently set for 1 Second


MainLoop:
LCDOut $FE,1,"Go Press..."
ButtonLoop:
Gosub GetButton
If ButtonPress>0 then
If ButtonPress=1
LCDOut $FE,1,"Short Press"
else
LCDOut $FE,1,"Long Press"
endif
Pause 1000
Goto MainLoop
endif
Goto ButtonLoop

'
' Subroutine weighs-up users finger
' in multiples of 50mS
' Constant LONGPRESS determines boredom level
' -------------------------------------------
' on Exit...
' ButtonPress=0 - No Press
' ButtonPress=1 - Short Press
' ButtonPress=2 - Long Press
GetButton:
ButtonPress=0
While MyButton=0 'PORTB.0, zero when not pressed, 1 when pressed
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
Wend
If ButtonPress>0 then
If ButtonPress=>LongPress then
ButtonPress=2
else
ButtonPress=1
endif
endif
Return

Archangel
- 24th November 2010, 20:37
I did not take the time to download your code and test it.
I would allow the button press (mainloop?) to run and collect the button press data before the gosub executes and/or write my code to allow the gosub to check for additional buttonpresses by short cycle returns or use interrupts to detect the button press.