Go Down

Topic: CODED LOCK, with different personal number, each number has its code (Read 803 times) previous topic - next topic

HOUSSi

hi guys , help pls
I want to register personnel numbers in my program (4 or 5 numbers), firstly the user must insert their registration number (each user has his number) if his entry is correct, then he must insert a default password (he can change it after ), if his password is correct, the door opens if not he must try again, I have done the password programming and the option to change it, but I have not found the solution to insert numbers, I need your help please
that's my program:






/*  Password Door Unlock with Arduino uno
  Dev: Vasilakis Michalis // Date:24/5/2016 // Ver:1.0  */

//Libraries
#include <LiquidCrystal.h>
#include <Keypad.h>

/*-------------------------------KEYPAD---------------------------------------*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keypressed;
char keymap[numRows][numCols]=
{
 {'7','8','9','A'},
  {'4','5','6','B'},
  {'1','2','3','C'},
  {'*','0','#','D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {4, 5, 6, 7};//Rows 0 to 3
byte colPins[numCols] = {0, 1, 2, 3};//Columns 0 to 3             
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/
LiquidCrystal lcd(13,12,11,10,9,8); //LCD
const int buzzer = A1;        //Buzzer/small speaker
const int lock = A3;       //Electric door opener


/*-------------------------------VARIABLES------------------------------------*/
String password
="1234"; //Variable to store the current password
String tempPassword=""; //Variable to store the input password
int doublecheck;    //Check twice the new passoword
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
boolean input_pass;   //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int i = 1; //variable to index an array
/*----------------------------------------------------------------------------*/

void setup(){
  pinMode(lock,OUTPUT);
  lcd.begin(16, 2); //Setup the LCD's number of columns and rows
  //Print welcome message...
  lcd.setCursor(0,0);
  lcd.print(" LES CIMENTS DE  ");
  lcd.setCursor(4,1);
  lcd.print(" BIZERTE  ");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);

}

void loop() { //Main loop
  unlockTheDoor();
}
/********************************FUNCTIONS*************************************/

void unlockTheDoor(){
  lockAgain: //goto label
  tempPassword="";
  lcd.clear();
  i=6;
  noTone(buzzer);
  digitalWrite(lock, LOW);
  while(!checkPassword){
    lcd.setCursor(0,0);
    lcd.print("ENTER PASSWORD:  ");
    lcd.setCursor(0,1);
    lcd.print("PASS>");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");       //Put * on lcd
        i++;
        tone(buzzer,800,200);   //Button tone
      }
      else if (keypressed == 'C'){
        changePassword();
        goto lockAgain;
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){  //Check for password
        if (password==tempPassword){//If it's correct...
          lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Correct password");
          lcd.setCursor(0,1);
          lcd.print("Door is unlocked");
          tone(buzzer,100);    //Play a tone while door is unlocked
          digitalWrite(lock, HIGH);//unlock the door for 5 seconds
          delay(5000);
          goto lockAgain;
        }
        else{           //if it's false, retry
           lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Password incorrect");
          lcd.setCursor(0,1);
          lcd.print("Try again ... ");
           delay(500);
          tempPassword="";
          tone(buzzer,500,200);
          delay(300);
          tone(buzzer,500,200);
          delay(300);
          goto lockAgain;
        }
      }
    }
  }
}

//Change current password
void changePassword(){
  retry: //label for goto
  tempPassword="";
  lcd.clear();
  i=1;
  while(!changedPassword){        //Waiting for current password
    keypressed = myKeypad.getKey();   //Read pressed keys
    lcd.setCursor(0,0);
    lcd.print("CURRENT PASSWORD");
    lcd.setCursor(0,1);
    lcd.print(">");
    if (keypressed != NO_KEY){
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");
        i++;
        tone(buzzer,800,200);       
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){
        i=1;
        if (password==tempPassword){
          storedPassword=false;
          tone(buzzer,500,200);
          newPassword();          //Password is corrent, so call the newPassword function
          break;
        }
        else{               //Try again
           lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Password incorrect");
          lcd.setCursor(0,1);
          lcd.print("Try again ... ");
           delay(1000);
          tempPassword="";
          tone(buzzer,500,200);
          delay(300);
          tone(buzzer,500,200);
          delay(300);
          goto retry;
        }
      }
    }
  }
}
String firstpass;
//Setup new password
void newPassword(){
  tempPassword="";
  changedPassword=false;
  lcd.clear();
  i=1;
  while(!storedPassword){
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (doublecheck==0){
      lcd.setCursor(0,0);
      lcd.print("SET NEW PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(">");
    }
    else{
      lcd.setCursor(0,0);
      lcd.print("One more time...");
      lcd.setCursor(0,1);
      lcd.print(">");
    }
    if (keypressed != NO_KEY){
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");
        i++;
          tone(buzzer,800,200);
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){
        if (doublecheck == 0){
          firstpass=tempPassword;
          doublecheck=1;
          newPassword();
        }
        if (doublecheck==1){
          doublecheck=0;
          if (firstpass==tempPassword){
            i=1;
            firstpass="";
            password = tempPassword; // New password saved
            tempPassword="";//erase temp password
            lcd.setCursor(0,0);
            lcd.print("PASSWORD CHANGED");
            lcd.setCursor(0,1);
            lcd.print("----------------");
              storedPassword=true;
              tone(buzzer,500,400);
              delay(2000);
              lcd.clear();
              break;
          }
          else{
            firstpass="";
            newPassword();
          }
        }
      }
    }
  }
}


sterretje

Please edit your post and add code tags.

Type [code] before your code.
Type [/code] after your code.

Further advise is to get rid of the goto.

To link numbers and passwords, a struct or class is the way to go. And for five of them, an array of structs or classes comes to mind.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


dougp

First things first:
 
Please edit your post and add code tags.

 after your code.
This is done with the symbol left of 'B' in the post editing window.

Then, investigate arrays and structures.
I don't trust atoms.  They make up everything.

No private consultations undertaken!

HOUSSi

Code: [Select]


//Libraries
#include <LiquidCrystal.h>
#include <Keypad.h>

/*-------------------------------KEYPAD---------------------------------------*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keypressed;
char keymap[numRows][numCols]=
{
 {'7','8','9','A'},
  {'4','5','6','B'},
  {'1','2','3','C'},
  {'*','0','#','D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {4, 5, 6, 7};//Rows 0 to 3
byte colPins[numCols] = {0, 1, 2, 3};//Columns 0 to 3             
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/
LiquidCrystal lcd(13,12,11,10,9,8); //LCD
const int buzzer = A1;        //Buzzer/small speaker
const int lock = A3;       //Electric door opener


/*-------------------------------VARIABLES------------------------------------*/
String password
="1234"; //Variable to store the current password
String tempPassword=""; //Variable to store the input password
int doublecheck;    //Check twice the new passoword
boolean armed = false;  //Variable for system state (armed:true / unarmed:false)
boolean input_pass;   //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int i = 1; //variable to index an array
/*----------------------------------------------------------------------------*/

void setup(){
  pinMode(lock,OUTPUT);
  lcd.begin(16, 2); //Setup the LCD's number of columns and rows
  //Print welcome message...
  lcd.setCursor(0,0);
  lcd.print(" LES CIMENTS DE  ");
  lcd.setCursor(4,1);
  lcd.print(" BIZERTE  ");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);

}

void loop() { //Main loop
  unlockTheDoor();
}
/********************************FUNCTIONS*************************************/

void unlockTheDoor(){
  lockAgain: //goto label
  tempPassword="";
  lcd.clear();
  i=6;
  noTone(buzzer);
  digitalWrite(lock, LOW);
  while(!checkPassword){
    lcd.setCursor(0,0);
    lcd.print("ENTER PASSWORD:  ");
    lcd.setCursor(0,1);
    lcd.print("PASS>");
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");       //Put * on lcd
        i++;
        tone(buzzer,800,200);   //Button tone
      }
      else if (keypressed == 'C'){
        changePassword();
        goto lockAgain;
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){  //Check for password
        if (password==tempPassword){//If it's correct...
          lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Correct password");
          lcd.setCursor(0,1);
          lcd.print("Door is unlocked");
          tone(buzzer,100);    //Play a tone while door is unlocked
          digitalWrite(lock, HIGH);//unlock the door for 5 seconds
          delay(5000);
          goto lockAgain;
        }
        else{           //if it's false, retry
           lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Password incorrect");
          lcd.setCursor(0,1);
          lcd.print("Try again ... ");
           delay(500);
          tempPassword="";
          tone(buzzer,500,200);
          delay(300);
          tone(buzzer,500,200);
          delay(300);
          goto lockAgain;
        }
      }
    }
  }
}

//Change current password
void changePassword(){
  retry: //label for goto
  tempPassword="";
  lcd.clear();
  i=1;
  while(!changedPassword){        //Waiting for current password
    keypressed = myKeypad.getKey();   //Read pressed keys
    lcd.setCursor(0,0);
    lcd.print("CURRENT PASSWORD");
    lcd.setCursor(0,1);
    lcd.print(">");
    if (keypressed != NO_KEY){
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");
        i++;
        tone(buzzer,800,200);       
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){
        i=1;
        if (password==tempPassword){
          storedPassword=false;
          tone(buzzer,500,200);
          newPassword();          //Password is corrent, so call the newPassword function
          break;
        }
        else{               //Try again
           lcd.clear();     
          lcd.setCursor(0,0);
          lcd.print("Password incorrect");
          lcd.setCursor(0,1);
          lcd.print("Try again ... ");
           delay(1000);
          tempPassword="";
          tone(buzzer,500,200);
          delay(300);
          tone(buzzer,500,200);
          delay(300);
          goto retry;
        }
      }
    }
  }
}
String firstpass;
//Setup new password
void newPassword(){
  tempPassword="";
  changedPassword=false;
  lcd.clear();
  i=1;
  while(!storedPassword){
    keypressed = myKeypad.getKey();   //Read pressed keys
    if (doublecheck==0){
      lcd.setCursor(0,0);
      lcd.print("SET NEW PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(">");
    }
    else{
      lcd.setCursor(0,0);
      lcd.print("One more time...");
      lcd.setCursor(0,1);
      lcd.print(">");
    }
    if (keypressed != NO_KEY){
      if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
      keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
      keypressed == '8' || keypressed == '9' ){
        tempPassword += keypressed;
        lcd.setCursor(i,1);
        lcd.print("*");
        i++;
          tone(buzzer,800,200);
      }
      else if (keypressed=='#'){
        break;
      }
      else if (keypressed == 'A'){
        if (doublecheck == 0){
          firstpass=tempPassword;
          doublecheck=1;
          newPassword();
        }
        if (doublecheck==1){
          doublecheck=0;
          if (firstpass==tempPassword){
            i=1;
            firstpass="";
            password = tempPassword; // New password saved
            tempPassword="";//erase temp password
            lcd.setCursor(0,0);
            lcd.print("PASSWORD CHANGED");
            lcd.setCursor(0,1);
            lcd.print("----------------");
              storedPassword=true;
              tone(buzzer,500,400);
              delay(2000);
              lcd.clear();
              break;
          }
          else{
            firstpass="";
            newPassword();
          }
        }
      }
    }
  }
}

sterretje

No wonder you need goto statements. You have a while-loop based on a variable that never changes.

To demonstrate how to use a struct

Code: [Select]
// max length of user code; 4 digits plus terminating nul character
#define CODE_LENGTH 5
// max length of password; 9 digits plus terminating nul character
#define PASSWORD_LENGTH 10

struct USER
{
  char code[CODE_LENGTH];
  char password[PASSWORD_LENGTH];
};

// variable to hold user code and password
USER user;

A struct combines related information. E.g. entries in a phone book with a name and a phone number; in this case a code and a password.

I suggest that you store the user information in eeprom; that way updated information will be remembered. The below code will put 10 user records in eeprom.

Code: [Select]
#include <EEPROM.h>
// generic macro to determine number of elements in array
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
// macro to determine the largest of two numbers
#define MAX(x,y) ((x>y)?x:y)

// max length of user code; 4 digits plus terminating nul character
#define CODE_LENGTH 5
// max length of password; 9 digits plus terminating nul character
#define PASSWORD_LENGTH 10
// max number of users
#define MAXUSERS 10

struct USER
{
  char code[CODE_LENGTH];
  char password[PASSWORD_LENGTH];
};

// variable to hold user code and password
USER user;

void setup()
{
  Serial.begin(57600);

  // default password for all users
  strcpy(user.password, "1234");
  for (byte userCnt = 0; userCnt < MAXUSERS; userCnt++)
  {
    // assign a random user code
    itoa(random(1000), user.code, 10);
    // create a user record in eeprom
    createUser(userCnt);
    //delay(100);
  }
}

void loop()
{
}

/*
  create a user record in eeprom; uses global user variable
*/
void createUser(byte number)
{
  EEPROM.put(number * sizeof(USER), user);
}

The only weak point is that it can create duplicate codes; I leave that up to you to fix if needed. If you can't fix it, use userCnt instead of random().

The next code displays all user records in the eeprom
Code: [Select]
#include <EEPROM.h>
// generic macro to determine number of elements in array
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
// macro to determine the largest of two numbers
#define MAX(x,y) ((x>y)?x:y)

// max length of user code; 4 digits plus terminating nul character
#define CODE_LENGTH 5
// max length of password; 9 digits plus terminating nul character
#define PASSWORD_LENGTH 10
// max number of users
#define MAXUSERS 10

struct USER
{
  char code[CODE_LENGTH];
  char password[PASSWORD_LENGTH];
};

// variable to hold user code and password
USER user;

void setup()
{
  Serial.begin(57600);

  for (byte userCnt = 0; userCnt < MAXUSERS; userCnt++)
  {
    EEPROM.get(userCnt * sizeof(USER), user);
    Serial.print("user: "); Serial.println(userCnt + 1);
    Serial.print("Code: "); Serial.println(user.code);
    Serial.print("Pass: "); Serial.println(user.password);
    Serial.println();
  }
}

void loop()
{
}

In a for-loop, it reads each user record and displays it.

The below function allows you to find a record in eeprom for a given user code
Code: [Select]
/*
  find a user by user code
  In:
    user code
  Return:
    0xFF if not found, else index in array in eeprom
*/
byte findUser(char *code)
{
  byte index = 0xFF;
  USER u;
  for (byte userCnt = 0; userCnt < MAXUSERS; userCnt++)
  {
    // read eeprom record
    EEPROM.get(userCnt * sizeof(USER), u);
    // if user code matches
    if (strcmp(u.code, code) == 0)
    {
      // remember index
      index = userCnt;
      // copy to global user variable
      memcpy(&user, &u, sizeof(USER));
    }
  }
  // if not found
  if (index == 0xFF)
  {
    // clear global user variable
    memset(&user, 0, sizeof(USER));
  }
  return index;
}

And can be used like this
Code: [Select]
#include <EEPROM.h>
// generic macro to determine number of elements in array
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
// macro to determine the largest of two numbers
#define MAX(x,y) ((x>y)?x:y)

// max length of user code; 4 digits plus terminating nul character
#define CODE_LENGTH 5
// max length of password; 9 digits plus terminating nul character
#define PASSWORD_LENGTH 10
// max number of users
#define MAXUSERS 10

struct USER
{
  char code[CODE_LENGTH];
  char password[PASSWORD_LENGTH];
};

// variable to hold user code and password
USER user;

void setup()
{
  Serial.begin(57600);

  // find user with code 878
  byte idx = findUser("878");
  if (idx == 0xFF)
  {
    Serial.println("user not found");
  }
  else
  {
    Serial.println("user found");
    Serial.print("user: "); Serial.println(idx + 1);
    Serial.print("Code: "); Serial.println(user.code);
    Serial.print("Pass: "); Serial.println(user.password);
    Serial.println();
  }
}


You can not store String (capital S) directly in eeprom; use of String (capital S) is anyway discouraged. The codes shown above use so-called c-strings (character arrays terminated with '\0');

I strongly suggest that you write a function to read a c-string from keypad that can be used for both the code and the password.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

lastchancename

Once you have the keypad and code validation figured out, you may like to explore STATE MACHINES

This will make it much easier for you to 'coordinate' your locks operation.
Allowing timeout, incomplete key entries etc to ensure the lock doesn't get into unexpected states.

As a lock, you may also like to explore using the WDT in future to avoid any unexpected freezes in your code - that will reset the system to a known (locked/waiting for keypad state) when something goes wrong.

Perhaps a push-button input to allow emergency unlock from the 'inside' of the room...
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Go Up