#include <Keypad.h>                 //Library keypad
#include <LiquidCrystal_I2C.h>    //Library LCD I2C
#include <ESP32Servo.h>
LiquidCrystal_I2C lcd(0x27,20,4);   //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] = { 14, 12, 19, 18 }; 
byte colPins[COLS] = { 5, 4, 2, 15 };

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 = 1379;    //Password
int ledmerah = 26;
int ledhijau = 25;
int buzer = 33;
byte ldr = 32;
Servo myservo;
int nilai

void setup(){
  pinMode(ledmerah, OUTPUT);    //Led Merah
  pinMode(ledhijau, OUTPUT);   //Led Hijau
  pinMode(buzer, OUTPUT);
  //pinMode(servoLock, OUTPUT);
  myservo.attach(27);
  lcd.init ();              //Mulai LCD
  lcd.setBacklight(HIGH);   //Nyalakan backlight
}
void loop() {
  Serial.begin(9600);        //LDR
  nilai = analogRead(ldr);   //LDR
  Serial.print("Nilai LDR: "); //LDR
  Serial.println(nilai);        //LDR

  lcd.setCursor(0,0);
  lcd.print("      Wellcome      "); //Tampilan pada layar LCD
  lcd.setCursor(0,1);
  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,2);
      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
        lcd.setCursor(0,2);
        lcd.print("   Open The Door!   ");  //Tampilan LCD
        delay(5000);
        number = 0;
        lcd.clear(); 
        digitalWrite(ledhijau, HIGH);
        digitalWrite(ledmerah, LOW);
        delay(100);
        noTone(buzer);
        //Kendali servo untuk membuka/menutup locker
        myservo.write(180);
        delay(5000);
        myservo.write(0);
        else if(nilai>500)
          myservo.write(180)
          delay(1000);
        
        
      }
      else{                             //Jika salah, maka
        lcd.setCursor(0,1);
        lcd.print("Invalid Password");  //Tampilan LCD
        delay(2000);
        number = 0;
        lcd.clear();
        digitalWrite(ledmerah, HIGH);
        digitalWrite(ledhijau, LOW);
        delay(100);
        tone(buzer, 400);  //Bib pertama
        delay(500);
        noTone(buzer);
        delay(400);
        tone(buzer, 400);  //Bib ke dua
        delay(500);
        noTone(buzer);
        delay(500);
        tone(buzer, 400);  //Bib ke tiga
        delay(500);
        noTone(buzer);
        delay(500);
      }
    break;

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