#include <Keypad.h>


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

char keys [ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
}; 
byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,8,9};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);

String pwd;
String par_pwd = "9876";
//Password password = Password ("2468");

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("");
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
keypad.addEventListener(keypadEvent);

}

void loop() {
  // put your main code here, to run repeatedly:
keypad.getKey();

}

void keypadEvent (KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
      Serial.print("Pressed: ");
      Serial.println(eKey);
      pwd = pwd + eKey;
      switch(eKey){
        case '*': 
          Serial.println(pwd);
          pwd.remove(4,1);
          checkPassword(pwd);
          break;
        case '#':
         pwd = "";
         break;  
        case 'D':
         digitalWrite(11,LOW);
         digitalWrite(12,LOW);
          pwd = "";

   
      }
  }

}

void checkPassword(String str){
  Serial.println(str);
  if (str == par_pwd){
    digitalWrite(11,HIGH);
 
  }
  else {
    digitalWrite(12,HIGH);
  }
   pwd = "";

}