// LCD1602 to Arduino Uno connection example

//#include <LiquidCrystal.h>

//LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27 ,2,1,0,4,5,6,7,3, POSITIVE);
//Ubah alamat 0x27 dengan alamat i2C Milikmu

#define BUZZ 13
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;

int R1 = 12;

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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

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

void setup() {
pinMode(R1,OUTPUT);
 Serial.begin(9600);
 lcd.begin(16,2);
tone(BUZZ,5000);
lcd.setCursor(0,0);
lcd.print(F("Door Lock System"));
lcd.setCursor(0,1);
lcd.print("     Control");
delay (3000);
standby();
}
void standby(){
  lcd.clear();
lcd.print(F("  Tekan # Untuk"));
lcd.setCursor(0,1);
lcd.print(" Input Password");
noTone(BUZZ);
}
void loop() {
digitalWrite(R1,LOW);
char keypressed = keypad.getKey();
if(keypressed == '#')
{
  tone(BUZZ,1200);
  delay(100);
  noTone(BUZZ);
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("Masukan Kode");
  keypressed = keypad.waitForKey();
  if (keypressed != NO_KEY)
  {
    c1 = keypressed;
    tone(BUZZ,1200);
    delay(100);
  noTone(BUZZ);
    lcd.setCursor(5,1);
    lcd.print("*");
  }
  keypressed = keypad.waitForKey();
  if (keypressed = keypad.waitForKey());
  if(keypressed != NO_KEY) 
{
  c2 =  keypressed;
  tone(BUZZ,1200);
  delay(100);
  noTone(BUZZ);
  lcd.setCursor(6,1);
  lcd.print("*");
}
keypressed = keypad.waitForKey();
if(keypressed != NO_KEY) 
{
    c3 =  keypressed;
    tone(BUZZ,1200);
    delay(100);
  noTone(BUZZ);
    lcd.setCursor(7,1);
    lcd.print("*");
}
keypressed = keypad.waitForKey();
if(keypressed != NO_KEY) 
{
  c4 =  keypressed;
  tone(BUZZ,1200);
  delay(100);
  noTone(BUZZ);
  lcd.setCursor(8,1);
    lcd.print("*");
}
keypressed = keypad.waitForKey();
if(keypressed != NO_KEY) 
{
  c5 =  keypressed;
  tone(BUZZ,1200);
  delay(100);
  noTone(BUZZ);
  lcd.setCursor(9,1);
    lcd.print("*");
}
keypressed = keypad.waitForKey();
if(keypressed != NO_KEY) 
{
  c6 =  keypressed;
  tone(BUZZ,1200);
  delay(100);
  noTone(BUZZ);
  lcd.setCursor(10,1);
    lcd.print("*");
}
i1 = (c1-48)*100000;
i2 = (c2-48)*10000;
i3 = (c3-48)*1000;
i4 = (c4-48)* 100;
i5 = (c5-48)* 10;
i6 = (c6-48);

tot=i1+i2+i3+i4+i5+i6;
  
  if (tot == code ||tot == code1)
  {
  tone(BUZZ,4000);
  delay(1000);
  tone(BUZZ,5000);
  delay(1000);
  tone(BUZZ,5000);
  delay(1000);
  tone(BUZZ,7000);
  noTone(BUZZ);
  delay(1000);
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("Selamat Datang");
    lcd.setCursor(5,1);
    lcd.print("MR.ART");
    digitalWrite(R1,HIGH);
    delay(4000);
    balik();
  }
  else 
  {
    lcd.clear();
    tone(BUZZ,1200);
    lcd.setCursor(1,0);
    lcd.print("Password Salah");
    delay(3000);
    lcd.clear();
    noTone(BUZZ);
    lcd.print(F("  Tekan # Untuk"));
    lcd.setCursor(0,1);
    lcd.print(" Input Password");
  }
  }
}
void balik(){
digitalWrite(R1,HIGH);
lcd.clear();
lcd.print(F("  Tekan * Untuk"));
lcd.setCursor(0,1);
lcd.print(" Mengunci Pintu");
noTone(BUZZ);
char keypressed = keypad.getKey();
keypressed = keypad.waitForKey();
if(keypressed == '*') 
{
 tone(BUZZ,4000);
 delay(1000);
 tone(BUZZ,7000);
 noTone(BUZZ);
  standby(); 
} else {
  balik();
}
}