Shop OBEX P1 Docs P2 Docs Learn Events
programming the basic stamp with keypad to be able to read and write 00-99 — Parallax Forums

programming the basic stamp with keypad to be able to read and write 00-99

paulvalpaulval Posts: 13
edited 2014-08-19 08:13 in BASIC Stamp
I am quite new at programming and I am using the program for the keypad. The program only reads 0-9 and a-d and i want to be able to read and write 00-99 a 2 digit number.
any help will be appreciated.

Thank You,

Paul

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-07-14 09:31
    Paul,

    Please link to the code you're referring to, attach it, or post it in code tags so others can see what you're referring to. Also you should link the keypad being used as well.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-07-14 10:14
    attachment.php?attachmentid=78421&d=1297987572
  • paulvalpaulval Posts: 13
    edited 2014-07-15 09:45
    I am using the 27899, 4x4 Matrix-keypad_demo.bs2

    I apologize since I am very new but I will be getting better as I use this web-site an well as learning programming.

    Thank You,

    Paul A. Valdez
  • AribaAriba Posts: 2,687
    edited 2014-07-16 18:44
    Hello Paul

    I have no BS2 to try, and also not much expirience with the BasicStamp, but here is a code-fragment that should build a 2 digit dec from two keypresses.
    If I understand the ReadKeypad subroutine right it returns a 16bit value where each bit stands for a pressed key.
    The GetDecKey subroutine waits until all keys are released and then a new key is pressed. Then the key value is decoded:
    With NCD keypad you get a number 1..16 for the key, and the LOOKUP reads the corresponding dec value for the key from a table (0..9).
    value VAR Word
    
    DO
      GOSUB WaitDecKey
      value = keypad
      GOSUB WaitDecKey
      value = value * 10 + keypad
      DEBUG value, CR
    LOOP
    
    WaitDecKey:
      DO
        GOSUB ReadKeypad
      LOOP UNTIL keypad = 0
      DO
        GOSUB ReadKeypad
      LOOP UNTIL keypad <> 0
      LOOKUP NCD keypad, [0,0,0,0,0,9,8,7,0,6,5,4,0,3,2,1], keypad
    RETURN
    
    'add also the ReadKeypad subroutine and the needed variables
    
    Andy
  • paulvalpaulval Posts: 13
    edited 2014-08-09 19:12
    Mr,. Savage,

    I still can't put the code on this forum. The code I am trying to modify is the demo given for the keypad on your web site. Please help me.

    Thank You,

    Paul
  • GenetixGenetix Posts: 1,752
    edited 2014-08-10 16:38
    Paul, cut and paste your code into a message at then add "Code Tags" so it easy to read.
    Type (code) on the line before you code and (/code) on the line below it. Use [] instead of the () I used.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-11 08:15
    Paul,

    Did Ariba's example not work for you? It looks like it should at first glance. You need only add the existing keypad read subroutine and necessary variable declarations as he suggested.
  • paulvalpaulval Posts: 13
    edited 2014-08-11 19:03
    ' 4x4MatrixKeypad_Demo.bs2
    ' Display buttons pressed on the 4x4 Matrix Membrane Keypad
    ' Author: Parallax HK Engineering
    ' +---+---+---+---+
    ' P0 --X--X--X--X | 1 | 2 | 3 | A |
    ' | | | | +---+---+---+---+
    ' P1 --X--X--X--X | 4 | 5 | 6 | B |
    ' | | | | +---+---+---+---+
    ' P2 --X--X--X--X | 7 | 8 | 9 | C |
    ' | | | | +---+---+---+---+
    ' P3 --X--X--X--X | * | 0 | # | D |
    ' | | | | +---+---+---+---+
    ' R R R R <
    4x1k resistors
    ' | | | |
    ' P4 P5 P6 P7
    '
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    row         VAR  Nib                        ' Variable space for row counting
    column      VAR  Nib                        ' Variable space for column counting
    keypad      VAR  Word                       ' Variable space to store keypad output
    keypadOld   VAR  Word                       ' Variable space to store old keypad output
    temp        VAR  Nib                        ' Variable space for polling column states
    
    DEBUG CLS                                   ' Clear Debug Terminal
    GOSUB Update                                ' Display keypad graphic
    
    DO
      GOSUB ReadKeypad                          ' Read keypad button states
      DEBUG HOME, BIN16 keypad, CR, CR,         ' Display 16-bit keypad value
                  BIN4 keypad >> 12,CR,         ' Display 1st row 4-bit keypad value
                  BIN4 keypad >> 8, CR,         ' Display 2nd row 4-bit keypad value
                  BIN4 keypad >> 4, CR,         ' Display 3rd row 4-bit keypad value
                  BIN4 keypad                   ' Display 4th row 4-bit keypad value
    
      IF keypad <> keypadOld THEN               ' If different button is pressed,
        GOSUB Update                            ' update the keypad graphic to clear
      ENDIF                                     ' old display
    
      IF keypad THEN                            ' Display button pressed in graphic
        GOSUB display
      ENDIF
    
      keypadOld = keypad                        ' Store keypad value in variable keypadOld
    LOOP
    
    '
    [ Subroutine - ReadKeypad ]
    '
    Read keypad BUTTON states
    ReadKeypad:
      keypad = 0
      OUTL   = 000000                        ' Initialize IO
      DIRL   = 000000
    
      FOR row = 0 TO 3
        DIRB = 11                            ' Set columns (P7-P4) as outputs
        OUTB = 00                            ' Pull columns low (act as pull down)
        OUTA = 1 << Row                         ' Set rows high one by one
        DIRA = 1 << Row
    
        temp = 0                                ' Reset temp variable to 0
        FOR column = 0 TO 3
          INPUT (column + 4)                    ' Set columns as inputs one by one
          temp = temp | (INB & (1 << column))   ' Poll column state and store in temp
        NEXT
    
        keypad = keypad << 4 | (Temp REV 4)     ' Store keypad value
      NEXT
    RETURN
    
    '
    [ Subroutine - Update ]
    ' Graphical depiction of keypad
    Update:
      DEBUG CRSRXY,0,7,
        "+---+---+---+---+",CR,
        "|   |   |   |   |",CR,
        "+---+---+---+---+",CR,
        "|   |   |   |   |",CR,
        "+---+---+---+---+",CR,
        "|   |   |   |   |",CR,
        "+---+---+---+---+",CR,
        "|   |   |   |   |",CR,
        "+---+---+---+---+"
    RETURN
    
    '
    [ Subroutine - Display ]
    ' Display button pressed in keypad graphic
    Display:
      IF KeyPad.BIT15 THEN  DEBUG CRSRXY, 02,08,"1"
      IF Keypad.BIT14 THEN  DEBUG CRSRXY, 06,08,"2"
      IF KeyPad.BIT13 THEN  DEBUG CRSRXY, 10,08,"3"
      IF Keypad.BIT12 THEN  DEBUG CRSRXY, 14,08,"A"
      IF KeyPad.BIT11 THEN  DEBUG CRSRXY, 02,10,"4"
      IF Keypad.BIT10 THEN  DEBUG CRSRXY, 06,10,"5"
      IF KeyPad.BIT9  THEN  DEBUG CRSRXY, 10,10,"6"
      IF Keypad.BIT8  THEN  DEBUG CRSRXY, 14,10,"B"
      IF KeyPad.BIT7  THEN  DEBUG CRSRXY, 02,12,"7"
      IF Keypad.BIT6  THEN  DEBUG CRSRXY, 06,12,"8"
      IF KeyPad.BIT5  THEN  DEBUG CRSRXY, 10,12,"9"
      IF Keypad.BIT4  THEN  DEBUG CRSRXY, 14,12,"C"
      IF KeyPad.BIT3  THEN  DEBUG CRSRXY, 02,14,"*"
      IF Keypad.BIT2  THEN  DEBUG CRSRXY, 06,14,"0"
      IF KeyPad.BIT1  THEN  DEBUG CRSRXY, 10,14,"#"
      IF Keypad.BIT0  THEN  DEBUG CRSRXY, 14,14,"D"
    RETURN
    
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-12 08:06
    Is that working code?
  • paulvalpaulval Posts: 13
    edited 2014-08-12 08:58
    that is the code for you 4x4 keypad
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-12 09:09
    Okay. I thought you were answering my reply before that. Did you see the message I posted asking about the code that was already suggested to you?
  • paulvalpaulval Posts: 13
    edited 2014-08-12 10:22
    Okay. I thought you were answering my reply before that. Did you see the message I posted asking about the code that was already suggested to you?
  • paulvalpaulval Posts: 13
    edited 2014-08-12 10:23
    yes, where would I insert that code?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-12 10:49
    The original code contained a subroutine to read the keypad and pass that information back to the main program. There were variables declared that were used in the subroutine. Those would go the same place they were in the original code.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-13 17:01
    Okay, well I decided to take things in a different direction. I will post back what I have later.
  • paulvalpaulval Posts: 13
    edited 2014-08-13 18:50
    Thank You Sir

    Paul
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-14 07:56
    Paul,

    I got my hardware assembled and code started and ran out of time. What I will be posting today doesn't use the example code at all. I actually decided against that the moment I looked at it.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-15 09:17
    Okay, phone calls, e-mail and forums were too much to get coding done yesterday, but I am back at it again today. Let's try for a win this time. =)
  • paulvalpaulval Posts: 13
    edited 2014-08-15 10:26
    Thank you, you are doing my a favor.

    Thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-15 12:48
    Paul,

    Attached is a simple program to do what you asked. Please note the schematic in the comments is different than the example code you have. I didn't write the original example and felt it was overly complex and wasted resources. The attached example uses the same I/O pin connections, except that there are 4 x 10K pull-down resistors instead of the 4 x 1K inline resistors. It is important to make these connections properly. My code uses a traditional row/column bit-scan and adds an offset to the key value until a key has been found. This value is the translated through the LOOKUP command to return the value of the key (or ASCII value in the case of the non-numeric keys). This example does not handle timeouts on the input, has minimal error checking and is hard-coded to a single key value or a 2-digit number (see comments in code). Note that the reason it took me so long today was I was playing around with using the * key as a backspace key, the C to clear the entry and the # to terminate input for up to a 5-digit number. In this case though I decided to stick to what you were trying to do since I didn't think you would be able to modify the full version. I hope this helps. I finished it during my lunch break. :cool:

    20140815_125445.jpg
  • paulvalpaulval Posts: 13
    edited 2014-08-16 11:24
    Mr. Savage,

    Your program worked. But how can I assign each value from 00 to 99 a different variable for instance 00 apples, 01 screws etc.

    Thank You,

    Paul
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-18 08:23
    Paul,

    You wouldn't assign each number to a variable. The number is returned in a variable already and you just need to say:

    IF num = 1 THEN
    IF num = 2 THEN
    etc.

    Depending on how many values you use there are other ways to do this. You'll also be limited by the memory of the BASIC Stamp as to how many words you can put into memory. If you need more you'll need a different model of BS2 or an external EEPROM to hold the data.
  • paulvalpaulval Posts: 13
    edited 2014-08-18 09:44
    Where in your program would you inset the above. I also purchased the propeller chip. please advise.

    Thank You,

    Paul
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-18 11:10
    Paul,

    Did you get the Private Message I sent you? As for where to check the characters, the main loop calls a subroutine to get the value of the number you type in. When it returns the variable specified contains that number. It is then displayed on the LCD. However you could instead have it print different messages based on the value. My example was designed to show you how to get a 2-digit value back from the keypad the way I felt the example code should have worked.
  • paulvalpaulval Posts: 13
    edited 2014-08-19 07:58
    Yes, I did. Please give me that information. Thank You
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-08-19 08:13
    I sent you a PM with the information.
  • Hello Mr. Savage.......
    I'm pavan aditya
    I'm trying to take up small module which displays the required text on the LCD when entered the repective code on the numeric pad.........
    If you dont mind can you assist me with a helpful code for it......please.....
  • PavanAditya,
    You will get better results by asking your question in one place rather than duplicating it in private conversations. It helps for you to specify what LCD and keypad you're using and whether you're using a Basic
    Stamp or Propeller for a controller. An example of what output you want to see on the LCD for a key or keys on the keypad would also help.
Sign In or Register to comment.