#include <ESP32Servo.h>
#include <Keypad.h>                
#include <LiquidCrystal_I2C.h>    
LiquidCrystal_I2C lcd(0x27,16,2);  
const int LED_1=27;
const int LED_2=26;
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] = { 23, 21, 19, 18 }; 
byte colPins[COLS] = { 5, 4, 2, 15 };

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);    

Servo servo1,servo2;
char customKey;        
int number = 0;        
int password1 = 0905;    
int password2 = 2000;

void setup() {
  servo1.attach(12);
  servo2.attach(13);

  servo1.write(90);
  servo2.write(90);

  lcd.init ();             
  lcd.setBacklight(HIGH);  
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);

  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);

  lcd.setCursor(0,0);
  lcd.print("NAMA: Fahmi Rustam R");      
  delay(1500);
  lcd.setCursor(0,1);
  lcd.print("NIM : 200604006");     
  delay(2000);
  lcd.clear();
  
}

void loop() {
  lcd.setCursor(2,0);
  lcd.print("MASUKAN PASSWORD");     
  customKey = customKeypad.getKey();   

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

    //------------Jika input '#' maka cek password------------//
    case '#':
      if(number == password1){
        digitalWrite(LED_2, HIGH);
        delay(800);
        digitalWrite(LED_2, LOW);
        delay(3000);
        lcd.clear();           
        lcd.setCursor(0,0);
        lcd.print("Access Accepted ");  
        lcd.setCursor(0,1);
        lcd.print("Loker 1 Buka");
        delay(1000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Silahkan Masukan");
        lcd.setCursor(0,1);
        lcd.print("Barang");
        delay(200);
        
        servo1.write(180);
        delay(3000);
        servo1.write(90); 
      }
      else if(number == password2){
        digitalWrite(LED_2, HIGH);
        delay(800);
        digitalWrite(LED_2, LOW);
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Access Accepted");
        lcd.setCursor(0,1);
        lcd.print("Loker 2 Buka");
        delay(1000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Silahkan Masukan");
        lcd.setCursor(0,1);
        lcd.print("Barang");
        delay(200);

        servo2.write(180);
        delay(3000);
        servo2.write(90);
        lcd.clear();
      }
      else{                             
        lcd.setCursor(0,1);
        lcd.print("Invalid Password");  
        number = 0;
        digitalWrite(LED_1, HIGH);
        delay(500);
        digitalWrite(LED_1, LOW);
        delay(3000);
        lcd.clear();
      }
    break;

    //------------Jika input '*' maka hapus tampilan------------//
    case '*':
      number = 0;
      lcd.clear();
    break;
  }
}