#include <Keypad.h>
//#include<LiquidCrystal.h>
#include<EEPROM.h>
//LiquidCrystal lcd(9,8,7,6,5,4);
char password[7];
char pass[7],pass1[7];
int i=0;
char customKey=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22,23,24,25}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26,27,28,29};   //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int led;
int buzzer = 7;
int m11;
int m12;
int output = 13;

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

  pinMode(11, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(output, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(m11, OUTPUT);
  pinMode(m12, OUTPUT);
  pinMode(4, OUTPUT);

  Serial.print(" Electronic ");
  Serial.print(" Bomb Lock ");
  delay(2000);
  Serial.println("Enter Passkey:");

  EEPROM.write(0,54);
  EEPROM.write(1,53);
  EEPROM.write(2,54);
  EEPROM.write(3,51);
  EEPROM.write(4,56);
  EEPROM.write(5,52);  
  EEPROM.write(6,57);

  for(int j=0;j<7;j++){
    pass[j]=EEPROM.read(j);
    int potato = pass[j];
    Serial.println(potato);

  }
}

void loop()
{
  digitalWrite(11, HIGH);
  digitalWrite(OUTPUT, LOW);
  customKey = customKeypad.getKey();
  if(customKey=='#')
    change();
  if (customKey)
  {
    password[i++]=customKey;

    Serial.print(customKey);
    beep();
  }
  if(i==7)
  {
    digitalWrite(11, HIGH);
    delay(200);
    for(int j=0;j<7;j++){
     int potatat = pass[j];
     Serial.println(potatat);
     pass[j]=EEPROM.read(j);
    }   
    if((!strncmp(password, pass,7)))
    {
      digitalWrite(led, HIGH);
      digitalWrite(output, HIGH);
      beep();
      beep();
      delay(100);
      beep();
      beep();
      delay(100);
      beep();
      beep();
      beep();
      beep();
      beep();
      delay(100);
      beep();
      beep();
      delay(100);
      beep();
      beep();
      beep();
      beep();
      beep();
      delay(100);
      beep();
      beep();
      delay(100);
      beep();
      beep();
      beep();
      Serial.println("Passkey Accepted");
      digitalWrite(11, LOW);
      delay(10000);
      i =0;
    }
    else
    {
      digitalWrite(11, HIGH);
      digitalWrite(buzzer, HIGH);
      Serial.println("Access Denied...");
      delay(2000);
      Serial.println("Enter Passkey:");
      i=0;
      digitalWrite(buzzer, LOW);
    }
  }
}
void change()
{
  int j=0;
  Serial.println("Current Password");
  while(j<7)
  {
    char key=customKeypad.getKey();
    if(key)
    {
      pass1[j++]=key;
      Serial.print(key);
      beep();
    }
    key=0;
  }
  delay(500);

  if((strncmp(pass1, pass, 7)))
  {
    Serial.println("Wrong Passkey...");
    Serial.println("Better Luck Again");
    delay(1000);
  }
  else
  {
    j=0;
    Serial.println("Enter New Pass:");
    while(j<7)
    {
      char key=customKeypad.getKey();
      if(key)
      {
        pass[j]=key;
        Serial.print(key);
        //EEPROM.write(j,key);
        j++;
        beep();
      }
    }
    Serial.println(" Done......");
    delay(1000);
  }
  Serial.println("Enter Password:");
  customKey=0;
}
void beep()
{
  digitalWrite(buzzer, HIGH);
  delay(20);
  digitalWrite(buzzer, LOW);
}
