creating a password combo with Pushbuttons
Hello, I'm trying to create a sort of password combo using pushbuttons. Like PushBttn1, and PushBttn2. Say push pushbttn1 three times then pushbttn2 two times then pushbttn1 once then debug that pass is complete. If any other combo is pressed is it will debug incorrect.
I'm starting with this.
[noparse][[/noparse]code]
' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Byte
DO
DO WHILE IN0 = 0
PAUSE 1
LOOP
IF IN0 = 1 and counter < 3 THEN
PAUSE 250
counter = counter + 1
DEBUG"pressed."
IF counter = 3 THEN
DEBUG"done?"
DO WHILE IN1 = 0
PAUSE 1
LOOP
IF IN1 = 1 THEN
DEBUG"Bttn2 Pressed"
PAUSE 150
counter2 = counter2 + 1
IF counter2 = 2 THEN
DEBUG"Done"
STOP
ENDIF
ENDIF
ENDIF
ENDIF
LOOP
END
I'm starting with this.
[noparse][[/noparse]code]
' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Byte
DO
DO WHILE IN0 = 0
PAUSE 1
LOOP
IF IN0 = 1 and counter < 3 THEN
PAUSE 250
counter = counter + 1
DEBUG"pressed."
IF counter = 3 THEN
DEBUG"done?"
DO WHILE IN1 = 0
PAUSE 1
LOOP
IF IN1 = 1 THEN
DEBUG"Bttn2 Pressed"
PAUSE 150
counter2 = counter2 + 1
IF counter2 = 2 THEN
DEBUG"Done"
STOP
ENDIF
ENDIF
ENDIF
ENDIF
LOOP
END
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
Another way, easier to code, might be as follows:
Call the number of pushbuttons base.· Call the length of the password length.··To enter a password, you make length presses, each time pressing one of base buttons.
Using arithmetic with base base, the password can be thought of as a number represented by length base-ary digits.· That is, if there are two buttons and a password is five pushes, the password is a base-two (binary) number five digits long.· If there are ten buttons and a password is seven pushes, the password is a base-ten (decimal) number seven digits long.· Similarly for any other base and any other length.
Maintain a counter (call it total) long enough to hold a password.
Start out with total equal to zero.·
Number the buttons from 0 to (base-1).· Whenever a button is pushed, you multiply total by base and add the assigned number of the button that was pushed.· Then you check to see whether total equals the password.· If so, you unlock the door for five seconds, lock it again, and set total to zero.
Advantage:· you don't have to count pushes, or remember how far along the password-entry has progressed.· This makes the coding much simpler.
If no button is pushed for five seconds, you set total to zero.
If this is imperfectly clear, choose a base and length and simulate it on paper by following the above instructions step-by-step and it ought to become clear.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i@arrl.net
Post Edited (Carl Hayes) : 2/14/2009 5:04:25 PM GMT
"Chris's solution sounds good, except that if someone enters half a password, leaving the algorithm half completed, you'll be trapped."
Not necessarily... If you have a known password length and you set it up so that each key press creates a FIFO (first in first out) to the input password variable, then you can simply look for a match when the appropriate combination is met.
For example for simplicity, say you have a password that is 4 digits... "1011"
Say I enter the wrong code and enter "1001" .. the password variable would look something like this as I enter the data...
"0000" - Initialized value
"0001" - First Key Press
"0010" - Second Key Press
"0100" - Third Key Press
"1001" - Fourth Key Press
...If I continue and enter the correct code sequence of "1011" the password variable would look something like this as I continue to enter the data...
"0011" - Fifth Key Press
"0110" - Sixth Key Press
"1101" - Seventh Key Press
"1011" - Eighth Key Press <<<--- a simple IF/THEN comparison would indicate that this is the correct password.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.