Menu

Help with Keypad Buttons (Arrays)

Help
Judah Ben
2020-10-19
2020-10-31
  • Judah Ben

    Judah Ben - 2020-10-19

    Hi, I'm working on a project and in the section I'm on, I am having problems.

    The project involves receiving a 4-digit input from a keypad and checking it against a previously stored 4-digit password in the EEPROM. This is just a part of the project.

    I found a little solace in the demo code of keypad lock but my project involves storing that 4-digit code (I would later tackle that) and checking against it unlike the demo code which already has the password "4242" hard-coded into it.

    From, the demo the way to do this would be storing the digits in an array. I'm finding it hard to understand how to do this. This is my major problem.

    I look through the demo code and I understand it except this part.

              'Store the pressed key in a list of pressed keys
              StoreKey PressedKey
    
               Sub StoreKey (In NewKey)
                   'Move the last 3 keys along in the array
                     KeyBuffer(1) = KeyBuffer(2)
                     KeyBuffer(2) = KeyBuffer(3)
                     KeyBuffer(3) = KeyBuffer(4)
                    dim KeyBuffer as string
                     cls
                     print KeyBuffer(4)
    

    This line is calling in the subroutine called StoreKey which I have also pasted below it. I don't understand however how PressedKey is supposed to relate to it. Does it accept that in as a parameter or what?
    This is my second problem

    Thanks a lot.
    Judah.

     
  • mmotte

    mmotte - 2020-10-20

    Judah,

    Looking at the demo. The "Do forever ...Loop" is gathering a "PressedKey" char by the person pressing a key and then sending it to storage in the keybuffer array. We want the last 4 digits entered so that is why the array has 4 spots. By putting the PressedKey in the last spot "KeyBuffer(4)" we are allowing ourselves "alignment" without figuring out if this is the first digit, second digit..last digit, we would need a counter. Here we just keep entering digits and when the last 4 match Then the lock opens. My partner at work used to confuse contractors by giving them a really long passcode to enter into the keypad at the door. The code was only 2 digits "53" but he would give them "123455432153" . All the other digits would fall off the end of the buffer but the "53" on the end would open the door.

    The code snippet you have shown is incorrect.

    '''Store a pressed key
    '''@param NewKey Key to store
      Sub StoreKey (In NewKey)
        'Move the last 3 keys along in the array
        KeyBuffer(1) = KeyBuffer(2)
        KeyBuffer(2) = KeyBuffer(3)
        KeyBuffer(3) = KeyBuffer(4)
    
        'Store the most recently pressed key at the end of the array
        KeyBuffer(4) = NewKey
      End Sub
    

    You are right the "NewKey " is a parameter of the "StoreKey" subroutine. So we are saving the last 4 keypresses. We have a NewKey to store. We need to shift the previous 3 keypresses out of the way and then finally store the new key press in "KeyBuffer(4). Then The "Do forever ...Loop" checks for a match with "Checkcode" subroutine. If you go through this routine by hand on paper you can realize how it works.

    I think it is important to understand this first.

    I understand that you want to enter a new secret code to match from the keypad. "Entering New Code" would be a branch subroutine that you have to get to from the keypad entry main routine. So you would leave the demo routines alone and make your own subroutine "NewCode" or something. How to get there? I don't know your keypad but maybe it has a * A B C D or # , so this could be something to check for in the "Do forever ...Loop" main routine. Then you would want a little security so you would check for another key before going to your "newCode" subroutine.
    You probably want to store the secret code in eeprom or flash so it don't go away when the power is off.

    When developing code sometimes I use an LED on a port pin that I flash when getting to different parts of the code. In this case when getting to the "NewCode" sub. This is just a diagnostic to help you know what is executing.

    GL
    Mike

     
  • mmotte

    mmotte - 2020-10-22
    'put in default CodeKey
    EPWrite 1, 4    'write at location 1 the number 4
    EPWrite 2, 2
    EPWrite 3, 4
    EPWrite 4, 2
    
    Sub CheckCode
        'Check each of the last 4 pressed keys to see if it matches the correct sequence.
        'The sequence is 4242, and can be changed by editing the If commands
        EPRead 1, keydig1
        EPRead 2, keydig2
        EPRead 3, keydig3
        EPRead 4, keydig4
        If KeyBuffer(1) = keydig1 Then
          If KeyBuffer(2) = keydig2 Then
            If KeyBuffer(3) = keydig3 Then
              If KeyBuffer(4) = keydig4 Then
    
                'If the correct sequence was entered, turn off the LOCKED output, and turn on the UNLOCKED output
                Set LOCKED Off
                Set UNLOCKED On
                for ResetBuffer = 1 to 4
                    KeyBuffer(ResetBuffer) = 255
                next
                wait 3 s
                'Then, exit from the subroutine to avoid the locking code below
                'this way it stays unlocked until you press anykey
                'Exit Sub
              End If
            End If
          End If
        End If
    
        'If the program gets to this point, then the code was incorrect. Lock again!
        Set LOCKED On
        Set UNLOCKED Off
      End Sub
    

    So I think you are confusing "Key" used as a button pushed and "Key" as codekey to matched OR the CodeKey being input to the keybuffer.

    You asked about EEProm storage and here are two blocks of code using EEProm.

    I do have the code for the keylock working but not with an LCD. I am able to enter a new codekey by pressing "star" and then "Pound" button and then the 4 buttons of the new codekey.
    I do not know your situation. Are you a student? i do not want to do your project by handing you all the code. I am willing to answer questions and help you.

    73
    Mike

     
  • Judah Ben

    Judah Ben - 2020-10-31

    Hello, Mike @mmotte.
    Thanks a lot for your very detailed responses.

    I'm sorry I haven't been able to get back on the project and back here to reply. Over the past few days, things took a drastic turn in my country over a couple of events and it's been terrible for everyone.

    I have just been able to resume working on the project today. And yes, I'm a student. This is my final year project work. I must say I am not too well grounded in this field as I have done only a few projects before this but I am keen on carrying out the project myself as it is well in line with my career choice.

    Thanks a lot for helping out thus far. I will get to work and reply back on how things are going.
    Judah.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.