#include <Keypad.h>                 //Library keypad
#include <LiquidCrystal_I2C.h>    //Library LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2);   //Alamat I2C

const byte ROWS = 4;    //Jumlah baris keypad
const byte COLS = 4;    //Jumlah kolom keypad

char Keys[ROWS][COLS] = {   //Membuat array keypad
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 19, 18, 5, 4 }; 
byte colPins[COLS] = { 2, 13, 12, 14 };

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);    //Masukkan info keypad pada library


char customKey;         //Variabel penampung input keypad
int number = 0;         //Variabel penampung nilai angka
int password = 9999;    //Password

void setup() {
  lcd.init ();              //Mulai LCD
  lcd.setBacklight(HIGH);   //Nyalakan backlight
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("Input Password");      //Tampilan pada layar LCD
  
  customKey = customKeypad.getKey();    //Baca input keypad

  //------------Prosedur jika input berupa angka------------//
  switch(customKey){
    case '0' ... '9':
      lcd.setCursor(0,1);
      number = number * 10 + (customKey - '0');
      lcd.print(number);
    break;

    //------------Jika input '#' maka cek password------------//
    case '#':
      if(number == password){           //Jika password benar, maka
        lcd.setCursor(0,1);
        lcd.print("Access Accepted ");  //Tampilan LCD
        number = 0;
        lcd.clear(); 
      }
      else{                             //Jika salah, maka
        lcd.setCursor(0,1);
        lcd.print("Invalid Password");  //Tampilan LCD
        delay(2000);
        number = 0;
        lcd.clear();
      }
    break;

    //------------Jika input '*' maka hapus tampilan------------//
    case '*':
      number = 0;
      lcd.clear();
    break;
  }
}/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */