HOW TO USE KEYPAD FOR MAKE A CONTROL ACCESS SISTEM

Hello Everybody,

I have one problem, I would like to do one sistem for access controle, but I don’t know how to do the comunicate between keypad and EEPROM.
Because in one control access is necessary to have passwords, I would like to know how:

  1. Put one password in EEPROM whith 6 digits or more;

  2. Digit the password with Keypad and Read it on Memory and compare;

  3. If it is true turn on the LED, RELÉ, or ETC…

  4. If isen’t true the led continue turn off.

Sorry, I think my English isen’t very good, but with these words I try to explain my idea.

  • My project is more compliqued, but for now I need only learn how to do that.

Have you written any code so far, if so please post it. Also how many passwords are you planning on using? I have written a password sketch that can check between multiple users, and the simple, single password sketch.

So post what you have, and we'll take a look.

Then, I'm starting to use arduino and I haven't been using the arduino.

I need to use some passwords, for example 3, for simulate somebody on the place. If you can help me I'll be gratefull for you

Anyway, I need to start this program and is it that really need. I have maked some programs but nothing like it. And it's seems a little hard to put a password on the memory and digit in keypad an compare. I've searched it, but I didn't find it.

Have you downloaded the keypad library, and got it to work? It would be nice to know what you have already, but if your just starting to learn, then try to simply compare two char arrays.

This is just a small example, its not a complete sketch.

Master[4] = {1,2,3,4};
MyArray[4] = {1,3,2,4};

for(int x = 0; x < 4; x++)
{
 if(Master[x] == MyArray[x] )
  {
    Pass_is_good = true;
  }

 else
  {
    Pass_is_good = false;
    break;
  }
}

if (Pass_is_good == true) 
 {
  //LED ON
 }

else
 {
  //LED OFF
 }

Thanks for your idea about code. I understand the logic...

But now I would like to know how to put and to read one password on Memory...

But now I would like to know how to put and to read one password on Memory...

The thought occurs to be that you should learn to crawl before entering the Boston Marathon.

First show us that you have a working keypad sketch, that can check a password. Then we'll show you how to store it on the EEPROM and recover it on start up.

this is my programm…I’ve been searched some examples on forum and now I can do this program. then now I need to set the passwords on Memory(EEPROM) and read it

PROGRAM…

#include <Keypad.h> //Includes keypad library

const byte ROWS = 4;
const byte COLS = 3;

char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad

char TestArray[4]; // Creat array to be comparate with other ones.
char Murillo[4] = {‘1’,‘2’,‘2’,‘1’}; // creates array for correct password first login
char Jeferson[4] = {‘1’,‘2’,‘2’,‘3’}; //creates array for correct Password Second login
char Messias[4] = {‘1’,‘1’,‘1’,‘1’}; //creates array for correct Password third login
char Newcostume[4] = {‘9’,‘9’,‘9’,‘9’}; //creates array for correct Password next login…

#define ledPin 9

int i = 0; //variable to count number of key pressed

void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
}

void loop(){
char key = kpd.getKey(); //read ley press

if(key){
TestArray = key; //record key press in input array

  • i++; // increase key pressed variable*

  • if (i == 4){ //If 4 keys have been pressed*

  • if (TestArray[0] == Murillo[0] && //compare input password to login 1 password*

  • TestArray[1] == Murillo[1] && //compare input password to login 1 password*

  • TestArray[2] == Murillo[2] && //compare input password to login 1 password*

  • TestArray[3] == Murillo[3]) //compare input password to login 1 password*

  • {*

  • digitalWrite(ledPin, HIGH);*

  • delay(1000);*

  • digitalWrite(ledPin, LOW);*

  • delay(500);*

  • memset(TestArray,’\0’,4); //clears the input array*

  • }*

  • else //in case of it isen’t be the first password read next*

  • {*

  • if (TestArray[0] == Jeferson[0] && //compare input password to login 2 password*

  • TestArray[1] == Jeferson[1] && //compare input password to login 2 password*

  • TestArray[2] == Jeferson[2] && //compare input password to login 2 password*

  • TestArray[3] == Jeferson[3]) //compare input password to login 2 password*

  • {*

  • digitalWrite(ledPin, HIGH);*

  • delay(1000);*

  • digitalWrite(ledPin, LOW);*

  • delay(500);*

  • memset(TestArray,’\0’,4); //clears the input array*

  • }*

  • else // in case of it isen’t be the second password read next*

  • {*

  • if (TestArray[0] == Messias[0] && //compare input password to login 3 password*

  • TestArray[1] == Messias[1] && //compare input password to login 3 password*

  • TestArray[2] == Messias[2] && //compare input password to login 3 password*

  • TestArray[3] == Messias[3]) //compare input password to login 3 password*

  • {*

  • digitalWrite(ledPin, HIGH);*

  • delay(1000);*

  • digitalWrite(ledPin, LOW);*

  • delay(500);*

  • memset(TestArray,’\0’,4); //clears the input array*

  • }*

  • else // in case of it isen’t be the third password read next*

  • {*

  • if (TestArray[0] == Newcostume[0] && //compare input password to new logins password*

  • TestArray[1] == Newcostume[1] && //compare input password to new logins password*

  • TestArray[2] == Newcostume[2] && //compare input password to new logins password*

  • TestArray[3] == Newcostume[3]) //compare input password to new logins password*

  • {*

  • digitalWrite(ledPin, HIGH);*

  • delay(1000);*

  • digitalWrite(ledPin, LOW);*

  • delay(500);*

  • memset(TestArray,’\0’,4); //clears the input array*

  • }*

  • else // in case of wrong passwords*

  • {*

  • memset(TestArray,’\0’,4); //clears the input array*

  • delay(1000); //wait 1 sec*

  • }*

  • }*

  • }*

  • }*

  • i=0; //Zero number of keys pressed*

  • }*

  • }*
    };

If you can help me I’ll stay very gratefull
and Thanks for help me with how to make a comparation.

I hope that this program help the people that want to do access control system!! XD

To shave your code down, use a for loop.
Now to save to the EEPROM, since you have a password of four characters long, it should be easy to save them.

Example:

for(street = 0; street < number_of_passwords * 4; street += 4)
{
for(address =0; address < 4; address++)
{
EEPROM.write(address + street, data);
}
}
This will write the passwords to the memory, then to retrieve the passwords, use EEPROM.read(address + street);

HAZARD MINDS,

Could you tell more about this code...?

I'll try to put it in my code, not try...I'll. thank's

The eeprom is an accessible memory you can use to save your data. It is looking for a byte so nothing over 255, unless converted or type casted. The eeprom is looking for two values, an address, and the data.
You want to save multiple passwords, so what you need to do is this.

for(street = 0; street < number_of_passwords * length_of_password; street += 4)// This is increments the address by 4 (length of password, 4 characters), so 4 passwords multiplied by 4 will give you 16 locations.
{
  for(address =0; address < 4; address++)// this will store values at 0 - 3, so with the aid of a "street", you can keep this for loop, and just switch the street.
  {
    EEPROM.write(address + street, data); // first cycle = 0 - 3, second cycle = 4 - 7, third cycle = 8 - 11, and last cycle = 12 - 15
  }
}

For this to work a little more smoothly, I would suggest combining your passwords into one (4 by 4) array, instead of single arrays for each person.

Passwords[4][4] = { // Passwords[ street ][ address ]
{ person 1 password },
{ person 2 password },
{ person 3 password },
{ person 4 password }
};

Now to read the passwords, you just need to use eeprom.read( address + street ) and use the same FOR loop setup as above.

I try to use it, but I'm not getting to do this.

Could show me some example with this code....?

I'll come up with something.

Ok! Thank's

Here is a simple example code. This uses the serial monitor, but if you change “key[num] = Serial.read();” to "key[num] = Keypad.getKey();" it should work. If not post your code and I’ll see what’s wrong.

#include <EEPROM.h>

const int number_of_passwords = 2, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;

char data[number_of_passwords][length_of_password] = {
  {
    '1','2','3','4'     }
  ,
  {
    '7','8','9','0'     }
};

void setup(){
  Serial.begin(9600);
  for(street = 0; street < number_of_passwords * length_of_password; street += 4)// This is increments the address by 4 (length of password, 4 characters), so 4 passwords multiplied by 4 will give you 16 locations.
  {
    for( address =0; address < length_of_password; address++)// this will store values at 0 - 3, so with the aid of a "street", you can keep this for loop, and just switch the street.
    {
      EEPROM.write(address + street, data[street/4][address]); // first cycle = 0 - 3, second cycle = 4 - 7, third cycle = 8 - 11, and last cycle = 12 - 15
    }
  }
  address = 0; // clear address
  street = 0; // clear street
}

void loop() {

  if(Serial.available() > 0){
    while(num != 4) // get four numbers
    {
      if(Serial.available() > 0){
        key[num] = Serial.read();
        Serial.print(key[num]);
        num++;
      }
    }
    while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
    {
      if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
      {
        address++; // if the first number matches, get the next.
      }
      else // no match was found, try next password
      {
        address = 0; // set address back to zero and start over
        user++; // keep track of users
        if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
      }
      if(address == 4){ // if address equals 4, password was found for said user
        Serial.println("");
        Serial.print("Password found: ");
        Serial.print("User ");
        Serial.println(user);
        cleardata();
        break;
      }
    }
    if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, display error message.
    {
      Serial.println("");
      Serial.println("Password not found");
      cleardata(); // clear variables
    }
  }
}

void cleardata()
{
  address = 0; 
  street = 0;
  user = 1;
  num = 0;
  return;
}

Unless you change the passwords, you can comment out the For loops in the setup function. They don’t need to be constantly stored if nothing has changed.
Now to save new passwords, you need increase the size of the data array, and the value “number_of_passwords”. You can then prompt the user to save the new password if the password was not found.

I would like to know how you can turn on the led when your password is correct.

I tryed to do the code and it became so…

#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library

const byte ROWS = 4; 
const byte COLS = 3; 

char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {8, 7, 6}; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad

const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;

char datatest[number_of_passwords][length_of_password]={{'1','2','2','1'},
                                                        {'1','2','2','3'}, 
                                                        {'1','1','1','1'},
                                                        {'9','9','9','9'}};

#define ledPin 9
#define AlarmWrong 10

int i = 0; //variable to count number of key pressed

void setup(){
pinMode(ledPin, OUTPUT); //set the alarm open pin
pinMode(AlarmWrong, OUTPUT);

for(street = 0; street < number_of_passwords * length_of_password; street += 4)
  {
    for( address =0; address < length_of_password; address++)
    {
      EEPROM.write(address + street, datatest[street/4][address]);
    }
  }
  address = 0; // clear address
  street = 0; // clear street
}

void loop(){ 
  kpd.getKey(); //read ley press  
  
  if(kpd.getKey() > 0){
    while(num != 4) // get four numbers
    {
      if(kpd.getKey() > 0){
        key[num] = kpd.getKey();
        num++;
      }
    }
    while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
    {
      if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
      {
        address++; // if the first number matches, get the next.
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
      }
      else // no match was found, try next password
      {
        address = 0; // set address back to zero and start over
        user++; // keep track of users
        if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
      }
      if(address == 4){ // if address equals 4, password was found for said user
        Serial.print("User ");
        Serial.println(user);
        correctpassword();
        cleardata();
        break;
        
      }
      if(length_of_password > 4){
      digitalWrite(AlarmWrong, HIGH);
      delay(500);
      digitalWrite(AlarmWrong, LOW);
      delay(500);
      cleardata(); // clear variables
      }
    }
    if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
    {
      digitalWrite(AlarmWrong, HIGH);
      delay(500);
      digitalWrite(AlarmWrong, LOW);
      delay(500);
      cleardata(); // clear variables
    }
  }
}




void cleardata()
{
  address = 0; 
  street = 0;
  user = 1;
  num = 0;
  return;
}
  
  
  
  
  


void correctpassword(){
                           digitalWrite(ledPin, HIGH); 
                           delay(1000); 
                           digitalWrite(ledPin, LOW); 
                           delay(500); 
                           cleardata(); //clears the input array
                     
                    }

but I think it still have some wrongs, because I tryed turn on the led when the passaword is correct but I didn’t get it…if you can help me I’ll stay very gratefull

Im not sure you fully understand what is happening with the code I wrote.

This will only store 4 numbers, if you try to enter more than four, it will roll over. So if I enter 12345, it will see that 1234 is a correct password, but then it will roll the 5 over, and store it for the next password check. I didn't implement any hang timer or anything that checks if the password is too long. I could add it if needed, but I would need to adjust the code to do so.

while(num != 4) // get four numbers { if(Serial.available() > 0){ key[num] = Serial.read(); Serial.print(key[num]); num++; } }

Im not sure what you why you add this IF statement, because "length_of_password" is a const, meaning it will not change throughout the code. So this IF statement will never happen.

if(length_of_password > 4){ digitalWrite(AlarmWrong, HIGH); delay(500); digitalWrite(AlarmWrong, LOW); delay(500); cleardata(); // clear variables }

Let me look at this a little more, and I'll get back to you.

UPDATE:
I added a hang timer of 5 seconds, but you can change the time if needed. I added more comments to try and explain what is occurring, and don’t be alarmed about the method for getting the numbers, I explained what is happening. Also I got rid and commented out a few things you added.

#include <Keypad.h> //Includes keypad library
#include <EEPROM.h> //Includes EEPROM library

const byte ROWS = 4; 
const byte COLS = 3; 

char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; 
byte colPins[COLS] = {8, 7, 6};

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //Creates the keypad

const int number_of_passwords = 4, length_of_password = 4;
char key[4];
int address = 0, street = 0, user = 1, num = 0;
unsigned long current_time = 0, time = 0, hang_time = 5000;
boolean timeout = false;

char myKey;
char datatest[number_of_passwords][length_of_password]={
  { '1','2','2','1' },
  { '1','2','2','3' }, 
  { '1','1','1','1' },
  { '9','9','9','9' }
};

#define ledPin 9
#define AlarmWrong 10

//int i = 0; //variable to count number of key pressed

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); //set the alarm open pin
  pinMode(AlarmWrong, OUTPUT);

  for(street = 0; street < number_of_passwords * length_of_password; street += 4)
  {
    for( address =0; address < length_of_password; address++)
    {
      EEPROM.write(address + street, datatest[street/4][address]);
    }
  }
  address = 0; // clear address
  street = 0; // clear street
}

void loop(){ 
  timeout = false;
  while(num != 4) // get four numbers
  { 
    myKey = kpd.getKey();  // Check the first keypress
    if(myKey){                  // This will check to see if a key was pressed
      current_time = millis();  // and if so, this will also store the current time.
      key[num] = myKey;         // This will add the keypress to the array
      Serial.print(key[num]);   
      num++;                    // This will increment the array for the next keys

      while(time < hang_time){         // This will allow the program to get the rest of the
        time = millis() - current_time;// of the numbers within the time limit of 5 seconds
        myKey = kpd.getKey(); // Check keypress again
        if(myKey){
          key[num] = myKey;
          myKey = 0; // The key must be set to zero, otherwise the code will not work correctly 
          Serial.print(key[num]);
          num++;
        }
        if(num == 4) 
        {
          time = 0; // reset variable "time" for timer to start over
          break;
        }
      }
      if(time >= 5000) // If hang time has reached 5 seconds, display timeout message
      {
        Serial.println(" Timed out");
        // ADD ALARMWRONG HERE
        cleardata(); // Clear everything
        timeout = true; // Even though timeout was set false in cleardata(), this is needed so that the last good password will not display.
        time = 0; 
        break;
      }
    }
  }
  while(street != number_of_passwords * length_of_password) // while street does not exceed maximum number
  {
    if(key[address] == EEPROM.read(address + street) ) // look through the EEPROM and see if the serial/keypad data matches what was stored.
    {
      address++; // if the first number matches, get the next.
      //WHY IS THIS NEEDED, bolded so it stands out?
      //      digitalWrite(ledPin, HIGH);
      //      delay(1000);
      //      digitalWrite(ledPin, LOW);
    }
    else // no match was found, try next password
    {
      address = 0; // set address back to zero and start over
      user++; // keep track of users
      if(street != (number_of_passwords * length_of_password) ) street+=4; // switch memory location
    }
    if(address == 4 && !timeout){ // if address equals 4, password was found for said user
      Serial.print(" User ");
      Serial.println(user);
      correctpassword();
      cleardata();
      break;
    }
  }

  if(street == (number_of_passwords * length_of_password) ) // if street reached max number of passwords, Alarm password is wrong.
  {
    Serial.println(" Password not found");
    digitalWrite(AlarmWrong, HIGH);
    delay(500);
    digitalWrite(AlarmWrong, LOW);

    cleardata(); // clear variables
  }
}


void cleardata()
{
  timeout = false;
  address = 0; 
  street = 0;
  user = 1;
  num = 0;
  return;
}

void correctpassword()
{
  digitalWrite(ledPin, HIGH); 
  delay(1000); 
  digitalWrite(ledPin, LOW); 
  delay(500); 
  cleardata(); //clears the input array
}

Thank's HazardsMind,

Now the code is working, before you post the code I tryed to put the led to work, but just alarm wrong have worked. Anyway the is working, then now I'll do some changes.

So thank's you help a lot.

Now I continue with my project, whose I will use ID-12, for hour I'll study more about it, later if I have some questions I post here

XD