//FARUQ ABDULLAH ISMAIL D400200143
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include<EEPROM.h>
const int button = 2;
int status =0;
char password1[6] ,password2[6] , password3[6];
char customKey = 0;
int i = 0;
int value;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int RELAY_PIN  = 4; // PIN RELAY 
const int ROW_NUM    = 4; // 4 BARIS
const int COLUMN_NUM = 4; // 4 KOLOM
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
	};
byte pin_rows[ROW_NUM] = {12, 11, 10, 9}; 
byte pin_column[COLUMN_NUM] = {8, 7, 6, 5}; 
Keypad customKeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
  for (int j = 0; j < 4; j++)
    EEPROM.write(j, j + 49);
  for (int j = 0; j < 4; j++)
    password1[j] = EEPROM.read(j);
  Serial.begin(9600);
  lcd.init();                    
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print(" WELCOME BACK ");
  delay(1000);
  lcd.clear();
  lcd.print("Input Password :");
  lcd.setCursor(0, 1);
  pinMode(RELAY_PIN, OUTPUT); 
  digitalWrite(RELAY_PIN, LOW); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(button, INPUT);
}
void loop() {
    int data =digitalRead(button);
    if (data== HIGH){
      digitalWrite(3, HIGH);
      delay(3000);
      lcd.setCursor(0,0);
      lcd.print("PINTU TERBUKA  ");
      delay(5000);
      lcd.clear();
    }else {
      digitalWrite(3, LOW);
    }
  customKey = customKeypad.getKey();
  if (customKey == '#')
    change();
  if (customKey)
  {
    password1[i++] = customKey;
    lcd.print(customKey);
  }
  if (i == 4)
  {
    delay(200);
    for (int j = 0; j < 4; j++)
      password2[j] = EEPROM.read(j);
    if (!(strncmp(password1, password2, 4)))
    {
      lcd.clear();
      lcd.print(" Password Benar");
      digitalWrite(3, HIGH);
      lcd.setCursor(1,1);
      lcd.print("SILAHKAN MASUK");
      delay(5000);
      digitalWrite(3, LOW);
      lcd.setCursor(0, 1);
      lcd.print("#. Ubah Password ???");
      delay(1000);
      lcd.clear();
      lcd.print("Input Password :");
      lcd.setCursor(0, 1);
      
      i = 0;
    }
    else
    {
      digitalWrite(4, LOW);
      delay(1000);
      lcd.clear();
      lcd.print("Password Salah");
      lcd.setCursor(0, 1);
      lcd.print("#.Ubah Password???");
      delay(2000);
      lcd.clear();
      lcd.print("Input Password :");
      lcd.setCursor(0, 1);
      i = 0;
    }
  }
  }
void change()
{
  int j = 0;
  lcd.clear();
  lcd.print("PasswordSekarang:");
  lcd.setCursor(0, 1);
  while (j < 4)
  {
    char key = customKeypad.getKey();
    if (key)
    {
      password2[j++] = key;
      lcd.print(key);
    }
    key = 0;
  }
  delay(500);
  if ((strncmp(password2, password1, 6)))
  {
    lcd.clear();
    lcd.print("Password Salah..");
    delay(1000);
  }
  else
  {
    j = 0;
    lcd.clear();
    lcd.print("Password baru :");
    lcd.setCursor(0, 1);
    while (j < 4)
    {
      char key = customKeypad.getKey();
      if (key)
      {
        password1[j] = key;
        lcd.print(key);
        EEPROM.write(j, key);
        j++;
      }
    }
    lcd.print(" SUCCESS.....");
    delay(1000);
  }
  lcd.clear();
  lcd.print("Input Password :");
  lcd.setCursor(0, 1);
  customKey = 0;
}
NOCOMNCVCCGNDINLED1PWRRelay Module