/*
#include <Wire.h> 
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Keypad.h>

#include <EEPROM.h>
unsigned char key,menu,urut;
unsigned char pw1,pw2,pw3,pw4,pw5,pw6,i;
unsigned char pw_kpd1,pw_kpd2,pw_kpd3,pw_kpd4,pw_kpd5,pw_kpd6;
unsigned char cek,cnt;
unsigned char fkpd; 
char pw[7],pw_kpd[7];
char s,t;  

//----------------------------INISIALISASI KONEKSI KEYPAD 4X4
//int code = 290594; //Password Pertama Ganti Pada Bagian Ini
//int code1 = 123456; //Password Kedua Ganti Pada Bagian Ini
int tot,i1,i2,i3,i4,i5,i6;
char c1,c2,c3,c4,c5,c6;
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]={13,12,14,27};
byte colPins[COLS]={26,25,33,32};
Keypad keypad = Keypad(makeKeymap(keys),rowPins, colPins, ROWS, COLS);
//#define BUZZ 35
//int R1 = 18;



char ssid [] = "Wokwi-GUEST";
char pass [] = "";


void setup(){
  Serial.begin(115200);
  WiFi.begin(ssid,pass);
  //pinMode(35,OUTPUT);
  //pinMode(9,OUTPUT);
  //digitalWrite(35,0);
  //digitalWrite(9,0);
  lcd.init();
  lcd.backlight();
  lcd.begin(16, 2);
  lcd.print(" Kunci Elektrik");
  lcd.setCursor(0,1);
  lcd.print("Dengan Password");
  delay(2000);
  //---------------------cek alamat 0
  cek=EEPROM.read(0);  
if(cek==0xFF){
   cek=1;   
   EEPROM.write(0,'1');     
   pw1='1'; EEPROM.write(1,'1');
   pw2='1'; EEPROM.write(2,'1');
   pw3='1'; EEPROM.write(3,'1');
   pw4='1'; EEPROM.write(4,'1');
   pw5='1'; EEPROM.write(5,'1');
   pw6='1'; EEPROM.write(6,'1');
   }
else{
   pw1=EEPROM.read(1);
   pw2=EEPROM.read(2);
   pw3=EEPROM.read(3);
   pw4=EEPROM.read(4);
   pw5=EEPROM.read(5);
   pw6=EEPROM.read(6);
   }
fkpd=0; 
delay(200);
}


void loop(){
     //----------------------------- display time
  digitalWrite(8,1);
  lcd.clear();
  lcd.print("Silakan kunci...");
  lcd.setCursor(0,1);
  lcd.print("Tekan #");
  fkpd=2;
  goKeypad();
  lcd.clear();
  lcd.print("Pintu Terkunci");
  delay(3000); 
pass:
  lcd.clear();
  lcd.print("Masukan Password");
  lcd.setCursor(0,1);
  fkpd=1;
  menu=0;
  goKeypad();
  if(menu==4) goto pass;
}
//---------------------------- keypad 
void goKeypad(){
do{
  key=keypad.getKey();
  if(key != NO_KEY){
    
    if(key=='*' && fkpd==1){  
      lcd.clear();
      lcd.print("Ganti Password");
      lcd.setCursor(0,1);
      lcd.print("PW lama=");  
      menu=1;
      delay(300); 
    }  
    
   else if(key=='#' && fkpd==2){
      fkpd=0;
      digitalWrite(8,0);
      delay(250);
     }
  
 
   else if ((key=='C')&&(fkpd == 1)){  //C
      if(menu==3){          
         lcd.clear();         
         lcd.print("PW baru:"); 
         menu=2; 
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         }
      else if(menu==2){         
         menu=4;
         fkpd=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         } 
      else if((menu==1)||(menu==0)){
         menu=4;
         fkpd=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         cnt=0;
         }
      delay(250);
      }
      
   else if (key=='D'&& menu==3){
      lcd.clear();
      lcd.print("Simpan PW Baru..");    
      EEPROM.write(1,pw_kpd[0]);
      EEPROM.write(2,pw_kpd[1]);
      EEPROM.write(3,pw_kpd[2]);
      EEPROM.write(4,pw_kpd[3]);
      EEPROM.write(5,pw_kpd[4]);
      EEPROM.write(6,pw_kpd[5]);
      EEPROM.write(0,1);
      pw1 = pw_kpd[0];
      pw2 = pw_kpd[1];
      pw3 = pw_kpd[2];
      pw4 = pw_kpd[3];
      pw5 = pw_kpd[4];
      pw6 = pw_kpd[5];
      
      fkpd = 0;
      for(t=0;t<6;t++)pw_kpd[t]=' ';
      delay(3000);
      menu = 4;
     }
      
    else if(menu<2 && fkpd==1 && key!='A' && key!='B'
       && key!='C' && key!='D' && key!='*' && key!='#'){       
       cek_password();
    }
    else if(menu==2 && fkpd==1&& key!='A' && key!='B'
       && key!='C' && key!='D' && key!='*' && key!='#'){
       entri_password();         
    }
  
 }  
}
 while(fkpd);
}


void cek_password(){   
   pw_kpd[cnt]=key;
   cnt++;                           
   lcd.print('*');                 
//   lcd.print(char(key));  
   if((cnt==6)&&(menu==0)){
     delay(500);
      if((pw1==pw_kpd[0])&&(pw2==pw_kpd[1])&&(pw3==pw_kpd[2])&&(pw4==pw_kpd[3])&&(pw5==pw_kpd[4])&&(pw6==pw_kpd[5])){
         lcd.clear();
         lcd.print("Password Benar");
         lcd.setCursor(0,1);
         lcd.print("Silakan Masuk");
         digitalWrite(8,1);
         digitalWrite(9,1);
         delay(200);
         digitalWrite(9,0);
         delay(200);
         digitalWrite(9,1);
         delay(200);
         digitalWrite(9,0);
         cnt=0;
         fkpd=0;
         s=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         delay(3000);
         }
      else {
         lcd.clear();
         lcd.print("Password Salah");
         s++;
         if(s < 3){
            digitalWrite(9,1);
            delay(2000);
            digitalWrite(9,0);
            lcd.clear();
            lcd.print("Masukan Password");
            lcd.setCursor(0,1);
            cnt=0;
            for(t=0;t<6;t++)
            pw_kpd[t]=' ';
            }
         else if (s==3){
            lcd.clear();
            lcd.print("Anda 3x Salah");
            lcd.setCursor(0,1);
            lcd.print("Tunggu 5 Menit");
            for(i=0;i<75;i++){
               digitalWrite(9,1);
               delay(2000);
               digitalWrite(9,0);
               delay(2000);
               }                    
            lcd.clear();
            lcd.print("Masukan Password");
            lcd.setCursor(0,1);
            cnt=0;
            s=0;
            for(t=0;t<6;t++)pw_kpd[t]=' ';
            }
         }
      }
   else if((cnt==6)&&(menu==1)){
      if((pw1==pw_kpd[0])&&(pw2==pw_kpd[1])&&(pw3==pw_kpd[2])&&(pw4==pw_kpd[3])&&(pw5==pw_kpd[4])&&(pw6==pw_kpd[5])){
         lcd.clear();
         lcd.print("Password Benar");
         lcd.setCursor(0,1);
         delay(2000);
         lcd.clear();
         lcd.print("PW baru:");
         cnt=0;
         menu=2;
         s=0;
         for(t=0;t<6;t++)pw_kpd[t]=' ';
         }
      else {
         s++;
         if(s < 3){            
             lcd.clear();
             lcd.print("Password Salah");
             cnt=0;
             delay(2000);
             lcd.clear();
             lcd.print("Ganti Password");
             lcd.setCursor(0,1);
             lcd.print("PW lama=");
             for(t=0;t<6;t++)pw_kpd[t]=' ';
            }
         else if (s==3){
            lcd.clear();
            lcd.print("Anda 3x Salah");
            lcd.setCursor(0,1);
            lcd.print("Tunggu 5 Menit");
            for(i=0;i<75;i++){
               digitalWrite(9,1);
               delay(2000);
               digitalWrite(9,0);
               delay(2000);
               }                    
            lcd.clear();
            lcd.print("Ganti Password");
            lcd.setCursor(0,1);
            lcd.print("PW lama=");
            cnt=0;
            s=0;
            for(t=0;t<6;t++)pw_kpd[t]=' ';
            }
      }
   }    
}

void entri_password(){
   lcd.print(char(key));
   pw_kpd[cnt]=key;
   cnt++;   
   if(cnt==6){
      lcd.setCursor(0,1);
      lcd.print("D=save,C=cancel");
      menu=3;
      cnt=0;
      
      }
}
*/

/*AT-MO PRODUCTION
membuat Password menggunakan Keypad dan Touch Sensor
tolong jika menggunakan code ini
mohon masukan sumber seperti di bawah ini
"AT-MO PRODUCTION 
EDIT BY OGI SINATRA"
*/

#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//#define TouchSensor 10 // Pin Di gunakan untuk capactitive touch sensor

//#define RELAY_ON 0
//#define RELAY_OFF 1
//#define RELAY_1  12   // pin yang digunakan yaitu pin 12

byte currentLength = 0;

boolean currentState = LOW;
boolean lastState = LOW;
boolean RelayState = LOW;

Password password = Password("1234");//Password bisa di ubah ubah
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]={13,12,14,27};
byte colPins[COLS]={26,25,33,32};
Keypad keypad = Keypad(makeKeymap(keys),rowPins, colPins, ROWS, COLS);




Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
Serial.begin(115200);

  //pinMode(red, OUTPUT);
  //pinMode(RELAY_1, OUTPUT);
  //pinMode(TouchSensor, INPUT);
  //digitalWrite(RELAY_1, RELAY_OFF);

  //sumber diambil pada examples di library password.
  keypad.addEventListener(keypadEvent);

  lcd.begin(16, 2);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("Password");
  
  lcd.setCursor(0, 1);
  lcd.print("AT-MO PRODUCTION");
  delay(5000);
  lcd.clear();
}

// Jika tombol ditekan, tombol ini disimpan dalam 'keypressed' variabel
// Jika kunci tidak sama dengan 'NO_KEY', maka tombol ini dicetak
// Jika jumlah = 17, maka penghitungan reset kembali ke 0 (ini berarti tidak ada tombol yang ditekan selama proses pemindaian seluruh keypad

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Masukan Password");
  lcd.setCursor(0,1);
  lcd.print("Pass: ");
  keypad.getKey();;

//code touch sensor
/*
  currentState = digitalRead(TouchSensor);
  if (currentState == HIGH && lastState == LOW) {
    Serial.println("Berhasil Buka Dari Dalam");
    delay(1);

    if (RelayState == HIGH) {
      //digitalWrite(RELAY_1, RELAY_OFF);
      RelayState = LOW;
      
    } 
    else {
      //digitalWrite(RELAY_1, RELAY_ON);
      RelayState = HIGH;
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("Berhasil Buka");
      lcd.setCursor(3,1);
      lcd.print("dari Dalam");
      delay(5000);//Lama waktu relay
      lcd.clear();
      password.reset();
      //digitalWrite(RELAY_1, RELAY_OFF);
    }
    */
  }
  lastState = currentState;
  
}


void checkPassword(){
   if(password.evaluate()){        
      //digitalWrite(RELAY_1, RELAY_ON); 
      lcd.clear();
      lcd.print("Berhasil");
      Serial.println("Berhasil Dari keypad");
      delay(5000);//Lama waktu relay
      password.reset();
      //digitalWrite(RELAY_1, RELAY_OFF);
   }
   else {
     // digitalWrite(red, HIGH);
      lcd.clear();
      lcd.print("Salah coba lagi");
      Serial.println("Salah coba lagi");
      delay(5000);//lama led on 
      password.reset();
     // digitalWrite(red, LOW);  
   }
}


void keypadEvent(KeypadEvent eKey){
  
  switch (keypad.getState()){  
    case PRESSED:
  lcd.setCursor(0,1);
    lcd.print(eKey);
    Serial.println(eKey);
  switch (eKey){
    case '*': checkPassword(); 
    lcd.clear(); currentLength=0;break;
    case '#': password.reset();
    lcd.clear();currentLength=0; break;
    default:// password.append(eKey);
          password << eKey;
          currentLength++;
     
        //Print some feedback.
        lcd.setCursor(0,1);
        lcd.print("Pass: ");
        for (byte i=0; i<currentLength; i++){
            lcd.print('*');
        }
           
   }
  }  
}