#include <LiquidCrystal_I2C.h> // includes the LiquidCrystal Library 
#include <Keypad.h>


#define ledPIN 8
#define pinPIRpower 13
#define alarmSOUNDER 9

const int pinPIRsen = 12;

int seconds = 0;
int redColour = 0;                //initiate value of red pixel to 0
int greenColour = 0;            //initiate value of green pixel to 0
int blueColour = 255;               //initate value of blue pixel to 0
int State_PIRsen;        //initiat value of PIR output to 0
int State_PIRenable;  //initiat power of PIR output to OFF
int buttonState_RX = 0;           //Recieve Pin variable
int errorCount = 0;


long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg =0;
String password="1234";
String codeStatus="";
String codeEnter ="";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4}; //Row pinouts of the keypad
byte colPins[COLS] = { 3, 2, 1, 0}; //Column pinouts of the keypad

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS); 
LiquidCrystal_I2C lcd(0x27, 16, 2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 

void setup() { 
  lcd.begin(0,2); 
  pinMode(alarmSOUNDER, OUTPUT); // Set buzzer as an output
  pinMode(pinPIRsen, INPUT); // Sets the pinPIRsen as an Output
}

void loop() {
  if (activateAlarm) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm will be");
    lcd.setCursor(0,1);
    lcd.print("activated in");
   
    int countdown = 9; // 9 seconds count down before activating the alarm
    while (countdown != 0) {
      lcd.setCursor(13,1);
      lcd.print(countdown);
      countdown--;
      tone(alarmSOUNDER, 700, 100);
      delay(1000);
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm Activated!");
    activateAlarm = false;
    alarmActivated = true;
  }
    State_PIRsen = digitalRead(pinPIRsen);  
    Serial.print(State_PIRsen);
  if ((alarmActivated == true) && (State_PIRsen == 1)){
    
        tone(alarmSOUNDER, 1000); // Send 1KHz sound signal 
        lcd.clear();
        codeEnter = "Pass>";
        codeStatus = "Alarm Activated!";
        enterPassword();
    }
  if ((alarmActivated == true) && (State_PIRsen == 0)) {
        noTone(alarmSOUNDER); // Send 1KHz sound signal 
        codeEnter = "Disable>";
        codeStatus = "Alarm Activated!";
        enterPassword();
      }
    

  if (!alarmActivated) {
    if (screenOffMsg == 0 ){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("A - Activate");
      lcd.setCursor(0,1);
      lcd.print("B - Change Pass");
      screenOffMsg = 1;
    }
    keypressed = myKeypad.getKey();
     if (keypressed =='A'){        //If A is pressed, activate the alarm
      tone(alarmSOUNDER, 1000, 200);
      activateAlarm = true;            
    }
    else if (keypressed =='B') {
      lcd.clear();
      int i=1;
      tone(alarmSOUNDER, 2000, 100);
      tempPassword = "";
      lcd.setCursor(0,0);
      lcd.print("Current Password");
      lcd.setCursor(0,1);
      lcd.print(">");
      passChangeMode = true;
      passChanged = true;   
      while(passChanged) {      
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
         tempPassword += keypressed;
         lcd.setCursor(i,1);
         lcd.print("*");
         i++;
         tone(alarmSOUNDER, 2000, 100);
        }
      }
      if (i > 5 || keypressed == '#') {
        tempPassword = "";
        i=1;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Current Password");
        lcd.setCursor(0,1);
        lcd.print(">"); 
      }
      if ( keypressed == '*') {
        i=1;
        tone(alarmSOUNDER, 2000, 100);
        if (password == tempPassword) {
          tempPassword="";
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Set New Password");
          lcd.setCursor(0,1);
          lcd.print(">");
          while(passChangeMode) {
            keypressed = myKeypad.getKey();
            if (keypressed != NO_KEY){
              if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
                  keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
                  keypressed == '8' || keypressed == '9' ) {
                tempPassword += keypressed;
                lcd.setCursor(i,1);
                lcd.print("*");
                i++;
                tone(alarmSOUNDER, 2000, 100);
              }
            }
            if (i > 5 || keypressed == '#') {
              tempPassword = "";
              i=1;
              tone(alarmSOUNDER, 2000, 100);
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Set New Password");
              lcd.setCursor(0,1);
              lcd.print(">");
            }
            if ( keypressed == '*') {
              i=1;
              tone(alarmSOUNDER, 2000, 100);
              password = tempPassword;
              passChangeMode = false;
              passChanged = false;
              screenOffMsg = 0;
            }            
          }
        }
      }
    }
   }
 }
}

void enterPassword() {
  int k=5;
  tempPassword = "";
  activated = true;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(codeStatus);
  lcd.setCursor(0,1);
  lcd.print(codeEnter);
      while(activated) {
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
          tempPassword += keypressed;
          lcd.setCursor(k,1);
          lcd.print("*");
          k++;
          tone(alarmSOUNDER, 2000, 100);
        }
      }
      if (k > 9 || keypressed == '#') {
        tempPassword = "";
        k=5;
        tone(alarmSOUNDER, 2000, 100);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(codeStatus);
        lcd.setCursor(0,1);
        lcd.print(codeEnter);
      }
      if ( keypressed == '*') {
        if ( tempPassword == password ) {
          tone(alarmSOUNDER, 2000, 100);
          activated = false;
          alarmActivated = false;
          noTone(alarmSOUNDER);
          screenOffMsg = 0; 
        }
        else if (tempPassword != password) {
          k = k -1;
          lcd.setCursor(0,1);
          lcd.print("Wrong! Try Again");
          delay(2000);
          lcd.clear();
          lcd.setCursor(0,0);
          tone(alarmSOUNDER, 1000, 200);
          if (errorCount >= 2){
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print(" *** ALARM *** ");
            tone(alarmSOUNDER, 1000);
          }
          errorCount = errorCount+1;
          lcd.setCursor(0,1);
          lcd.print("Wrong! Try Again");
          lcd.setCursor(0,1);
          lcd.print(codeEnter);
        }
      }    
    }
}
// Custom function for the Ultrasonic sensor

/*
void deactivateAlarm() {
  int k=8;
  tempPassword = "";
  activated = true;
  lcd.setCursor(0,1);
  lcd.print(codeStatus);
    while(activated && (State_PIRsen == 0)) {
      State_PIRsen = digitalRead(pinPIRsen);  
      Serial.print(State_PIRsen);
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
          tempPassword += keypressed;
          lcd.setCursor(k,1);
          lcd.print("*");
          k++;
        }
      }
      if (k > 12 || keypressed == '#') {
        tempPassword = "";
        k=8;
        lcd.clear();
        lcd.setCursor(0,0);
        tone(alarmSOUNDER, 1000, 200);
        lcd.print("Wrong! Try Again");
        lcd.setCursor(0,1);
        lcd.print(codeStatus);
      }
      if ( keypressed == '*') {
        if ( tempPassword == password ) {
          activated = false;
          alarmActivated = false;
          noTone(alarmSOUNDER);
          screenOffMsg = 0; 
        }
        else if (tempPassword != password) {
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Wrong! Try Again");
          delay(2000);
          lcd.setCursor(0,0);
          tone(alarmSOUNDER, 1000, 200);
          if (errorCount >= 2){
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print(" *** ALARM *** ");
            tone(alarmSOUNDER, 1000);
          }
          errorCount = errorCount+1;
          lcd.setCursor(0,1);
          lcd.print(codeStatus);
        }
      }    
    }
}
// Custom function for the Ultrasonic sensor
*/