Shop OBEX P1 Docs P2 Docs Learn Events
binary password lock. — Parallax Forums

binary password lock.

Microman171Microman171 Posts: 111
edited 2008-01-25 06:07 in BASIC Stamp
' {$STAMP BS2}
' {$PBASIC 2.5}

INPUT 0
INPUT 1
INPUT 2
INPUT 3



'HIGH 15
'PAUSE 2000
'LOW 15

no1       VAR     Byte
no2       VAR     Byte
no3       VAR     Byte
no4       VAR     Byte
pw1       VAR     Bit
pw2       VAR     Bit
pw3       VAR     Bit
pw4       VAR     Bit
digit     VAR     Nib

DO

  IF (IN0 = 1) THEN
    no1 = 1
  ELSE
    no1 = 0
  ENDIF
  IF (IN1 = 1) THEN
    no2 = 2
  ELSE
    no2 = 0
  ENDIF
  IF (IN2 = 1) THEN
    no3 = 4
  ELSE
    no3 = 0
  ENDIF
  IF (IN3 = 1) THEN
    no4 = 8
  ELSE
    no4 = 0
  ENDIF

  digit = no1 + no2 + no3 + no4

  IF (digit > 0) THEN
     DEBUG DEC digit, CR
  ENDIF

  IF (digit = 1) THEN
    pw1 = 1
  ELSE
    pw1 = 0
    pw2 = 0
    pw3 = 0
    pw4 = 0
  ENDIF
  IF (digit = 2 AND pw1 = 1) THEN
    pw2 = 1
  ELSE
    pw1 = 0
    pw2 = 0
    pw3 = 0
    pw4 = 0
  ENDIF
  IF (digit = 3 AND pw2 = 1) THEN
    pw3 = 1
  ELSE
    pw1 = 0
    pw2 = 0
    pw3 = 0
    pw4 = 0
  ENDIF
  IF (digit = 4 AND pw3 = 1) THEN
    pw4 = 1
  ELSE
    pw1 = 0
    pw2 = 0
    pw3 = 0
    pw4 = 0
  ENDIF
  IF (pw4 = 1 AND digit = 12) THEN
    HIGH 15
    PAUSE 1000
    LOW 15
    pw1 = 0
    pw2 = 0
    pw3 = 0
    pw4 = 0
  ENDIF



LOOP



I really need to tidy up this code. I have a really noobish way of the password lock. This is wired to a DTMF decoder outputting in binary. I need help making the DATA and READ commands to make a password lock. I have seen examples but I cant put them to my use. Brain overload. Somebody please help me to make this code more simple with a
password DATA "1, 2, 3, 4

at the start.

If anyone knows what I am talking about and can help please do smilewinkgrin.gif


Thanks heaps

Microman171

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 + 1 = 10 Base 2
2 + 2 = 10 Base 4
3 + 3 = 10 Base 6
4 + 4 = 10 Base 8
5 + 5 = 10 Base 10

Comments

  • ZootZoot Posts: 2,227
    edited 2008-01-23 22:05
    How about something like:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    GH 15
    'PAUSE 2000
    'LOW 15
    
    inPw VAR NIB
    pw VAR NIB
    eeAddr VAR WORD
    
    ' I'm presuming 4 bit passwords here, so you can use decimal 0-15 or binary 0-1111
    
    Passwords DATA %1111, 14, %1011, 9, $FF ' $FF terminates pw list
    
    
    Main:
    
      inPw = INA ' set pw = pins 0...3
      eeAddr = Passwords
      DO
         READ eeAddr, pw
         IF pw = $FF THEN EXIT
    
         IF pw = inPw THEN
           DEBUG "Yes, that is a correct password", CR
           HIGH 15
           PAUSE 1000
           LOW 15
           EXIT
         ELSE
           eeAddr = eeAddr + 1
         ENDIF
      LOOP
    
      GOTO Main
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Microman171Microman171 Posts: 111
    edited 2008-01-23 22:38
    Sorry. I ahev No clue what you just wrote... I have just tried it and it makes a continuous loop. Can you please explain how it works?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • ZootZoot Posts: 2,227
    edited 2008-01-24 00:31
    OK, here it is with more comments. I made a few presumptions based on your initial code:

    - pins 0-3 are inputs where a binary password is "entered"

    - input password is compared with password(s) in list

    - if there is match, make pin 15 high for 1 second

    - otherwise try again with input password

    The only thing I wasn't sure of is that I *think* you are not allowing %0000 to be a password, so you could check for that before entering the do loop. Up to you.

    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    inPw VAR NIB   ' password taken from input
    pw VAR NIB    ' password read from data
    eeAddr VAR WORD ' pointer to address in bytes of data
    
    ' I'm presuming 4 bit passwords here, so you can use decimal 0-15 or binary 0-1111
    
    ' below is a list of passwords that will "work". The list can be any length, the list ends
    ' with $FF -- i.e., $FF can't be a good password
    
    Passwords DATA %1111, %1110, %1011, %1001, $FF
    
    
    Main:
    
      inPw = INA ' copy the state of pins 0..3 to inPw
      IF inPw = 0 THEN Main ' no pw entered, try again
      eeAddr = Passwords  ' otherwise point eeaddr to start of data list
      DO
         READ eeAddr, pw   ' read the byte at eeaddr into pw
         IF pw = $FF THEN EXIT ' is the byte $FF? if so, then list is over, you are done, exit the loop
    
         IF pw = inPw THEN    ' does password from list match what was input?
           DEBUG "Yes, that is a correct password", CR ' yes, debug that it's good
           HIGH 15  ' set pin high
           PAUSE 1000 ' wait 1 second
           LOW 15 ' set pin low
           EXIT ' exit the loop
         ELSE  ' password did not match, so...
           eeAddr = eeAddr + 1   ' point to next byte in the data list
         ENDIF
      LOOP   ' and check the next byte
    
      GOTO Main  ' list ended or password match was found, so loop is over, start again
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 1/24/2008 12:39:08 AM GMT
  • Microman171Microman171 Posts: 111
    edited 2008-01-24 02:58
    Thanks heaps!! I'll be sure to try it later. I can actually understand it now.

    Thanks again

    Dan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • ZootZoot Posts: 2,227
    edited 2008-01-24 03:24
    Let us know how it works out. You might want to check out the Pbasic Manual and read up on READ and WRITE -- using WRITE allows your program to change the data at places like "Passwords".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Microman171Microman171 Posts: 111
    edited 2008-01-24 04:34
    is there anyway to get more than one digit? At the moment each numeral is 4 bit. I need a 4 digit password. The DTMF decoder outputs in 4 bit then I need to have 4 digits.

    Thanks for the help!!

    Dan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • ZootZoot Posts: 2,227
    edited 2008-01-24 06:50
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    inPw VAR Word   ' password built up  from digits of input
    pw VAR Word    ' password read from data
    eeAddr VAR WORD ' pointer to address in bytes of data
    i VAR Nib ' index counter
    
    
    ' below is a list of passwords that will "work". The list can be any length, the list ends
    ' with $FFFF -- i.e., $FFFF can't be a good password - that would be 16-16-16-16 anyway
    ' note that the digits are in hex -- i.e., these are BCD numbers where each nib is a digit
    Passwords DATA Word $1234, Word $9999, Word $3456, $FFFF
    
    
    Main:
    
      ' here you will need to do some work because I don't quite get how are you are readiing each
      ' digit, i.e., each set of 4 bits in to a digit in the PW
      FOR i = 3 TO 0 ' get 4 digits, this is backwards because if you were entered "7821" you'd want 7 in nib3, 8 in nib2, etc
         inPw.NIB0(i) = INA ' copy the state of pins 0..3 to inPw NIB i (each digit)
         PAUSE 300 ' or whatever you do between enters of each digit
      NEXT
     ' now you've got BCD numbers as "digits" in each nib position of the Word inPw
    
    
      IF inPw = 0 THEN Main ' no pw entered, try again
      eeAddr = Passwords  ' otherwise point eeaddr to start of data list
      DO
         READ eeAddr, Word pw   ' read the Word at eeaddr into pw
         IF pw = $FFFF THEN EXIT ' is the Word $FFFF? if so, then list is over, you are done, exit the loop
    
         IF pw = inPw THEN    ' does password from list match what was input?
           DEBUG "Yes, that is a correct password", CR ' yes, debug that it's good
           HIGH 15  ' set pin high
           PAUSE 1000 ' wait 1 second
           LOW 15 ' set pin low
           EXIT ' exit the loop
         ELSE  ' password did not match, so...
           eeAddr = eeAddr + 1   ' point to next byte in the data list
         ENDIF
      LOOP   ' and check the next byte
    
      GOTO Main  ' list ended or password match was found, so loop is over, start again
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Microman171Microman171 Posts: 111
    edited 2008-01-24 07:36
    Wow smile.gifsmile.gifsmile.gif thanks for all of the help! I'll be sure to try this when I get onto the other computer

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • Microman171Microman171 Posts: 111
    edited 2008-01-24 21:35
    So sorry. Im sure I have confused you by saying that it was binary. The output from the DTMF decoder is 4-bit binary. BCD in other words. That is converted to decimal as per my original code. I want to be able to store a password like this (if possible)
    password  DATA "1234" ' The password is 1234
    

    the password would be 4 sets of 4-bit binary outputted onto 0 - 3. This is actually going to be my science fair project. A cell phone controlled door latch. You ring up the phone connected to the BS2 and dial the code over the line. Each tone is converted to 4-bit binary. The BS2 need to be able to read each digit and check if they match the password/s stored in the BS2.

    You help is greatly appreciated

    Dan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • ZootZoot Posts: 2,227
    edited 2008-01-24 23:36
    That's exactly what I did above, only you don't do "1234" which would be ASCII, you do $1234 which puts your binary digit in the right places. The code I posted above reads the 4bit binary number (from INA) to each nib, i.e.

    INA 1st time - bits 12..15 (digit 4)
    INA 2nd time - bits 8..11 (digit 3)
    INA 3rd time - bits 4..7 (digit 2)
    INA 4th time - bits 0..3 (digit 1)

    Since the "good" passwords are also stored as BCD, e.g., $4321, then you can just match pw to inPw as above.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Microman171Microman171 Posts: 111
    edited 2008-01-25 03:26
    thanks!! Does that mean it reads the code backwards? so by typing $4321 it means 1234 password??

    Greatly appreciated

    Dan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • Microman171Microman171 Posts: 111
    edited 2008-01-25 03:46
    Actually... I ahve figured out why this wont work for me. Each decimal come in as the 4-bit as you know. The problem is in between these digits could be a pause of however long it takes a person to enter the 4 digits. This means there could be the 4-bit code 0000 comeing through all the time unless another digit comes in. Maybe I will just have to stick with the original code... Just seems messy is all. If I leave the Stamp reading the inputs with my old code It would be like this:

    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    1
    1
    1
    1
    0
    0
    0
    0
    0
    0
    2
    2
    2
    2
    2
    2
    2
    0
    0
    0
    0
    0
    3
    3
    3
    3
    3
    3
    0
    0
    0
    0
    0
    0
    0
    0
    0
    4
    4
    4
    4
    4
    4
    4
    light
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
  • ZootZoot Posts: 2,227
    edited 2008-01-25 04:49
    Somebody said...
    IF inPw = 0 THEN Main ' no pw entered, try again

    This takes care of infinite 0's. But what you would need to is add a counter in -- if 4 digits are received, then check.

    Somebody said...
    $4321 it means 1234 password??

    No, if you counted 0 through 3 then it *would* be backward.... it's like this:

    If I type $4 first, really that's the MOST SIGNIFICANT digit -- which would be NIB3 (the highest four bits of the pw). Humans *read* digits most-significant to least-significant; but if you are INDEXING digits, 0 is the LEAST significant. Try it with a debug and see:

    
    inPw VAR WORD
    digCntr VAR NIB
    
    FOR digCntr = 3 TO 0
      DEBUG CLS, "Enter digit ", DEC digCntr, " (from 0 to 9)", CR
      DEBUGIN DEC inPw.NIB0(digCntr)
    NEXT
    
    DEBUG CR, CR, "You entered the 4-digit number:", HEX4 inPw
    
    DO:LOOP
    END
    
    
    



    Here's my earlier code with a digit counter. If it were me, I would put in another counter to *roughly* time things. If you don't get 4 digits in 30000 ticks or some such number, reset the digit counter (otherwise the Stamp could wait years for the 4 digit).

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    inPw VAR WORD   ' password built up  from digits of input
    pw VAR WORD    ' password read from data
    eeAddr VAR WORD ' pointer to address in bytes of data
    digCntr VAR NIB
    
    
    ' below is a list of passwords that will "work". The list can be any length, the list ends
    ' with $FFFF -- i.e., $FFFF can't be a good password - that would be 16-16-16-16 anyway
    ' note that the digits are in hex -- i.e., these are BCD numbers where each nib is a digit
    Passwords DATA WORD $1234, WORD $9999, WORD $3456, $FFFF
    
    
    Main:
    
      ' here you will need to do some work because I don't quite get how are you are readiing each
      ' digit, i.e., each set of 4 bits in to a digit in the PW
      FOR digCntr = 3 TO 0 ' get 4 digits, this is backwards because if you were entered "7821" you'd want 7 in nib3, 8 in nib2, etc
         DO
           ' you could put a word sized counter here; if the counter exceeds some number before you get all 4 digits
           ' then back to main and start all over
         LOOP WHILE INA = 0 ' keep looping until you get non-zero input
         inPw.NIB0(digCntr) = INA ' copy the state of pins 0..3 to the correct nib of inPw
         PAUSE 100 ' or whatever you do between enters of each digit
         
      NEXT
     
     ' now you've got BCD numbers as "digits" in the correct nib positions of inPw
      eeAddr = Passwords  ' otherwise point eeaddr to start of data list
      DO
         READ eeAddr, WORD pw   ' read the Word at eeaddr into pw
         IF pw = $FFFF THEN EXIT ' is the Word $FFFF? if so, then list is over, you are done, exit the loop
    
         IF pw = inPw THEN    ' does password from list match what was input?
           DEBUG CLS, "Yes, that is a correct password", CR ' yes, debug that it's good
           HIGH 15  ' set pin high
           PAUSE 1000 ' wait 1 second
           LOW 15 ' set pin low
           EXIT ' exit the loop
         ELSE  ' password did not match, so...
           eeAddr = eeAddr + 1   ' point to next byte in the data list
         ENDIF
      LOOP   ' and check the next byte
    
      GOTO Main  ' list ended or password match was found, so loop is over, start again
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 1/25/2008 4:54:54 AM GMT
  • Microman171Microman171 Posts: 111
    edited 2008-01-25 06:07
    [noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse][noparse]:D[/noparse] I am SOOOOOO happy!! Thank you SOOO much for all of your help! I have finally got it working. It works exactly as it should. Thank you heaps.

    [noparse]:D[/noparse]

    Dan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 + 1 = 10 Base 2
    2 + 2 = 10 Base 4
    3 + 3 = 10 Base 6
    4 + 4 = 10 Base 8
    5 + 5 = 10 Base 10
Sign In or Register to comment.