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
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
If you have an LCD for example, you can always clear the display (or light a LED if you have one) to indicate that a SET has been performed. Here's a variation..
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
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
If ButtonPress=LongPress then LCDOut $FE,1
Wend
If ButtonPress>0 then
If ButtonPress=>LongPress then
ButtonPress=2
else
ButtonPress=1
endif
endif
Return
Another variation which I tend to use, is if you have a Piezo, beep it at every Button Press start (ie ButtonCount=1) as a confidence indicator for the user (they like things like that), and then Beep Constantly once the LONGPRESS value has been reached. example...
Code:
'
' 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
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
If ButtonPress=1 then Gosub Beep
If ButtonPress=>LongPress then Gosub Beep
Wend
If ButtonPress>0 then
If ButtonPress=>LongPress then
ButtonPress=2
else
ButtonPress=1
endif
endif
Return
You can go on with variations for ever....
Code:
'
' 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
If ButtonPress<255 then ButtonPress=ButtonPress+1
Pause 50 ' This is also our Debounce value
If ButtonPress=1 then Gosub Beep
If ButtonPress=>LongPress then Gosub Beep
If ButtonPress=255 then Gosub Klaxon ' Users fallen asleep
Wend
If ButtonPress>0 then
If ButtonPress=>LongPress then
ButtonPress=2
else
ButtonPress=1
endif
endif
Return
I use a WORD as the Counter in many cases, and if the Button is held for a really long time (eg 30 Seconds or even a Minute), it lets me jump into a Secret Set-Up Menu or just display an unexpected message for the User if I'm feeling devilish... or just as a 'Granny Button' to erase Passwords, Contrast and LCD Backlight levels that have been messed up. Users are a peculiar bunch... they put a password into the Menu (which they promptly forget) or save a near Invisible LCD Contrast setting and then they call Tech-Support to pull them out of the sh*t. It's nice to tell them to Press and Hold the Button for 30 minutes (or until their finger goes numb - whichever comes first) and everything will reset back to the factory default settings... (it'll reset after a minute, but if you tell them 30 they'll not do it a second time).
Bookmarks