Shop OBEX P1 Docs P2 Docs Learn Events
interfacing a BS2e with a 4x4 keypad — Parallax Forums

interfacing a BS2e with a 4x4 keypad

jmsiinojmsiino Posts: 7
edited 2008-06-10 05:50 in BASIC Stamp
I have a 4x4 keypad with 8 pins and a carrier board with a BS2e that I finally got to work, but the code is ugly. I'm looking for a cleaner solution.

The keypad pins are 1 = Col4, 2 = Col3, 3 = Col2, 4 = Col1, 5 = Row1, 6 = Row2, 7 = Row3, and 8 = Row4. I am using the BS2e pins Pin1 = Col1 through Pin4 = Col4 as toggling High and Low and testing Pins 5 to 8 (as Rows 1 to 4) as inputs for the High (I added a 330 ohm in line).

Here is my code (I will develop a parser later):

' KeyPadTest4.bse
' {$STAMP BS2e}
' {$PBASIC 2.5}

' values for rdg and key
' button     key       rdg
' 1 - 9      1 - 9     1-3, 5-7, 9-11
' 0          0         14
' A - D      10 - 13   4, 8, 12, 16
' *          14        13
' #          15        15

row     VAR Byte
col     VAR Byte
rdg     VAR Word
key     VAR Word
oldkey  VAR Word

DEBUG CLS, "testing the keypad", CR, CR, CR, "  "

DO UNTIL oldkey = 14

  oldkey = 20

  FOR col = 1 TO 4

  rdg = 20
  key = 20

  LOW 5
  LOW 6
  LOW 7
  LOW 8

  HIGH col

  INPUT 5
  IF IN5 THEN
     rdg = col
  ELSE
     INPUT 6
     IF IN6 THEN
        rdg = col + 4
     ELSE
        INPUT 7
        IF IN7 THEN
           rdg = col + 8
        ELSE
           INPUT 8
           IF IN8 THEN rdg = col + 12
        ENDIF
     ENDIF
  ENDIF

  LOW col

  IF rdg <> 20 THEN LOOKUP rdg, [noparse][[/noparse]0,1,2,3,10,4,5,6,11,7,8,9,12,14,0,15,13], key

  IF (key <> oldkey) THEN
      IF (key < 10) THEN
         DEBUG BKSP, DEC1 key
      ELSE
          SELECT key
             CASE 10
                 DEBUG BKSP, BKSP, " A"
             CASE 11
                 DEBUG BKSP, BKSP, " B"
             CASE 12
                 DEBUG BKSP, BKSP, " C"
             CASE 13
                 DEBUG BKSP, BKSP, " D"
             CASE 14
                 DEBUG BKSP, BKSP, " *"
             CASE 15
                 DEBUG BKSP, BKSP, " #"
          ENDSELECT
      ENDIF
  ENDIF

  oldkey = key

  IF (oldkey = 14) THEN QuitNow

  NEXT
LOOP

QuitNow:
      DEBUG CR, CR, CR, "done", CR
END


Sign In or Register to comment.