4x4 KeyPad Project
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.
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
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.
They're below \/\/\/\/\/\/
Phil has written a little tutorial on how to post code in the forum. Here's his link.
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?
' {$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 ENDI'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
You did miss one question.
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.
Does the original demo code work?
Is your problem with the part you've modified?
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?
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.
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.
when i use the demo they work fine and im just customing the demo for my own use
No i haven't done that. That's what I'm stuck on the coding for the translation.
' {$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 RETURNOnce 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.
' {$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 RETURNI 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 RETURNIn the program I posted above, I had forgotten to include the square brackets around the [ "D#0*C987B654A321"]
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 ?
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 decodeThe lookup has the form, 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.
thanks
If you do that would be a great help.
binary-password-lock.
creating-a-password-combo-with-Pushbuttons