Shop OBEX P1 Docs P2 Docs Learn Events
creating a password combo with Pushbuttons — Parallax Forums

creating a password combo with Pushbuttons

syberiumsyberium Posts: 1
edited 2009-02-15 08:20 in BASIC Stamp
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

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-13 16:43
    If you’re only using two buttons you could call one Button 0 and the other Button 1. You could create a ‘password’ in a DATA statement using ones and zeros to represent your button codes. Write a small loop that compares each press to the next state in the EEPROM until the loop completes at which time you do you event. If at any point there is a mismatch the counter resets and the sequence starts over. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-02-14 16:50
    Chris's solution sounds good, except that if someone enters half a password, leaving the algorithm half completed, you'll be trapped.· You need a timeout -- say, if no button is pushed for five seconds, the sequence restarts from the beginning (waiting for first push).

    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
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2009-02-15 07:50
    Carl Hayes,

    "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.
  • SRLMSRLM Posts: 5,045
    edited 2009-02-15 08:20
    A password longer than 4 bits would be good if you're going to use this in a setup where security matters. Online vendors use 128 bit encryption (granted, the application is different), but you can use up to 16 bits in a single variable (65,536 values).
Sign In or Register to comment.