New guy, need password-type assistance
BasicCant
Posts: 6
Hi everyone,
I searched around the forums and found a lot of incomplete threads that didn't help me too much. I'm pretty new to BS2, and I'm trying to accomplish password storage, and comparison. I am aware of the need for time-outs and other security measures with passwords. I am fairly confident I can work those out on my own once I have the basic password operation working. What I need help with is how to store a string of inputs and compare that to a specified 'correct' string.
I am using a BS2 board and the 4x4 membrane keypad. I have cut out the necessary parts of the keypad example code to begin with. As a reference it is here:
My questions are:
Where should I add code to start reading the keypad inputs?
Do I store the 'keypad' value to a second variable? Like concatenate them into a string? How would that work?
Is LOOKUP the best way to compare the strings? How might this look coded?
I'll be watching this thread closely as I need to finish this objective within a day or two. I'll do my best to aid anybody willing to help with follow-ups.
Thankyou in advance, everybody.
I searched around the forums and found a lot of incomplete threads that didn't help me too much. I'm pretty new to BS2, and I'm trying to accomplish password storage, and comparison. I am aware of the need for time-outs and other security measures with passwords. I am fairly confident I can work those out on my own once I have the basic password operation working. What I need help with is how to store a string of inputs and compare that to a specified 'correct' string.
I am using a BS2 board and the 4x4 membrane keypad. I have cut out the necessary parts of the keypad example code to begin with. As a reference it is here:
'========= Keypad Password ====================================== password DATA "1234" 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 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 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
My questions are:
Where should I add code to start reading the keypad inputs?
Do I store the 'keypad' value to a second variable? Like concatenate them into a string? How would that work?
Is LOOKUP the best way to compare the strings? How might this look coded?
I'll be watching this thread closely as I need to finish this objective within a day or two. I'll do my best to aid anybody willing to help with follow-ups.
Thankyou in advance, everybody.
Comments
Disregard the deadline I mentioned, but did you have any input besides advice? This seems like it would be fairly elementary for most members of this board.
FYI...there are many, many users here that are more than happy to help you out. What we generally don't do as a community is provide a finished project. Showing someone "how to fish" is usually the case.
Large lists of items to tackle and short time frames are difficult and will most likely not receive much attention.
That said...
1) Of the code you posted (and thank you for using the [code] tags), does it work as you expect?
2) Information on the keypad would be nice
3) Have you constructed any kind of flow chart, flow diagram to help visualize the end result?
4) What is your background on the BS2? Newbie, some?
All this will help formulate a response.
Second, I am assuming you mean the 4x4 Matrix Membrane Keypad pn 27899. There is example code for the basic stamp at http://www.parallax.com/product/27899. Use that as a starting point.
In response to your other questions:
Where should I add code to start reading the keypad inputs?
A common practice is to have a subroutine that performs all the required functions (scanning, debouncing, key decoding, etc) and returns a code for each valid key press or an error code for multiple or no key-presses. Your main program would call this subroutine when it needs to read a key.
Do I store the 'keypad' value to a second variable? Like concatenate them into a string? How would that work?
One way is to read all the numbers and store them in a string variable. Once you have read in and stored all of the characters you compare them to a table of valid passwords to see if there is a match.
Is LOOKUP the best way to compare the strings? How might this look coded?
Lookup is one way to compare the strings. Download the Basic Stamp manual and Basic Stamp 2 application notes (links below) for examples and more info.
http://www.parallax.com/downloads/basic-stamp-manual
http://www.parallax.com/downloads/basic-stamp-2-app-notes
DJ -
I respect the "how to fish" approach as mentioned.
1) Of the code you posted (and thank you for using the [code] tags), does it work as you expect?
2) Information on the keypad would be nice
3) Have you constructed any kind of flow chart, flow diagram to help visualize the end result?
4) What is your background on the BS2? Newbie, some?
1) Yes the code I posted works. It is the example code from Parallax with the diagram portions removed only.
2) It is as kwinn assumed, sorry I wasn't more specific. I forget that most users here probably use/fabricate a massive range of products. It is the 4x4 Membrane Keypad found here: http://www.parallax.com/product/27899
3) I have not created one specific for the keypad operation, I have one for the entire project. But essentially I was thinking:
- Wait for keypad input
- Save input (considering the WRITE command to EEPROM)
- Use "#" input as terminating condition
- Compare with saved password
- Conditionals based on correct or incorrect
4) I have only been experimenting with BS2 for about two months now. I have progressed through the entire Whats A Microcontroller? text.
kwinn -
I plan to queue the password-entry action with a subroutine.
Thanks for the links to those downloads, I'm giving them a read-over now.
---
Anyway, I'll try to ask less open-ended questions. Also, in the above code, I realize the comments wrapped and made things hard to read. Sorry.
Would it make sense to add a WRITE command into the ReadKeypad subroutine above that would copy the BIN16 value into an EEPROM address? Then I was planning to use READ to call and compare the entries. Is there a conversion process I need to conduct before saving?
Thanks again for being patient with me. My inexperience with this hardware is making it difficult for me to know how to ask my questions.
Thanks Genetix! I had v3.0 and they apparently omitted this. This is certainly helpful.
Also, I realized the 'keypad' variable will return powers of 2 if converted to decimal. I can use this to identify which key is pressed. All I need to work out now is some sort of loop that stores the 5 consecutive entries into the EEPROM for comparison.
Why would you want to store the keypad entries in eeprom? Are these for the table of valid passwords?
Yes. Trying to create some code that will write one of the keypresses to a variable, then read it back to the debug window so that I can confirm it stored/reads correctly.
Then I'll expand on it to store a list of 4 values. And then fit it into a loop so that it will scan continuously for user inputs.
End result is: I want to send the program into a state that will read keypad entries of 4 digits and confirm it matches a password. There may be simpler ways of doing this than the approach I'm taking, but I am -so far- unaware of them.
Once you have the keypad routine returning the proper code for each key press add a getpassword subroutine that calls the keypad routine four times and stores each code in an array or string. Change the main program loop so it now calls the getpassword routine and sends the resulting key code to the debug terminal.
Once you have everything up to this point you can continue to add subroutines and calls to perform the rest of the functions you need.
Next, add the WAM password code (remember the WAM program uses the keyboard)
Then you expand upon it. It's always best to start with working code and then it modify it to meet your needs.