#include <ESP32Servo.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

#define ledAlert 27

const uint8_t ROWS = 4;
const uint8_t COLS = 4;


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

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

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

Servo myservo1,myservo2,myservo3,myservo4,myservo5; //create servo object to control servo
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

char customKey;         //Variabel penampung input keypad
int number = 0;         //Variabel penampung nilai angka
int password1 = 1179;    //Password Loker 1
int password2 = 1279;    //Password Loker 2
int password3 = 1379;    //Password Loker 3
int password4 = 1479;    //Password Loker 4
int password5 = 1579;    //Password Loker 5


void setup(){
  myservo1.attach(23);
  myservo2.attach(13);
  myservo3.attach(32);
  myservo4.attach(25);
  myservo5.attach(33);

  // myservo1.write(180); // Posisi Buka
  // myservo2.write(180); // Posisi Buka
  // myservo3.write(180); // Posisi Buka
  // myservo4.write(180); // Posisi Buka
  // myservo5.write(180); // Posisi Buka

  myservo1.write(90); // Posisi Tutup
  myservo2.write(90); // Posisi Tutup
  myservo3.write(90); // Posisi Tutup
  myservo4.write(90); // Posisi Tutup
  myservo5.write(90); // Posisi Tutup

  pinMode(ledAlert, OUTPUT);
  digitalWrite(ledAlert, HIGH);

  LCD.init();
  LCD.backlight();
  LCD.setCursor(0, 0);
  LCD.print("M. NUZULUDDIN");
  LCD.setCursor(0, 1);
  LCD.print("SMART - PUSTAKA");
  delay(3000);
  LCD.clear();
}

void loop(){
  LCD.setCursor(0, 0);
  LCD.print("MASUKAN PASSWORD");
  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 == password1){           //Jika password benar, maka
        LCD.setCursor(0, 0);
        LCD.print("ACCESS ACCEPTED ");  //Tampilan LCD
        LCD.setCursor(0, 1);
        LCD.print("LOKER 1 OPEN");
        delay(1000);
        LCD.clear();
        LCD.setCursor(0,0);
        LCD.print("Silahkan Masukan");
        LCD.setCursor(0,1);
        LCD.print("Barang");
        delay(100);
        
        myservo1.write(180);
        delay(3000);
        myservo1.write(90);
      }
      else if(number == password2){
        LCD.setCursor(0, 0);
        LCD.print("ACCESS ACCEPTED ");  //Tampilan LCD
        LCD.setCursor(0,1);
        LCD.print("LOKER 2 OPEN");
        delay(1000);
        LCD.clear();
        LCD.setCursor(0,0);
        LCD.print("Silahkan Masukan");
        LCD.setCursor(0,1);
        LCD.print("Barang");
        delay(100);
        
        myservo2.write(180);
        delay(3000);
        myservo2.write(90);
      }
      else if(number == password3){
        LCD.setCursor(0, 0);
        LCD.print("ACCESS ACCEPTED ");  //Tampilan LCD
        LCD.setCursor(0,1);
        LCD.print("LOKER 3 OPEN");
        delay(1000);
        LCD.clear();
        LCD.setCursor(0,0);
        LCD.print("Silahkan Masukan");
        LCD.setCursor(0,1);
        LCD.print("Barang");
        delay(100);
        
        myservo3.write(180);
        delay(3000);
        myservo3.write(90);
      }
      else if(number == password4){
        LCD.setCursor(0, 0);
        LCD.print("ACCESS ACCEPTED ");  //Tampilan LCD
        LCD.setCursor(0,1);
        LCD.print("LOKER 4 OPEN");
        delay(1000);
        LCD.clear();
        LCD.setCursor(0,0);
        LCD.print("Silahkan Masukan");
        LCD.setCursor(0,1);
        LCD.print("Barang");
        delay(100);
        
        myservo4.write(180);
        delay(3000);
        myservo4.write(90);
      }
      else if(number == password5){
        LCD.setCursor(0, 0);
        LCD.print("ACCESS ACCEPTED ");  //Tampilan LCD
        LCD.setCursor(0,1);
        LCD.print("LOKER 5 OPEN");
        delay(1000);
        LCD.clear();
        LCD.setCursor(0,0);
        LCD.print("Silahkan Masukan");
        LCD.setCursor(0,1);
        LCD.print("Barang");
        delay(100);
        
        myservo5.write(180);
        delay(3000);
        myservo5.write(90);
      }
      else{                             //Jika salah, maka
        LCD.setCursor(0,1);
        LCD.print("INVALID PASSWORD");  //Tampilan LCD
        delay(200);
        digitalWrite(ledAlert, HIGH);
        delay(200);
        digitalWrite(ledAlert, LOW);
        delay(200);
        digitalWrite(ledAlert, HIGH);
        delay(200);
        digitalWrite(ledAlert, LOW);
        delay(200);
        digitalWrite(ledAlert, HIGH);
        delay(200);
        digitalWrite(ledAlert, LOW);
        delay(200);
        digitalWrite(ledAlert, HIGH);
      }
      delay(2000);
      number = 0;
      LCD.clear(); 

    break;

    //------------Jika input '*' maka hapus tampilan------------//
    case '*':
      digitalWrite(ledAlert, LOW);
      number = 0;
      LCD.clear();
    break;
  }
}