Keypad Entry with Multiple PIN Numbers

Hi.

I'm quite new to this but I hope that someone can help me please?

I am making a basic keypad entry system.

The code is below:

/*

       This sketch takes input from a keypad for a PIN code.  
       
       If the code is entered correctly, it turns on a GREEN LED to simulate access granted.                                          
                                                                                     
       If an invalid code is entered, the RED LED is lit.                                                         

*/


#include <Keypad.h>          // Include the Keypad library.


#define GREENLED 10    // Define the Green LED as Digital Pin 10.
#define REDLED 11      // Define the Red LED as Digital Pin 11.


char* ourCode = "1974";      // Set the required PIN code.
int currentPosition = 0;     // Keeps track of the position of the code entered.



const byte rows = 4;          // Define the amount of rows and columns for the keypad.
const byte cols = 4;

char keys[rows][cols] = {      // Define the characters of the keypad.
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[rows] = {2,3,4,5};    // Define the pins connected to the keypad.
byte colPins[cols] = {6,7,8,9};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);      // Initialize the keypad.



void setup()
{
    pinMode(REDLED, OUTPUT);        // Set up the LED pins as outputs.
    pinMode(GREENLED, OUTPUT);
    digitalWrite(REDLED, LOW);      // Turn the LEDs off.
    digitalWrite(GREENLED, LOW);
  
    
       
}

void loop()
{
   int l; 
  
   char key = keypad.getKey();      // Get the key press from the keypad.
  
   if (int(key) != 0) {                    // Check to see if there is a value present.
     
     for (l=0; l<=currentPosition; ++l)
    { }
    
    if (key == ourCode[currentPosition])        // If the value is correct, increase he code position by one.
      {
        ++currentPosition;
        
        if (currentPosition == 4)                // If all four parts of the code are correct, unlock the door.
        {
          unlockDoor();
          currentPosition = 0;
        }
      } 
     
     else {
        invalidCode();                           // If any parts of the code are wrong, do not open the door.
        currentPosition = 0;
      }
    
    
  }
}

void invalidCode()
{
  digitalWrite(REDLED, HIGH);          // Code to run when an incorrect code is entered.
  delay(5000);
  digitalWrite(REDLED, LOW);
  
}


void unlockDoor()
{
  digitalWrite(GREENLED, HIGH);        // Code to simulate unlocking the door.
  delay(5000);
  digitalWrite(GREENLED, LOW);
  
}

This all works fine for now.

What I would like to be able to do though, is have more than one PIN number.
I would like people to be able to have their own PIN number.
Is this possible?
Would my sketch be able to check more than one PIN number?

The reason for this, eventually this will be on a Leonardo and it will log the time and person to a PC.

Thanks in advance for any help.

It's possible, but requires a fundamental change to the way you validate PINs. Currently you validate each input against the PIN and allow the user to enter an endless sequence of numbers until you get four consecutive numbers that match the expected value. That's very unsecure, and in any case won't work when you need to support multiple PINs.

Instead I suggest you separate out entering the PIN from validating it. Require the user to enter a PIN of the expected length before you validate it. I suggest you apply a timeout between key presses so that if an incomplete PIN is entered the system will clear itself back down after a little while. Store the entered PIN as a null-terminated char array.

Store the valid PINs in an array of strings (null terminated char arrays).

When the whole PIN has been entered you can validate it by looping through each of the valid PINs and using strcmp() to test whether the entered PIN matches that entry.

It would be sensible to introduce a minimum inter-key delay, and lock the keypad out for a while after each failed attempt, to increase the time needed for a 'brute force' attack.

You should consider how you are going to configure and maintain the set of PINs. In my opinion it would be more sensible to do the PIN validation and logging on an attached PC and just use the Arduino as an interface to the keypad and display etc; with this approach, the valid PINs would be defined on the PC (where they are easy to display and change) and the Arduino part can be relatively dumb.

store all of the possible PINs into one array

process all four keypad entires sequentially into an array

compare the entered number against the entire array.

something like this pseudo:

int totalUsers = 8;
int inputArray [4];
int storedPins [totalUsers] [4] = {
  {1, 2, 3, 4},
  {2, 3, 4, 5},
  etc...

loop()

while(waiting for a complete pin to be entered)
   process entries into array
//
for (int i =0; i < totalUsers; i++); //index through the total users 
  for (j = 0; j < 4; j++) check each digit for ==

//

Thank you very much for the replies.
I really do appreciate it.

I understand what you are both saying but, being a total newbie, i'm not sure yet how to implement it.
I am still at the stage where I cobble together code from various places and then try to learn how and why it works.

At least I now have an idea of what is needed and I will try to learn further.

Thank you very much for your time.