#include <Keypad.h>                 //Library keypad
#include <LiquidCrystal_I2C.h>    //Library LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2);   //Alamat I2C
#define LED_R 27
#define LED_B 26
int buzzer = 25;

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


void setup() {
  Serial.begin(115200);
  lcd.init ();              //Mulai LCD
  lcd.setBacklight(HIGH);   //Nyalakan backlight
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(buzzer, OUTPUT);

}

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
        delay(2000);
        digitalWrite(26, HIGH);
        digitalWrite(27, LOW);
        number = 0;
        lcd.clear(); 
      }
      else{                             //Jika salah, maka
        lcd.setCursor(0,1);
        lcd.print("Invalid Password");  //Tampilan LCD
        delay(2000);
        digitalWrite(27, HIGH);
        digitalWrite(26, LOW);
        tone(25, 1000);
        number = 0;
        lcd.clear();
      }
    break;

    //------------Jika input '*' maka hapus tampilan------------//
    case '*':
      number = 0;
      lcd.clear();
      digitalWrite(26, LOW);
      digitalWrite(27, LOW);
      noTone(buzzer);
    break;

    
    }

    }
  

/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */