Entering 2 Passwords in a single system for 1 and 2 digit

Hello all,

                I am having a code for password operated digital  lock with 4x4 keypad.

I modified it as per my application to make an output LED High for single digit password say "1"
in a desired amount of time for On and OFF state

Now i want to enter another password with 2 digit say "11" to glow the LED for a different ON & OFF time (other than previous case).

The 2 password entry options should be on the same program & same keyboard but individually working and not interfering each other

Please check the code as only the single digit "1" is working but not allowing "11" to get on work.

So how to solve it ?

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

#include <SoftwareSerial.h>

#include "DFRobotDFPlayerMini.h"

//const int pin1 = LOW;
//int pin1State=0;
//bool pinState = LOW;

#define Num_Length 2
#define Num_Length1 3

int signalPin = 12;

char Data[Num_Length];
char Master[Num_Length] = "1";

char Data1[Num_Length1];
char Master1[Num_Length1] = "11";

char customKey;

byte data_count = 0, master_count = 0;
byte data_count1 = 0, master_count1 = 0;

// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;

// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Create LCD object
LCD_I2C lcd(0x27, 16, 2);  

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
byte pbRegister = 0;

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

  mySoftwareSerial.begin(9600);
  
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }

  myDFPlayer.volume(29);
  myDFPlayer.playFolder(2, 2);
  
  lcd.begin();
  lcd.backlight();

 

  pinMode(signalPin, OUTPUT);
}


void loop(){

 
  // Get key value if pressed
  char customKey = customKeypad.getKey();


  if (customKey)
    {
         
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++;

    }       
    
     if(data_count == Num_Length-1)
     {
      lcd.clear();
      //clearData();
      
      switch(data_count)
        
    {
    case 1:
    if(!strcmp(Data, Master)){
      lcd.print("Correct");
      digitalWrite(signalPin, HIGH); 
      delay(1000);

      }
    else{
      lcd.setCursor(0,0);
      lcd.print("incorrect");
      lcd.setCursor(0,1);
      lcd.print("InValid Entry");
      delay(2000);
      }
      
      clearData();
      break;
      
     }  //switch end
        
        //lcd.clear();
         
        
     } //if(data_count == Num_Length-1)


          pbRegister = 0;
          delay(100);


      if (customKey)
    {
    
    Data1[data_count1] = customKey; 
    lcd.setCursor(data_count1,1); 
    lcd.print(Data1[data_count1]); 
    data_count1++;

    }

      if(data_count1 == Num_Length1-1)
     {
      lcd.clear();
      //clearData();
      
    switch(data_count1)    
    {
    case 2:
    if(!strcmp(Data1, Master1)){
      lcd.setCursor(3,0);
      lcd.print("all good");
      lcd.setCursor(3,1);
      lcd.print("opened");
      digitalWrite(signalPin, HIGH); 
      delay(5000);

      }
    else{
      lcd.setCursor(0,0);
      lcd.print("go back");
      lcd.setCursor(0,1);
      lcd.print("InValid Entry");
      delay(7000);
      }
      
      clearData();
      break;
      
     }  //switch end
        
        //lcd.clear();
         
        
     } //if(data_count == Num_Length-1)

      
                             
   }                        // void loop()

   

      void clearData()
      {
        while(data_count !=0)
        {
        Data[data_count--] = 0; 
        }
        while(data_count1 !=0)
        {
        Data1[data_count1--] = 0; 
        }
        
      return; 
    }
       

Please help me out !! Thanksssss . . .

despite of in/correctness of first password the program goes further and make check for second password without to read new keys

I see . . so what to do for it's solution ??

What changes i need to make or what things needs to get added in code ?

Please help !!!

i am not clear with that what you want with LED. I understand you should have a keypad where 2 different passwords to type are, why not one password with 3 symbols.

Okk let me clear it first !

I have keypad and everything needed

Now suppose key pad is connected from 2 to 9 , then remaining pins can be used as output pin say 10, 11, 12 . .

Now Suppose, lets take a simple example of a door with 3 electro-mechanical locks and all the three should be opened with their individual passwords assigned and are activated with 10, 11, 12 output respectively for lock 1 lock 2 lock 3
I don't want to keep all the pass code same for security reasons and decided to make it like
1, 11 & 111 for lock 1 , lock2 lock 3 . .

Its just an example not the real motive actually

My basic aim is to assign any of the pass codes from a single digit to 2 and 3 digit individually operated one

So in that case i can assign it like

3, 48 & 221 or anything like that, just independent of having any limit for a set of digit

So how to go for that ??

Just let me know about only 2 cases say 1 and 11 for now . . that's it !!!

Ok sir le me go through this and check it out !!

Thanks !

Sir this code is not working any of the way

by pressing 1, it just display 1 at the second line

By pressing 11 the screen goes off blank

Its the result I'hv got

change any Serial outputs to proper LCD outputs and delays

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

#define Num_Rings 2

String Data = "";
String Master[Num_Rings] = {"10", "230"};

char Key = 0;
byte Pass = 0;
const byte signalPin = 12;

const byte ROWS = 4;
const byte COLS = 4;
const char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad KPD = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 16, 2);

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
byte pbRegister = 0;

void setup() {
  Serial.begin(115200);
    mySoftwareSerial.begin(9600);
    if (!myDFPlayer.begin(mySoftwareSerial))while (true);
    myDFPlayer.volume(29);
    myDFPlayer.playFolder(2, 2);
    lcd.init();
    lcd.backlight();
  pinMode(signalPin, OUTPUT);
}

void loop() {
  char Key = KPD.getKey();
  if (Key != NO_KEY) {
    Data += Key;
    Serial.print(Key);
    Key = 0;

    if (Data.length() == Master[Pass].length()) {
      Serial.println();
      if (Data == Master[Pass]) {
        Serial.println("Correct");
        Data = "";
        Pass ++;
        if (Num_Rings == Pass) {
          Serial.println("Opened");
        Pass = 0;
        }
      }
      else {
        Serial.println("incorrect");
        Pass = 0;
      }
      Data = "";
      Serial.println();
    }
  }
  pbRegister = 0;
  delay(100);
}                        // void loop()

Ya Sir all the way i did with this code

But nothing is going to get happen

No display out !

did you made this?

Where exactly i need to do this Sir ??

serialBegin () ????????

open serial monitor and press 1, 0, 2, 3, 0

Ya it shows nothing

And by pressing other buttons, it sometimes display any of the key and sometimes not
and depending on that combination it displays incorrect

93 62 etc etc on random press

but with 1 0 2 3 0 it shows nothing
I am trying more n more

Ya i just do a waiting operation

pressed 1 and displayed 1 . . . then after waiting sometime I keep pressing 0 so many times and finally it placed 0 just after 1 and displayed Correct

Then same thing i did for 230 pressing so many times

It displayed
Correct
Opened

That way its working

Have to wait for a long time after a button press

or

have to press a button so so many times or sometimes in 2 3 press only

connected LED out to digital write and its working fine that way

#include <LCD_I2C.h>
//#include <Wire.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

#define Num_Rings 2

String Data = "";
String Master[Num_Rings] = {"10", "230"};

char Key = 0;
byte Pass = 0;
const byte signalPin = 12;

const byte ROWS = 4;
const byte COLS = 4;
const char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad KPD = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LCD_I2C lcd(0x27, 16, 2); 

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
byte pbRegister = 0;

void setup() {
  Serial.begin(9600);
    mySoftwareSerial.begin(9600);
    if (!myDFPlayer.begin(mySoftwareSerial))while (true);
    myDFPlayer.volume(29);
    myDFPlayer.playFolder(2, 2);
    lcd.begin();
    lcd.backlight();
  pinMode(signalPin, OUTPUT);
}

void loop() {
  char Key = KPD.getKey();
  if (Key != NO_KEY) {
    Data += Key;
    Serial.print(Key);
    Key = 0;

    if (Data.length() == Master[Pass].length()) {
      Serial.println();
      if (Data == Master[Pass]) {
        Serial.println("Correct");
        digitalWrite(signalPin, HIGH);
        delay(3000);
        digitalWrite(signalPin, LOW);
        Data = "";
        Pass ++;
        if (Num_Rings == Pass) {
          Serial.println("Opened");
          
        Pass = 0;
        }
      }
      else {
        Serial.println("incorrect");
        Pass = 0;
      }
      Data = "";
      Serial.println();
    }
  }
  pbRegister = 0;
  delay(1000);
}                        // void loop()

But display is still not working Sir

How to solve that ??????

Sir one thing we missed that don't have the content under LCD.print to display

Not even a single line beginning note . .

Then we have nothing left here to display any operation's output

every where Serial.println() is you change back to lcd.print() . do not forget lcd.setcursor. copy from old sketch

Ya Sir did some changes and after practicals this code is displaying things but a four line 2 digit is displaying after word correct like this below

.........
.........
.........
.........

and rest issues are same like , have to press so many times and all that

#include <LCD_I2C.h>
//#include <Wire.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

#define Num_Rings 2

String Data = "";
String Master[Num_Rings] = {"10", "230"};

char Key = 0;
byte Pass = 0;
const byte signalPin = 12;

const byte ROWS = 4;
const byte COLS = 4;
const char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad KPD = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LCD_I2C lcd(0x27, 16, 2); 

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
byte pbRegister = 0;

void setup() {
  Serial.begin(9600);
    mySoftwareSerial.begin(9600);
    if (!myDFPlayer.begin(mySoftwareSerial))while (true);
    myDFPlayer.volume(29);
    myDFPlayer.playFolder(2, 2);
    lcd.begin();
    lcd.backlight();
  pinMode(signalPin, OUTPUT);
}

void loop() {

   //lcd.setCursor(0,0);
        //lcd.println("Enter Key - ");
  
  char Key = KPD.getKey();
  if (Key != NO_KEY) {
    Data += Key;
   
    Serial.print(Key);   
    lcd.print(Key);
    Key = 0;

    if (Data.length() == Master[Pass].length())
    {
      Serial.println();
      //lcd.println();
      if (Data == Master[Pass])  
      {
        Serial.println("Correct");
        lcd.setCursor(4,1);
        lcd.println("Correct");
        digitalWrite(signalPin, HIGH);
        delay(3000);
        digitalWrite(signalPin, LOW);
        Data = "";
        Pass ++;
        if (Num_Rings == Pass) {
          Serial.println("Opened");
          lcd.println("Opened");
          
        Pass = 0;
        }
      }
      else {
        Serial.println("incorrect");
        lcd.println("inCorrect");
        Pass = 0;
      }
      Data = "";
      Serial.println();
      //lcd.println();
    }
  }
  pbRegister = 0;
  delay(1000);
}                        // void loop()

Displaying things are too going complicated and everytime the system needs reset for a proper output on screen

#include <LCD_I2C.h>

#include <Keypad.h>
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"

#define Num_Rings 2

String Data = "";
String Master[Num_Rings] = {"10", "230"};

char Key = 0;
byte Pass = 0;
const byte signalPin = 12;

const byte ROWS = 4;
const byte COLS = 4;
const char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad KPD = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 16, 2);

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup() {
  Serial.begin(9600);
  mySoftwareSerial.begin(9600);
  if (!myDFPlayer.begin(mySoftwareSerial))while (true);
  myDFPlayer.volume(29);
  myDFPlayer.playFolder(2, 2);
  lcd.begin();
  lcd.backlight();
  pinMode(signalPin, OUTPUT);
}

void loop() {
  char Key = KPD.getKey();
  if (Key != NO_KEY) {
    Data += Key;
    lcd.setCursor(0, 1);
    lcd.print(Data);
    Key = 0;

    if (Data.length() == Master[Pass].length()) {
      if (Data == Master[Pass]) {
      lcd.clear();
        lcd.print("Correct");
        digitalWrite(signalPin, HIGH);
        delay(1000);
        Data = "";
        Pass ++;
        if (Num_Rings == Pass) {
          lcd.setCursor(3, 0);
          lcd.print("all good");
          lcd.setCursor(3, 1);
          lcd.print("opened");
          digitalWrite(signalPin, HIGH);
          delay(5000);
      lcd.clear();
          Pass = 0;
        }
      }
      else {
        lcd.setCursor(0, 0);
        lcd.print("go back");
        lcd.setCursor(0, 1);
        lcd.print("InValid Entry");
        delay(7000);
      lcd.clear();
        Pass = 0;
      }
      Data = "";
    }
  }
  delay(100);
}

Ya sir its working in one go but the system execute once !!

After that it just takes data one by one and then displays number and alphabet.

Either we need to reset Arduino or have to enter wrong code to reach the initial point of void loop

Then only again the system is available to enter the code and to operate further !!!