Shop OBEX P1 Docs P2 Docs Learn Events
absolute number — Parallax Forums

absolute number

antonisantonis Posts: 9
edited 2010-07-22 16:09 in BASIC Stamp
hello to everybody!!!!!!!!
i have a numeric kaypad and i enter a 4-digit code.

i have a master code store in a variable and i want to compare with tha and if is thet to do an action

example:
master_code=1234
when i enter the 4-digits i entered one by one and stored in a temporary variable and transfered to eeprom
so ihave in four eeprom places one digit & one digit & one digit & one digit (1 & 2 & 3 & 4)
how i can saidto bs2 take that four digit put in one variable and compare?

thank you!

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-04 06:12
    number = (digit1 * 1000) + (digit2 * 100) + (digit3 * 10) + digit4

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • metron9metron9 Posts: 1,100
    edited 2010-06-04 13:25
    or use a loop to directly test each number stored in memory against a stored array.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2010-06-04 19:36
    It's not effifient or a good idea to store incoming data directly into the EEPROM regularly. You do that when you're initially setting the code or something, but for general input and comparison you want to stick to RAM since the EEPROM cells have a limited write cycle lifetime.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    ·
  • metron9metron9 Posts: 1,100
    edited 2010-06-05 03:21
    I think though Chris if I may point out, input from a keyboard performed by a human for a numeric keypad used to accept a 4 digit code perhaps used a few times per day would last a lifetime even if only 4 eeprom locations were used.
    using a rotating algorithm with 40 bytes could last ten lifetimes.

    You should perhaps store the codes in eeprom and then store the input into ram space to compare though as Chris suggests. that way you can have many codes to compare input for different users for example.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2010-06-07 05:06
    metron9,

    You're correct in that the likelihood of this particular use isn't likely to cause any issues in and of itself, it tells me that programmer is getting comfortable using the EEPROM in place of RAM which could cause problems in many other situations. If you develop your code to use the right resources the first time then you can re-use it later, comfortable in the knowledge that it will work. =) You also have to think of the possibility of there being an error in the switch input of code that causes data to be written in a continuous loop. It's the little things that I notice sometimes. Take care all.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2010-06-07 07:12
    antonis,

    "...how i can saidto bs2 take that four digit put in one variable and compare?..."

    Why not store the incoming keypad data in one 16-bit variable (WORD)? ... If you can keep the value of each 'key' between 0 and 15, then on each new keypress you can just shift the data left by one nibble and enter the new key on the least significant nibble.


    Example Code

    Note: on the first itteration the DEBUG window should say "WRONG CODE!!!" ... following that after a false or 'simulated' key pad entry the DEBUG window will display "Code Matches!!"

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    key     VAR Nib
    oldkey  VAR Nib
    
    Master  CON $1234    '<- Master Code ; entered as a HEX value (notice the $ at the beginning
    Code    VAR Word
    
    '------------------------------------------------------
    '------------------------------------------------------
    'Test Only!!! - simulates key entry loop
    Code=Code<<4
    Code=Code+1    '<- same as entering in a "1"
    Code=Code<<4
    Code=Code+2    '<- same as entering in a "2"
    Code=Code<<4
    Code=Code+3    '<- same as entering in a "3"
    
    'Used to trigger Key entry loop below
    key = 4        '<- pre-loads the 'key' variable as if you entered a "4" on the keyboard.  
    oldkey = 0
    
    '------------------------------------------------------
    '------------------------------------------------------
    
    
    MainLoop:
      DEBUG HEX Master,TAB,HEX Code
    
      IF Master = Code THEN
         DEBUG TAB,"Code matches!!"
      ELSE
         DEBUG TAB,"WRONG CODE!!!"
      ENDIF
    
      DEBUG CR
    
    'Key Entry Loop
    '----------------------------
    ' Get new key value HERE
    '----------------------------
      IF key<>oldkey THEN
         W0=W0<<4
         W0=W0+key
      ENDIF
      oldkey = key
    '----------------------------
    
    GOTO MainLoop
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 6/7/2010 7:19:19 AM GMT
  • perception13perception13 Posts: 3
    edited 2010-07-22 16:09
    Beau Schwabe,

    your post
    Example Code

    Note: on the first itteration the DEBUG window should say "WRONG CODE!!!" ... following that after a false or 'simulated' key pad entry the DEBUG window will display "Code Matches!!"
    end of post

    I'm not sure I understand why its getting the correct code...
    How should it be wired?

    Thanks in advance
Sign In or Register to comment.