View Full Version : Difficulty with multi-key debounce Getkey subroutine

08-23-2005, 09:50 AM
I'm using a modified version of the menu software from NV51 except with direct inputs, and I cannot seem to get the buttons to work. Here are the relevant excerpts from my code and my whole code is attached. Can anyone see the problem?

Thank you,


' Stamp (Function) Connection
' --------------------- -----
' pin 1 SOUT J1:6
' pin 2 SIN J1:5
' pin 3 ATN J1:4
' pin 4 VSS J1:3
' pin 5 P0 broken
' pin 6 P1 J1:2 Tourney Lock
' pin 7 P2 open
' pin 8 P3 J3:5 Trigger
' pin 9 P4 J3:3 Safety
' pin 10 P5 J3:1 Valve
' pin 11 P6 J2:9 Mode
' pin 12 P7 J2:7 Select
' pin 13 P8 open
' pin 14 P9 open
' pin 15 P10 J2:6 E
' pin 16 P11 J2:5 RS
' pin 17 P12 J2:4 DB4
' pin 18 P13 J2:3 DB5
' pin 19 P14 J2:2 DB6
' pin 20 P15 J2:1 DB7
' pin 21 Vdd +5V out
' pin 22 RES open
' pin 23 Vss Ground
' pin 24 Vin +9V in


Trig_Swt CON 3 ' Trigger switch
Safe_Swt CON %00010000 ' Safety switch
Key_Mode CON %01000000 ' Mode Button
Key_Sel CON %10000000 ' Select Button
E CON 10 ' LCD Enable pin (1 = enabled)
RS CON 11 ' Register Select (1 = char)


key VAR Byte ' button/switch input variable
key_In VAR INL ' shoes state of input buttons

DIRS = %1111110000100000 ' defines outputs, 1 = output
OUTS = $0000 ' clear the pins (hex $0000 is equivalent to %0000000000000000

key = %11010000 ' assume all pressed
FOR loop = 1 TO 5 ' test five times
key = key & ~key_In ' test against new input
PAUSE 5 ' wait 5 ms between tests

Jon Williams
08-23-2005, 10:32 AM
Are your inputs active-high (1 when pressed)ˇor active-low (0 when pressed)?ˇ I ask because I usually use the ~ operator to invert active-low inputs.

Jon Williams
Applications Engineer, Parallax

08-23-2005, 11:12 AM
I'm using the INEX-1000 board which has active low with pullup buttons.

Jon Williams
08-23-2005, 11:18 AM
That explains it then, you need to pre-initialize the buttons variable to 1's before calling the routine.ˇ The inversion will turn a 0 (button pressed) into 1, and that ANDed with an existing 1 will stay 1.ˇ When a button is released, 0 gets ANDed with the 1 and will stay that way, no matter what happens through the rest of the loop.

ˇ key = %11111010ˇˇˇˇˇˇˇˇˇˇ' assume all pressed
ˇ FOR idx = 1 TO 5ˇˇˇˇˇˇˇˇ ' test five times
ˇˇ ˇkey = key & ~key_Inˇˇˇ ' test against new input
ˇˇ ˇPAUSE 5ˇˇˇˇˇˇˇˇˇˇˇˇˇˇ ˇ' wait 5 ms between tests

Note that I changed your variable called "loop" to "idx" -- this prevents a keyword conflict with PBASIC 2.5.ˇ When the subroutine is done there will be a 1 in the bits of "key" that had a button pressed through the entire routine.

Jon Williams
Applications Engineer, Parallax

08-24-2005, 06:48 AM
I put in what you wrote above, changed key to key = 11010000 and loop to idx. Now, however, the Select button (Key_Sel) is pressing repeatedly, whether the switch is connected or not. Any ideas?

Thank you,


08-27-2005, 04:34 AM
Still having trouble with the Select key, which is on P7. If I do a single line program:


I get only 111 in the debug window, so I think something might be wrong with P7. Is this a good test?


Chris Savage
08-27-2005, 05:03 AM

ˇˇ It's certainly possible that something happened to it somewhere.ˇ There's a schematic in the Help file for the editor for connecting a pushbutton, Active-High or Active-Low.ˇ Try wiring up P7 like that, then try the test code included in that page under Example (Top right of page) and make sure you set the pin for P7.ˇ That should tell you in the pin is working as an input.

Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

08-27-2005, 07:09 AM
I will try that with 7, however moving it off 7 has given me a working circuit and code. Now that it's working I see that I get debounce but I also get repeat with this code. How can I code it so that I get debounce but NO repeat.