#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int RELAY_PIN  = 1; // PIN RELAY 
const int ROW_NUM    = 4; // 4 BARIS
const int COLUMN_NUM = 4; // 4 KOLOM
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// tempat untuk inisialisasi kode/password
const String password_1 = "2602"; // change your password here
const String a;
int i;
String input_password;
void setup() {
  Serial.begin(9600);
  lcd.init();                    // initialize the lcd
  lcd.backlight();
  input_password.reserve(32); // maksimal karakter password
  pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
  digitalWrite(RELAY_PIN, LOW); // relay kondisi low
}
void loop() {
    char key = keypad.getKey();
  if (key){
    Serial.println(key);
    
    if(key == '*') {
      a = input_password; // reset the input password
      Serial.print("pass :");
      Serial.println(a);
      password_1 = a;
      input_password = "";
    }
    else if(key=='#'){
          input_password = "";
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Latest password");
          lcd.setCursor(0,1);
          lcd.print("Press D");
          input_password = "";}
    else if(key == 'D'){
      if(input_password == password_1){
              input_password = "";
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("New Password");
              delay(3000);
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("*, for yes");
          }
          else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Incorrect  Code");
        lcd.setCursor(0,1);
        lcd.print("D, batal reset");
        input_password = "";
      }}
    else if(key == 'D'){
      input_password = ""; //hapus password
    }
    else if(key == 'B') {
      // Serial.println(password_1);
      if(input_password == password_1) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Door Unlocked");
        lcd.setCursor(0,1);
        lcd.print("#,change code");
        buka();
      } 
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Incorrect Code");
        lcd.setCursor(0,1);
        lcd.print("Try Again");
        Serial.print("Incorrect Code ");
        Serial.println("Try Again");
        input_password = "";
      }
    } 
    else {
      input_password += key; // append new character to input password string
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Input password!");
      lcd.setCursor(0,1);
      lcd.print(input_password);
      tutup();
    }
  }
}
void buka(){
  digitalWrite(RELAY_PIN,HIGH);  
  delay(5000);
  digitalWrite(RELAY_PIN,LOW);
}
void tutup(){
  digitalWrite(RELAY_PIN,LOW);
}
NOCOMNCVCCGNDINLED1PWRRelay Module