View Full Version : Button command question
aherrera
- 30th August 2009, 00:41
I am using the 5 button commands to handle 5 push buttons in my application. No auto repeat is used, just a simple if pressed… then do… My software checks on each button in order or importance, and once it finds that one is pressed, it jumps out of the 5 button sequence and performs the necessary task. All this works very well.
Now my client requests that when 2 buttons are pressed simultaneously, something else is done. Can any one point me in the right direction to check for two of them pressed at once?
Archangel
- 30th August 2009, 01:16
Hello aherrera,
Ok suppose you are using PortB as follows
'PortB.0 = switch 1
'PortB.1 = switch 2
'PortB.2 = switch 3
'PortB.3 = switch 4
'PortB.4 = switch 5
and you are doing If PortB.0 = 1 then . . .
the port has a binary value when you push a switch, port B.0 = 1, portB.1 = 2, both = 3
so you could use a select case statement or a lookup table to direct your code flow.
aherrera
- 30th August 2009, 01:24
I am not checking every pin; I am using the button command so I can de-bounce the switches.
Archangel
- 30th August 2009, 01:53
I am not checking every pin; I am using the button command so I can de-bounce the switches.
Ok button command is overrated and too complicated.
i am not home and do not have the manual with me so bear with me if I error.
myVar var byte[5]
myVar.0 = portB.0
myVar.1 = portB.1
myvar.2 = portB.2
myvar.3 = portB.3
myvar.4 = portB.4
check:
if myVar != 0 then pause 50
if myVar != 0 then
select case myvar
case 0 goto check
case1 ' button1 pushed
do something . . . fish
case2 ' button2 pushed
do something . . .cut bait
case3 ' 2 buttons pushed
do another something . . . look for better lake
case 4 ' button3 pushed
. . .
case 7 ' 3 buttons pushed
do or not do you decide
case else
goto check
end select
endif
else
goto check
some of the syntax may be incorrect above but it should steer you to what you are wanting
aherrera
- 30th August 2009, 02:37
Thank you for your help, so, you think is over rated, I see you are de-bouncing by pausing, but this will cause the software to repeat the press if someone holds the button pressed, the button command takes care of all this.
mackrackit
- 30th August 2009, 02:43
Another idea.. sort of.
I do not think it is likely that two buttons will be pressed at the same time. So if the buttons are on the same port then read the port twice to see if the second one has been pushed.
Untested but something like
CHECK_BUTS:
BUTVAR = PORTB & $1F
IF BUTVAR >= 1 THEN CHECK2
GOTO CHECK_BUTS
CHECK2
PAUSE 50
BUTVAR = PORTB & $1F
SELECT CASE BUTVAR
'insert case statements
GOTO CHECK_BUTS
Acetronics2
- 30th August 2009, 09:11
Hi,
BUTTON Command could be fine here ... let's suppose button tie pin to ground.
SCAN:
FOR I = 0 to 4
BVar = 0
BUTTON PORTB.0[I],0,255,0,BVar,1,Action
NEXT I
I = 255 ' just to allow CASE 0 ...
Action :
SELECT CASE I
CASE 0 ...
...
CASE ELSE GOTO SCAN
END SELECT
As the debouncing do not occur if button is not pressed ( Mr de la Pallice )... scanning is very fast !
CQFD
Alain
Acetronics2
- 30th August 2009, 13:32
For twin Button press ...
just add another scan loop ...
SCAN:
RESULT = 0
FOR I = 0 to 4
BVar = 0
BUTTON PORTB.0[I],0,255,0,BVar,1,Action
NEXT I
GOTO SCAN ' No need to go further ...
Action :
RESULT.I = 1 ' Store the first button pushed
PAUSE 250 ' Let a little time for pushing the
' next button ...
FOR J= 0 to 4
IF J = I THEN I = I+1 ' Button already pushed !!! ... Skip test.
BVar = 0
BUTTON PORTB.0[I],0,255,0,BVar,1,Action2
NEXT J
GOTO Action1 ' Only one button Pushed ...
Action2:
RESULT.J = 1 ' Store the second button pushed
Action1:
SELECT CASE RESULT
CASE %00000001 ' Button 0 alone
...
CASE %00000010 ' Button 1 Alone
...
CASE %00000100 ' Button 2 Alone
...
...
...
CASE %00000011 ' Buttons 0 and 1 ... or 1 and 0
...
CASE %00000101 ' Buttons 0 and 2 ... or 2 and 0
...
...
...
CASE %00000110 ' Buttons 1 and 2 ... or 2 and 1
...
CASE %00001010 ' Buttons 1 and 3 ... or 3 and 1
...
...
...
CASE %00001100 ' Buttons 2 and 3 ... or 3 and 2
...
CASE %00010100 ' Buttons 2 and 4 ... or 4 and 2
CASE ELSE GOTO SCAN
END SELECT
with 16 bits for RESULT, RESULT.Highbyte for I, RESULT.Lowbyte for J, .... you can even check in which order the buttons have been pushed ...
Too much ??? Ok, I take it back to my kangaroo pocket ...
Alain
aherrera
- 31st August 2009, 00:28
I want to thank everybody for their great help. I think I have enough to get me going here...
Acetronics2
- 31st August 2009, 08:44
Arrrgh !!!
a little typing bug :
IF J = I THEN I = I+1 ' Button already pushed !!! ... Skip
should have been
IF J = I THEN J = J+1 ' Button already pushed !!! ... Skip
as everybody did notice ...
Alain
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.