Shop OBEX P1 Docs P2 Docs Learn Events
4x4 KeyPad Project — Parallax Forums

4x4 KeyPad Project

jayzdabossjayzdaboss Posts: 18
edited 2012-01-30 10:37 in BASIC Stamp
I just need help programming my BASIC STAMP so I can see what I'm clicking on the keypad on the 7 segment LED for a pass code entry system. The list below is the parts list.

List:
4x4 KeyPad - http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/keypad/List/0/SortField/4/ProductID/739/Default.aspx
7-Segment LED Display - http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/7/List/0/SortField/4/ProductID/388/Default.aspx
1K Resister

If you can help that will be great.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-22 12:12
    I see in my old BS2 notes that a 470 Ohm resistor is often used to drive a LED. So you'll want to use seven or eight (if the decimal point lights up) of these resistors to drive the segments. I know there are instructions on using a 7-segment display in one of Parallax's BS2 or Boe-Bot books.

    Have you tried the demo code on the keypad product page?

    The book "What's a Microcontroller" (a free pdf version is available) will help you get going on being able to use input and send output (to the 7-segment display) using the BS2.
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-22 13:28
    Duane Degn wrote: »
    I see in my old BS2 notes that a 470 Ohm resistor is often used to drive a LED. So you'll want to use seven or eight (if the decimal point lights up) of these resistors to drive the segments. I know there are instructions on using a 7-segment display in one of Parallax's BS2 or Boe-Bot books.

    Have you tried the demo code on the keypad product page?

    The book "What's a Microcontroller" (a free pdf version is available) will help you get going on being able to use input and send output (to the 7-segment display) using the BS2.

    I'm trying to do a pass code entry system and im trying to get keypad to enter the code and i have no idea how im suppose to code that.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-22 13:35
    Duane Degn wrote: »
    Have you tried the demo code on the keypad product page?
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-22 14:54
    Yes i have and this is what I've gotten to.

    They're below \/\/\/\/\/\/
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-22 16:43
    Using quotes instead of code blocks if a common early error when posting code.

    Phil has written a little tutorial on how to post code in the forum. Here's his link.

    attachment.php?attachmentid=78421&d=1297987572


    You can also use code blocks to post the output from the Stamp's debug window.

    I think you can copy and paste text from the debug window into the forum editor window.

    I don't have the Basic Stamp editor installed on this computer. As I've mentioned before, I generally use Propellers in my projects.

    Is the code you posted the original demo program, or have you modified it?

    Did it work the way it's supposed to? (I shouldn't have to ask this.)

    What Basic Stamp board/kit are you using? What Basic Stamp books do you have? Have you learned to use the Basic Stamp to light a LED?

    Have you found an example of using the 7-segment display with a BS2 yet?
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-23 04:51
    oh alright thanks..
    ' {$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
    
    
    '-----[ PassCode Entry ]----------------------------------------------------------
    ' Entering passcode
    
    
    counter     VAR   Word                       ' Counter Variable
    Password    DATA  "1234"                     ' Store "secret" password here.
    index       VAR   Nib                        ' Index variable.
    userEntry   VAR   Byte(4)                    ' Store user entered password.
    
    
    
    
    DO
    
    
    DEBUG "Enter password: "                                   ' User instructions
    DEBUGIN STR userEntry \4                                   ' Get user input password.
    FOR index = 0 TO 3                                         ' Check array against DATA
    READ Password + index, keypad                              ' Get next password char
    IF keypad <> userEntry(keypad) THEN EXIT                    ' Compare to user input,
    NEXT                                                       ' exit if not equal.
    IF index <> 4 THEN                                         ' If exit, then index not equal
    DEBUG CR,"Password not correct.", CR                       ' to 5 and pass is not correct.
      DO                                                       ' Alarm is ON
      FREQOUT 5, 500, 3000                                     ' Creation of a beeping sound
      HIGH 13                                                  ' Flashing LEDs
      LOW 12
      PAUSE 100
      FREQOUT 5, 500, 3000
      HIGH 12
      LOW  13
      LOOP
    ENDIF
    LOOP UNTIL index = 4                                        ' Only get out of loop when  ' index = 5.
    
    
    DEBUG CR, "Password is correct;", CR,                       ' Program can move on when
                          "program can continue..."             ' password is correct.
    
    
    HIGH 13
       FOR counter = 1 TO 150                                   ' "Door Opens" (Sliding door)
       PULSOUT 14, 1200
       PAUSE 20
       NEXT                                                     ' Light ON for 5 sec
    PAUSE 5000
    LOW 13                                                      ' Then off
       FOR counter = 1 TO 150                                   ' "Door closes" (Sliding door)
       PULSOUT 14, 300
       PAUSE 20
       NEXT
    END
    

    I've modified it to work with the pass code portion of the code but it seems that when i tip something it doesn't enter it to the password

    No it doesn't work the way its suppose to.

    This is the kit i have: http://www.parallax.com/Store/Microcontrollers/BASICStampProgrammingKits/tabid/136/CategoryID/11/List/0/SortField/0/Level/a/ProductID/789/Default.aspx

    and yes i have learned to use the BASIC STAMP to light a LED.

    Yes i just dont know how to integrate the code so when i press 1 on the key pad it will show up on the 7- segment display
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-23 06:52
    Thank you for the information. It helps me to know what to ask or point out next.

    You did miss one question.
    Duane Degn wrote: »
    Have you found an example of using the 7-segment display with a BS2 yet?

    I found an example on page 165 of WAM v2.2.

    Have you gone through (entered the code and ran the programs and also tried the variations they suggest) this example?

    Can you enter a number in the terminal and have it show up on the 7-segment display?

    With your keypad code, can you press a key and have it show up in the debug window?

    If you can answer yes to the two last questions, you should be close to your goal of pressing a key (on the keypad) and having it show up on the 7-segment display.

    I noticed they use 1000 Ohm resistors and not 470 Ohm to light the 7-segment display. I'd use the 1000 Ohm resistors as the book suggests.
    jayzdaboss wrote: »
    I've modified it to work with the pass code portion of the code but it seems that when i tip something it doesn't enter it to the password

    Does the original demo code work?

    Is your problem with the part you've modified?
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-23 07:22
    Yes i have.

    no i cant

    no it dosent show up on the debug i think theres something i need to at or remove for it to show up on the debug

    i use 1k Ohms as well

    yes the demo dose work but not with the pass code entry

    the part i modified dont show anything on the debug

    im kinda stumped lol
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-23 15:54
    jayzdaboss wrote: »
    Yes i have.

    no i cant

    no it dosent show up on the debug i think theres something i need to at or remove for it to show up on the debug

    i use 1k Ohms as well

    yes the demo dose work but not with the pass code entry

    the part i modified dont show anything on the debug

    im kinda stumped lol

    I keep hoping someone who uses the BS2 will jump in to help.

    jayzdaboss, Please don't make me work so hard to try to help. I don't want to go back through my earlier post to see what answer goes with which question. "no i cant" doesn't tell me much.

    Please provide more detail about what you can and can't do with the Basic Stamp so far.

    Is your goal the same as your original post of wanting the key you press to display with the 7-segment?
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-23 19:07
    My goal is to have a pass code entry system where I can tip in the pass with a keypad and see what I'm tipping on the 7segment display. Then when I tip in the correct pass word it unlock (unlocks with servo and a pull door lock ) a pull out drawer that I can open and have my personal belongings in.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-01-24 06:29
    What I would do is focus on the ReadKeypad subroutine. First make sure the column sweeping is working correctly. To do that put a PAUSE statement in there to slow it down. Then use an LED with a resistor to act as a logic probe. Check each column pin to make sure that it lights in sequence. If that works then force the state of a specific row pin by bringing it high via a 10K resistor tied to +5 volts.

    Basically I'm guessing there's something amiss with your wiring or the row column mapping on the keypad. Also posting a high res picture of your setup may help.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-24 09:15
    Martin,

    The data from the keypad is a binary word with one bit for each key, whereas the data required by the keypad decoder is a string of ascii codes. A crucial step is to make the translation from one form to the other. I don't see that in the code you posted. Have you done that? There would be several ways to approach it, but the one that comes to mind uses the
    value = NCD keypad
    operator to change the keypad variable into an integer from 1 to 16, followed by a
    LOOKUP value-1,["D#0*C9.........."],userEntry
    to convert that to the ascii code corresponding to each key.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-01-24 10:07
    Tracy Allen, that's a good point also, but it is not my code, but the original poster.
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-24 12:52
    Martin_H,

    when i use the demo they work fine and im just customing the demo for my own use
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-24 13:31
    Tracy Allen,

    No i haven't done that. That's what I'm stuck on the coding for the translation.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-25 00:17
    It's hard to do a mash-up of two programs like this. You quickly realize that you have to understand enough to take them apart and to put them back together in your new way. Here is a snippet that might help.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    keypad VAR WORD
    asciicode VAR BYTE
    
    DO
      DO
        GOSUB readkeypad
      LOOP WHILE keypad=0    'wait here when key IS NOT pressed
      PAUSE 64
      DO 
        GOSUB readkeypad
      LOOP while keypad>0   ' wait here  when key IS pressed
      PAUSE 64
    
      ' arrive here with a keypad value
      ' convert the keypad value to a corresponding ascii code
      LOOKUP NCD keypad - 1, "D#0*C987B654A321", asciicode
     
      DEBUG asciicode  ' as the program runs, see if the codes are correct
    
    LOOP
    
    readKeypad:
    '... as in demo
    RETURN
    

    Once you know the program is decoding the correct ascii characters, it will be possible to merge it into the passcode routine. The above routine provides debouncing, in the sense that each character printed requires that you have to press and release a key. The decoding step translates the keypad result into one of 16 ascii codes. You can look up the LOOKUP instruction in the Stamp manual, also the NCD operator. Don't hesitate to ask for clarification and help for a next step after you play around with it a bit.
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-26 09:06
    I had to modify it a little because it wasn't working at all. This is what i cam up with so far.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    keypad VAR Word
    asciicode VAR Byte
    row         VAR  Nib                        ' Variable space for row counting
    column      VAR  Nib                        ' Variable space for column counting
    keypadOld   VAR  Word                       ' Variable space to store old keypad output
    temp        VAR  Nib                        ' Variable space for polling column states
    
    
    
    
    DO
      DO
        GOSUB readkeypad
      LOOP WHILE keypad=0    'wait here when key IS NOT pressed
      PAUSE 64
      DO
        GOSUB readkeypad
      LOOP WHILE keypad>0   ' wait here  when key IS pressed
      PAUSE 64
    
    
      ' arrive here with a keypad value
      ' convert the keypad value to a corresponding ascii code
    
    
      LOOKUP NCD keypad, ["D, #, 0, *,C ,9 ,8, 7, 6, 5, 4, 3, 2, 1"], asciicode
    
    
      DEBUG asciicode  ' as the program runs, see if the codes are correct
    
    
    LOOP
    
    
    ReadKeypad:
      keypad = 0
      OUTL   = %00000000                        ' Initialize IO
      DIRL   = %00000000
      FOR row = 0 TO 3
        DIRB = %1111                            ' Set columns (P7-P4) as outputs
        OUTB = %0000                            ' 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
          temp = temp | (INB & (1 << column))   ' Poll column state and store in temp
        NEXT
        keypad = keypad << 4 | (Temp REV 4)     ' Store keypad value
      NEXT
    RETURN
    

    I just need help with this peace because all it right when I tip in the keypad is D.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-26 11:57
    Please explain what you mean by "I just need help with this peace because all it right when I tip in the keypad is D. "

    If your native language is not English, I understand, but more information about what you tried and what worked or didn't work will help to resolve the problem.

    I don't have your keypad set up, but I did try out the decoding part, as follows:
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    keypad VAR WORD
    keycode VAR BYTE
    idx VAR NIB
    
    FOR idx=0 TO 15
      keypad = DCD idx  ' DCD is the inverse of NCD
      GOSUB decode
      DEBUG CR,BIN16 keypad,TAB,keycode
    NEXT
    STOP
    
    decode:
      LOOKUP NCD keypad - 1, ["D#0*C987B654A321"], keycode
      RETURN
    

    In the program I posted above, I had forgotten to include the square brackets around the [ "D#0*C987B654A321"]
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-26 12:57
    Explanation:
    What ever I click on the key pad it shows up as a "D" in the debug. It is my native language i was just half asleep when i wrote it.

    I found that out when I saw it.

    Why do I need to put a - 1 ?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-26 14:33
    I goofed the pin test, sorry, but that happens when writing code when half asleep, doesn't it? The criterion should be UNTIL, not WHILE... You see, the idea is that the program waits until no key is pressed, and then it waits for a key to be pressed and decodes the key, and then back to waiting for the key to be released. That way, you only get one time around for each key press. The way I had it before, with WHILE, it would always pass zero to the decoding routine, and you always saw a "D".
    DO
        GOSUB readkeypad
        LOOP UNTIL keypad=0   ' wait here  until key IS NOT pressed
      PAUSE 64
      DO
        GOSUB readkeypad
        LOOP UNTIL keypad>0    'wait until a key is pressed
      PAUSE 64    ' now pass keypad value on to decode
    


    The lookup has the form,
    LOOKUP NCD keypad - 1, ["D#0*C987B654A321"], asciicode
      DEBUG CR,BIN16 keypad,TAB,asciicode
    
    There should not be commas between the letters. The -1 is necessary because the value of NCD keypad will be zero when no key at all is pressed, and 1 when the key attached to "D" is pressed, 2 for "#" etc.. The "D" is at offset zero in the string.

    I stuck in a changed DEBUG too, so that you can see also what the keypad value is as well as the ascii code, to help with debugging.
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-27 08:18
    Yeah I understand. I'll try the newly referbushed code now. Thank you for not just handing me the code and saying here you go and instead you actually tell me what was wrong and explaining it to me.

    thanks
  • jayzdabossjayzdaboss Posts: 18
    edited 2012-01-27 12:51
    Do you know where I can look for info on pass code systems or where I can read up on it so i can integrator it in to the code you so nicely came up with for me .... don't want you doing all the work lol.

    If you do that would be a great help.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-30 10:37
    Jayz, are you making progress on this? It is up to you how you want to make it work, how tamperproof, or how easy to remember, often opposing demands. You could have the "#" key frame every entry, for example, or you could simply ask that the right sequence of numbers be entered without regard to framing, or you could add additional tricks like time durations to the mix. Here are a couple of threads you might find helpful...

    binary-password-lock.

    creating-a-password-combo-with-Pushbuttons
Sign In or Register to comment.