Shop OBEX P1 Docs P2 Docs Learn Events
Difficulty with multi-key debounce Getkey subroutine — Parallax Forums

Difficulty with multi-key debounce Getkey subroutine

TorquewrenchTorquewrench Posts: 28
edited 2005-08-27 00:09 in BASIC Stamp
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,

Phil

' 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

'Constants

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)

'Variables

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

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

GetKey:
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
NEXT
RETURN

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-23 03:32
    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
  • TorquewrenchTorquewrench Posts: 28
    edited 2005-08-23 04:12
    I'm using the INEX-1000 board which has active low with pullup buttons.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-23 04:18
    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.

    Get_Key:
    · 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
    · NEXT
    · RETURN

    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
  • TorquewrenchTorquewrench Posts: 28
    edited 2005-08-23 23:48
    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,

    Phil
  • TorquewrenchTorquewrench Posts: 28
    edited 2005-08-26 21:34
    Still having trouble with the Select key, which is on P7. If I do a single line program:

    DEBUG BIN INB

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

    Thanks,
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-26 22:03
    Torquewrench,

    ·· 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
  • TorquewrenchTorquewrench Posts: 28
    edited 2005-08-27 00:09
    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.

    Thanks,

    Phil
Sign In or Register to comment.