Having problem with my DIY alarm system code

Hello everyone :slight_smile: ,

I'm having a problem inside my code here.

/*
Transplant Wiring
Arduino GND   -> Breadboard
Arduino +5V   -> Breadboard
Arduino 8     -> Speaker
Arduino 7     -> Reed_Switch
Arduino 4     -> PIR_Sensor
Arduino A0    -> Photo_Resistor
Arduino A4    -> PCF8574 - LCD/Keypad
Arduino A5    -> PCF8574 - LCD/Keypad
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <i2ckeypad.h>

#define Speaker 8
#define Switch 7
#define PIR 4
#define P_R A0
#define PCF8574_ADDR 0x20

#define ROWS 4
#define COLS 4



char* ourCode = "1234";
int currentPosition = 0;
//keypad variable

int reedPin = 0;
//Reed Switch variable

int pirPin = 0;
//PIR Sensor variable

int phPin = 0;
//Photo Resistor variable

int alarmActive = 0;
int zone;


LiquidCrystal_I2C lcd (0x38, 16, 2); // set the LCD address to 0x20 for a 16 chars and 2 line display
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);

void setup()
{
 pinMode(Speaker, OUTPUT);
 //Speaker variable

 pinMode(PIR, INPUT);
 //PIR Sensor variable

 pinMode(Switch, INPUT);
 digitalWrite(Switch, HIGH);
 //Reed Switch variable

 pinMode(P_R, INPUT);
 //Photo Resistor variable

 Wire.begin();

 kpd.init();
 //keypad variable

 lcd.init();
 lcd. backlight();
 displayEntryCodeScreen();

}

void loop()
{

 checkPass();

 if (alarmActive == 1)
 {
   pirPin = digitalRead(PIR);
   phPin = analogRead(P_R);
   reedPin = digitalRead(Switch);

   if (reedPin == HIGH && alarmStatus == 0)
   {

     zone = 0;
     alarmStatus = 1;
     alarmTriggered();
   }


   else if (pirPin == HIGH && alarmStatus == 0)
   {

     zone = 1;
     alarmStatus = 1;
     alarmTriggered();
   }

   else if (phPin < 700 && alarmStatus == 0)
   {

     zone = 2;
     alarmStatus = 1;
     alarmTriggered();
   }

 }

}

void checkPass()
{
 char key = kpd.get_key();
 if (int(key) != 0)
 {
   lcd.setCursor(14, 3);
   lcd.print("     ");
   lcd.setCursor(14, 3);

   if (key == ourCode[currentPosition])
   {
     ++currentPosition;
     if (currentPosition == 4 && alarmActive == 0 )
     {
       armAlarm();
       currentPosition = 0;
     }
     else if (currentPosition == 4  && alarmActive == 1 )
     {
       disarmAlarm();
       currentPosition = 0;
     }
   }
   else if (key != ourCode[currentPosition] && alarmActive == 0)
   {
     invalidCode_1();
     currentPosition = 0;
   }
   else if (key != ourCode[currentPosition] && alarmActive == 1)
   {
     invalidCode_2();
     currentPosition = 0;
   }
 }
}
void armAlarm()// alarm armed and countdown start
{
 clearScreen();
 lcd.setCursor(0, 0);
 lcd.print("   Alarm ARMED    ");
 noTone(Speaker);
 delay(1000);
 countDown_1();

}

void countDown_1()// alarm countdown
{
 lcd.setCursor(0, 0);
 lcd.print("Countdown Start  ");
 int count = 5;
 while (count != 0 )
 {
   count--;
   lcd.setCursor(0, 1);
   lcd.print("------ ");
   lcd.print(count);
   lcd.print(" -------");
   delay(1000);
 }
 if (count == 0)
 {
   clearScreen();
   lcd.setCursor(0, 0);
   lcd.print("<<System READY>>");
   alarmActive = 1;
 } // when countdown reach zero


}
void countDown_2()// entry sensor countdown
{
 clearScreen();
 lcd.setCursor(0, 0);
 lcd.print("-Enter Password-");
 int count = 10;
 char key = kpd.get_key();

 attachInterrupt(0x20, checkPass, HIGH);

 while ( count != 0) // ten seconds to enter the correct code
 {
   count--;
   lcd.setCursor(0, 1);
   lcd.print("------ ");
   lcd.print(count);
   lcd.print(" -------");
   delay(1000);


   if (count == 0)  // if countdown reach zero alarm will triggered
   {
     lcd.setCursor(0, 0);
     lcd.print("Countdown Ended ");
     lcd.setCursor(0, 1);
     lcd.println("Alarm Triggered!");
     tone(Speaker, 1200);
   }

 }

}


void disarmAlarm()// code for disarming alarm
{
 lcd.setCursor(0, 0);
 lcd.print(" Alarm DISARMED ");
 lcd.setCursor(0, 1);
 lcd.println(" Access GRANTED  ");
 noTone(Speaker);

 delay(1000);
 displayEntryCodeScreen();
}

void invalidCode_1()// when alarm is not armed even if password input is incorrect no tone will be sound
{
 lcd.setCursor(0, 0);
 lcd.print("  Access DENIED ");
 lcd.setCursor(0, 1);
 lcd.println("  Invalid code  ");

}

void invalidCode_2()// when alarm is not armed even if password input is incorrect no tone will be sound
{

 lcd.setCursor(0, 0);
 lcd.print("  Access DENIED ");
 lcd.setCursor(0, 1);
 lcd.println("  Invalid code  ");
 tone (Speaker, 1200);
}

void clearScreen()// code for clearing the screen
{
 lcd.setCursor(0, 0);
 lcd.print("                          ");
 lcd.setCursor(0, 1);
 lcd.print("                          ");
}

void displayEntryCodeScreen()// code for displaying the entry code screen
{
 clearScreen();
 lcd.setCursor(0, 0);
 lcd.print("-Enter Password-");
}



void alarmTriggered() // when alarm is triggered, which zone will be printed on the LCD and tone will play
{

 clearScreen();
 lcd.setCursor(0, 0);
 lcd.print("Alarm Triggered!");


 if (zone == 0)// if zone zero is triggered
 {
   lcd.setCursor(0, 1);
   lcd.print("Countdown Start ");
   countDown_2();
 }

 else if (zone == 1)// if zone one is triggered
 {
   lcd.setCursor(0, 1);
   lcd.print("Motion in Zone 1 ");
   tone(Speaker, 1200);
 }

 else if (zone == 2)// if zone two is triggered
 {
   lcd.setCursor(0, 1);
   lcd.print("Motion in Zone 2 ");
   tone(Speaker, 1200);
 }
}

void entryTriggered()
{
 lcd.setCursor(0, 0);
 lcd.print("Countdown Ended ");
 lcd.setCursor(0, 1);
 lcd.println("Alarm Triggered!");
 tone(Speaker, 1200);

}

Most of the loop works fine, but I'm struggling over for the reed switch countdown loop, while it is counting down, I wish to enter my password and stop the countdown, any suggestion? One more problem is, if I triggered one of the alarm and disarm it, the second time I armed the alarm it cannot detect anything on all the sensors any more, please help!![tt][/tt]

Use code tags. Please edit your post.

Done :slight_smile:

DarkxRift:
but I'm struggling over for the reed switch countdown loop, while it is counting down, I wish to enter my password and stop the countdown, any suggestion?

Which part of the 300 lines of code deals with that?

I would record the value of millis() when the password entry begins and check regularly to see if the entry time has expired.

...R

maybe you more/another state

once the sensor is tripped, it goes into PASSWORD_WAIT state and your countdown begins.

something like this outline:

#define TIMEOUT_INTERVAL 15000UL  // 15 seconds to disarm

enum AlarmState{
  DISARMED = 0,
  ARMED,
  PASSWORD_WAIT,
  ALARM
};

AlarmState alarmState = DISARMED;

bool sensorTripped = false;
unsigned long passwordTimeout;

void setup() 
{
  // put your setup code here, to run once:

}

void loop() 
{
  if(alarmState == DISARMED)
  {
    if(sensorTripped) 
    {
      alarmState = PASSWORD_WAIT;
      passwordTimeout = millis();
    }
    // do other DISARMED stuff
  }
  else if (alarmState == ARMED)
  {
    // do ARMED stuff
  }
  else if (alarmState == PASSWORD_WAIT)
  {
    while (millis() - passwordTimeout > TIMEOUT_INTERVAL)
    {
      checkPass();
    }
    alarmState = ALARM;
  }
  else if (alarmState == ALARM)
  {
    //makeNoise();
  }
}

void checkPass()
{
  //
  //if(PassWordIsCorrect)
  {
    alarmState = DISARMED;
  }
}

Robin2:
Which part of the 300 lines of code deals with that?

I would record the value of millis() when the password entry begins and check regularly to see if the entry time has expired.

...R

The countDown_2() loop isdoing that operation, as for millis, I don't actually know how to implement in my project.. :frowning:

Thank you BulldogLowell!! I think you have sort out my problem, actually I'm making a 3 zone alarm sensor, PIR sensor and photo resistor is zone 1 and 2, reed switch is the front door, when it is triggered, it gives the person 10 seconds to type in the password or else it will go off. The reason I have two countdown loop is because when the person type in the password to arm the alarm, there is 5 seconds for the person to leave the area, the other countdown is for the reed switch whenever it is triggered.

This is from Reply #4

 else if (alarmState == PASSWORD_WAIT)
  {
    while (millis() - passwordTimeout > TIMEOUT_INTERVAL)
    {
      checkPass();
    }
    alarmState = ALARM;
  }

I don't see how that will trigger the alarm if the checkPass() function does not complete within the allowed interval. Can't the code just stay indefinitely in the checkPass() function?

The use of millis() is illustrated in Several Things at a Time

...R

BulldogLowell, I'm having issue with this part of code:
if I upload this part, it will straight away give me the Alarm DISARMED ,Access GRANTED screen before I have the chance to type in my password :frowning:

if (alarmState == DISARMED)
  {

    if (sensorTripped)
    {
      alarmState = PASSWORD_WAIT;
      passwordTimeout = millis();
    }
    disarmAlarm();
  }
void disarmAlarm()// code for disarming alarm
{
  lcd.setCursor(0, 0);
  lcd.print(" Alarm DISARMED ");
  lcd.setCursor(0, 1);
  lcd.println(" Access GRANTED  ");
  noTone(Speaker);

  delay(1000);
  displayEntryCodeScreen();
}

I started to read the code and to be honest it looked like you started with a bad plan. Using "while" and "delay" is never going to work on a alarm system that needs to be scanning the program constantly.

Theres also parts of your code like alarmStatus=1 that seems ok except theres no where that I can see that alarmStatus is ever set back to 0.

I think taking a step back and learning millis timing will fix most of the problems you are seeing. Also look at references to "while" as that is creating a small loop where the arduino does nothing else until the while is satisfied so using it to do some graphics on the lcd means everything else in the program is not being done.

DarkxRift:
BulldogLowell, I'm having issue with this part of code:
if I upload this part, it will straight away give me the Alarm DISARMED ,Access GRANTED screen before I have the chance to type in my password :frowning:

yeah, it needed a bit more fleshing out...

Take a look at this (albeit a very wonky password check function) you can see how it works its way through the states...

  /*
Transplant Wiring
Arduino GND   -> Breadboard
Arduino +5V   -> Breadboard
Arduino 8     -> Speaker
Arduino 7     -> Reed_Switch
Arduino 4     -> PIR_Sensor
Arduino A0    -> Photo_Resistor
Arduino A4    -> PCF8574 - LCD/Keypad
Arduino A5    -> PCF8574 - LCD/Keypad
  */

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//#include <i2ckeypad.h>

#define TIMEOUT_INTERVAL 15000UL  // 15 seconds to disarm
#define LIGHT_THRESHOLD 700

enum AlarmState {
  READY_TO_ARM = 0,
  EXIT_NOW,
  ARMED,
  PASSWORD_WAIT,
  ALARM,
  NOT_READY
};

const char* const alarmText[] = {"**READY TO ARM**", "**ALARM  ARMED**", "*****ARMED******", " ENTER PASSWORD ", "**** ALARM ****", "***NOT  READY***"};

const char* myPassword = "1234";

AlarmState alarmState = NOT_READY;
AlarmState lastState = READY_TO_ARM;

byte doorSensor = 7;
bool doorSensorTripped = false;

byte pirSensor = 4;
bool pirSensorTripped = false;

byte lightSensor = A0;
bool lightSensorTripped = false;

bool keyPadArmed = false;
bool passWordIsCorrect = false;
unsigned long passwordTimeout;
unsigned long armedTimeout;

LiquidCrystal_I2C lcd (0x38); // set the LCD address to 0x20 for a 16 chars and 2 line display

void setup()
{
  Serial.begin(9600);
  Serial.println(alarmText[alarmState]);
  pinMode(doorSensor, INPUT_PULLUP);
  pinMode(pirSensor, INPUT);
  Wire.begin();
  lcd.begin(16,2);
  lcd.backlight();
  lcd.home ();
  lcd.print("***Weclome To***");
  lcd.setCursor(0,1);
  lcd.print("****My Alarm****");
  delay(3000);
}

void loop()
{
    // perform sensor checks all the time
  doorSensorTripped = !digitalRead(doorSensor);
  pirSensorTripped = digitalRead(pirSensor);
  lightSensorTripped = analogRead(lightSensor) > LIGHT_THRESHOLD;
    //

  if (alarmState == READY_TO_ARM)
  {
    if (checkPass()) // all you do is wait for keyPad
    {
      alarmState = EXIT_NOW;
      armedTimeout = millis();
    }
    if (doorSensorTripped || pirSensorTripped || lightSensorTripped)
    {
      alarmState = NOT_READY;
    }
  }
  else if (alarmState == ARMED)
  {
    if (doorSensorTripped)
    {
      alarmState = PASSWORD_WAIT;  // perhaps only one sensor (door allows for the timeout)
      passwordTimeout = millis();
    }
    if (pirSensorTripped || lightSensorTripped)
    {
      alarmState = ALARM;
    }
    if (checkPass())
    {
      alarmState = NOT_READY;
    }
  }
  else if (alarmState == PASSWORD_WAIT)
  {
    if (millis() - passwordTimeout > TIMEOUT_INTERVAL)
    {
      alarmState = ALARM;
    }
    if (checkPass())
    {
      alarmState = READY_TO_ARM;
    }
  }
  else if (alarmState == EXIT_NOW)
  {
    if (millis() - armedTimeout < TIMEOUT_INTERVAL)
    {
      if (checkPass())
      {
        alarmState = NOT_READY;
      }
    }
    else
    {
      alarmState = ARMED;
    }
  }
  else if (alarmState == ALARM)
  {
    while (!checkPass())
    {
      makeNoise();
    }
    alarmState = READY_TO_ARM;
  }
  else if (alarmState == NOT_READY)
  {
    if (!(doorSensorTripped || pirSensorTripped || lightSensorTripped))
    {
      alarmState = READY_TO_ARM;
    }
  }
  if (lastState != alarmState) // do your state change things here
  {
    lcd.setCursor(0,0);
    lcd.print(alarmText[alarmState]);
    lcd.setCursor(0,1);
    switch (alarmState)
    {
      case READY_TO_ARM:
        lcd.print(F("***ALL  ZONES***"));
        Serial.println(F("Alarm Disarmed, Ready to arm"));
        break;
      case ARMED:
        lcd.print(F("                "));
        Serial.println(F("Alarm Armed"));
        break;
      case PASSWORD_WAIT:
        lcd.print(F("*ENTER PASSWORD*"));
        Serial.println(F("Hurry and Enter Password"));
        break;
      case EXIT_NOW:
        lcd.print(F("****Exit NOW****"));
        Serial.println(F("Alarm Armed, Exit Now"));
        break;
      case ALARM:
        lcd.print(F("Password2cancel"));
        Serial.println(F("Alarm!!"));
        break;
      case NOT_READY:
        char faultMessage[17] = "";
        sprintf(faultMessage, "Zone: %d %d %d     ", doorSensorTripped? "D" : " ", lightSensorTripped? "L" : " ", pirSensorTripped? "P" : " ");  //Door, Light or Pir
        lcd.print(faultMessage);
        Serial.println(F("Not ready to Arm"));
        break;
    }
    lastState = alarmState;
  }
}

bool checkPass()
{
  static char userPassword[5] = {NULL};
  static byte index = 0;
  if (Serial.available())
  {
    userPassword[index] = Serial.read();
    if (!strcmp(myPassword, userPassword))
    {
      Serial.println("matched");
      for (byte i = 0; i < 4; i++)
      {
        userPassword[i] = NULL;
      }
      index = 0;
      return true;
    }
    index++;
    if (index > 3)
    {
      Serial.println("try again");
      for (byte i = 0; i < 4; i++)
      {
        userPassword[i] = NULL;
      }
      index = 0;
    }
  }
  return false;
}

void makeNoise()
{
  static unsigned long lastMillis = 0;
  if(millis() - lastMillis > 1000UL)
  {
    Serial.print("!");
    lastMillis = millis();
  }
}

You will have to modify the LCD bits... My library wouldn't compile without a few changes.

not completely tested!

Thx for the heads up gpop1, I'm now learning how to implement millis to my code :).
Thank you for the code BulldogLowell, will have a look after I have a better understanding on millis :).

DarkxRift:
Thank you for the code BulldogLowell, will have a look after I have a better understanding on millis :).

Give it a shake and see if you like the way it behaves.

The State Machine approach lets you manage each state's behavior the way you wish. Non- blocking code is key!

Is it possible to be albe to arm the alarm whenever is not armed? BulldogLowell :slight_smile:

DarkxRift:
Is it possible to be albe to arm the alarm whenever is not armed? BulldogLowell :slight_smile:

Regardless if sensors are tripped, you mean?

What I meant to do is before I armed the alarm all sensor are not activate, after the correct code has been entered, five seconds for the person to leave the area, all sensors start to do its work, if front door(which is reed switch) is triggered, ten second countdown for the person to enter password, I'm just struggling over that even I enter the correct code, the countdown still goes on.

DarkxRift:
What I meant to do is before I armed the alarm all sensor are not activate, after the correct code has been entered, five seconds for the person to leave the area, all sensors start to do its work, if front door(which is reed switch) is triggered, ten second countdown for the person to enter password, I'm just struggling over that even I enter the correct code, the countdown still goes on.

this post is going to go on for a while so its a good idea after any changes to re-post the current code you are using. I would like to help but im not sure what your current code looks like.

My current code looks like this:

/*
Transplant Wiring
Arduino GND   -> Breadboard
Arduino +5V   -> Breadboard
Arduino 8     -> Speaker
Arduino 7     -> Reed_Switch
Arduino 4     -> PIR_Sensor
Arduino A0    -> Photo_Resistor
Arduino A4    -> PCF8574 - LCD/Keypad
Arduino A5    -> PCF8574 - LCD/Keypad
*/

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <i2ckeypad.h>

#define Speaker 8
#define Switch 7
#define PIR 4
#define P_R A0
#define PCF8574_ADDR 0x20

#define ROWS 4
#define COLS 4


char* ourCode = "1234";
int currentPosition = 0;
//keypad variable

int reedPin = 0;
//Reed Switch variable

int pirPin = 0;
//PIR Sensor variable

int phPin = 0;
//Photo Resistor variable

int alarmActive = 0;
int alarmStatus = 0;
int zone;

int interval_reed = 9000;

LiquidCrystal_I2C lcd (0x38, 16, 2); // set the LCD address to 0x20 for a 16 chars and 2 line display
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);

void setup()
{
  pinMode(Speaker, OUTPUT);
  //Speaker variable

  pinMode(PIR, INPUT);
  //PIR Sensor variable

  pinMode(Switch, INPUT);
  digitalWrite(Switch, HIGH);
  //Reed Switch variable

  pinMode(P_R, INPUT);
  //Photo Resistor variable

  Wire.begin();
  Serial.begin(9600);

  kpd.init();
  //keypad variable

  lcd.init();
  lcd. backlight();
  displayEntryCodeScreen();

}

void loop()
{

  checkPass();



  if (alarmActive == 1)
  {
    pirPin = digitalRead(PIR);
    phPin = analogRead(P_R);
    reedPin = digitalRead(Switch);

    if (reedPin == HIGH && alarmStatus == 0)
    {

      zone = 0;
      alarmTriggered();
      alarmStatus = 1;

    }

    else if (pirPin == HIGH && alarmStatus == 0)
    {

      zone = 1;
      alarmStatus = 1;
      alarmTriggered();
    }

    else if (phPin < 700 && alarmStatus == 0)
    {

      zone = 2;
      alarmStatus = 1;
      alarmTriggered();
    }
  }

}


void checkPass()
{
  char key = kpd.get_key();
  if (int(key) != 0)
  {
    lcd.setCursor(14, 3);
    lcd.print("     ");
    lcd.setCursor(14, 3);

    if (key == ourCode[currentPosition])
    {
      ++currentPosition;
      if (currentPosition == 4 && alarmActive == 0 )
      {
        armAlarm();
        currentPosition = 0;
      }
      else if (currentPosition == 4  && alarmActive == 1 )
      {
        disarmAlarm();
        currentPosition = 0;
      }
    }
    else if (key != ourCode[currentPosition] && alarmActive == 0)
    {
      invalidCode_1();
      currentPosition = 0;
    }
    else if (key != ourCode[currentPosition] && alarmActive == 1)
    {
      invalidCode_2();
      currentPosition = 0;
    }
  }
}

void armAlarm()// alarm armed and countdown start
{
  clearScreen();
  lcd.setCursor(0, 0);
  lcd.print("   Alarm ARMED    ");
  noTone(Speaker);
  delay(1000);
  countDown_1();

}

void countDown_1()// alarm countdown
{
  lcd.setCursor(0, 0);
  lcd.print("Countdown Start  ");
  int count = 5;
  while (count != 0 )
  {
    count--;
    lcd.setCursor(0, 1);
    lcd.print("------ ");
    lcd.print(count);
    lcd.print(" -------");
    delay(1000);
  }
  if (count == 0)
  {
    clearScreen();
    lcd.setCursor(0, 0);
    lcd.print("<<System READY>>");
    alarmActive = 1;
  } // when countdown reach zero


}

void countDown_2()
{
  clearScreen();

  lcd.setCursor(0, 0);
  lcd.print("-Enter Password-");
  int count = 10;

  unsigned long passTimeout = millis();

  while((millis() - passTimeout) < interval_reed)
  {
    chcekPass();
  }



  if ((millis() - passTimeout) < interval_reed)
  {
    lcd.setCursor(0, 0);
    lcd.print("Countdown Ended ");
    lcd.setCursor(0, 1);
    lcd.println("Alarm Triggered!");
    tone(Speaker, 1200);
  }
}



void disarmAlarm()// code for disarming alarm
{
  lcd.setCursor(0, 0);
  lcd.print(" Alarm DISARMED ");
  lcd.setCursor(0, 1);
  lcd.println(" Access GRANTED  ");
  noTone(Speaker);
  alarmStatus = 0;
  alarmActive = 0;

  delay(1000);
  displayEntryCodeScreen();
}

void invalidCode_1()// when alarm is not armed even if password input is incorrect no tone will be sound
{
  lcd.setCursor(0, 0);
  lcd.print("  Access DENIED ");
  lcd.setCursor(0, 1);
  lcd.println("  Invalid code  ");

}

void invalidCode_2()// when alarm is not armed even if password input is incorrect no tone will be sound
{

  lcd.setCursor(0, 0);
  lcd.print("  Access DENIED ");
  lcd.setCursor(0, 1);
  lcd.println("  Invalid code  ");
  tone (Speaker, 1200);
}

void clearScreen()// code for clearing the screen
{
  lcd.setCursor(0, 0);
  lcd.print("                          ");
  lcd.setCursor(0, 1);
  lcd.print("                          ");
}

void displayEntryCodeScreen()// code for displaying the entry code screen
{
  clearScreen();
  lcd.setCursor(0, 0);
  lcd.print("-Enter Password-");
}



void alarmTriggered() // when alarm is triggered, which zone will be printed on the LCD and tone will play
{

  clearScreen();
  lcd.setCursor(0, 0);
  lcd.print("Alarm Triggered!");

  if (zone == 0)// if zone one is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Countdown Start ");

  }



  if (zone == 1)// if zone one is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Motion in Zone 1 ");
    tone(Speaker, 1200);
  }
  if (zone == 2)// if zone two is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Motion in Zone 2 ");
    tone(Speaker, 1200);
  }
}

I have change this part:

void countDown_2()
{
  clearScreen();

  lcd.setCursor(0, 0);
  lcd.print("-Enter Password-");
  int count = 10;

  unsigned long passTimeout = millis();

  while((millis() - passTimeout) < interval_reed)
  {
    chcekPass();
  }



  if ((millis() - passTimeout) < interval_reed)
  {
    lcd.setCursor(0, 0);
    lcd.print("Countdown Ended ");
    lcd.setCursor(0, 1);
    lcd.println("Alarm Triggered!");
    tone(Speaker, 1200);
  }
}

everything else works fine, the after I've disarm the alarm I can still arm the alarm, sensor still work, but the reed switch countdown, I've set it to 10 seconds chance to check password but I've no idea how to make the alarm go off cause even if I wait for 10 seconds the alarm didn't go off, another thing is, is there anyway to show that the loop is counting down like the arming alarm countdown loop?

void countDown_1()// alarm countdown
{
  lcd.setCursor(0, 0);
  lcd.print("Countdown Start  ");
  int count = 5;
  while (count != 0 )
  {
    count--;
    lcd.setCursor(0, 1);
    lcd.print("------ ");
    lcd.print(count);
    lcd.print(" -------");
    delay(1000);
  }
  if (count == 0)
  {
    clearScreen();
    lcd.setCursor(0, 0);
    lcd.print("<<System READY>>");
    alarmActive = 1;
  } // when countdown reach zero


}

chcekPass();

that doesn't look right

maybe you mean checkPass();
which calls a function

seriously got to get you to avoid using "while"

the code especially involving the lcd is all over the place. functions when used correctly have useful purposes. When used incorrectly its just a program that jumps all over the place and makes mistakes easy to make and hard to spot

This program should have no more than 4 functions

1/ state machine for the alarm
2/ keypad code
3/ lcd code
4/ audio alarm

The idea of a function is to allow for easy trouble shooting

thing like

void clearScreen()// code for clearing the screen
{
  lcd.setCursor(0, 0);
  lcd.print("                          ");
  lcd.setCursor(0, 1);
  lcd.print("                          ");
}

make no sense as the lcd library already has a code to do that

lcd.clear();

jumping from function to function all the time writing info to the screen is going to cause problems.

If you write to line one then switch function and rewrite to the same line its going to cause problems.

Ive tried to address some of the issues but its hard to follow. (it may be easier seeing this on the display then fixing the problems). A better way would be to use flags then use a function called display and keep the different screen separated using a "switch/ case".

I don't think this will work but ive tried to clean up the code and fix some of the mistakes. The fact that I can not see all of the problems indicates that theres a problem with the plan.

/*
Transplant Wiring
Arduino GND   -> Breadboard
Arduino +5V   -> Breadboard
Arduino 8     -> Speaker
Arduino 7     -> Reed_Switch
Arduino 4     -> PIR_Sensor
Arduino A0    -> Photo_Resistor
Arduino A4    -> PCF8574 - LCD/Keypad
Arduino A5    -> PCF8574 - LCD/Keypad
*/

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <i2ckeypad.h>

#define Speaker 8
#define Switch 7
#define PIR 4
#define P_R A0
#define PCF8574_ADDR 0x20

#define ROWS 4
#define COLS 4


char* ourCode = "1234";
int currentPosition = 0;
//keypad variable

int reedPin = 0;
//Reed Switch variable

int pirPin = 0;
//PIR Sensor variable

int phPin = 0;
//Photo Resistor variable

int alarmActive = 0;
int alarmStatus = 0;
int zone;

int interval_reed = 9000;
byte count = 10;
byte count2 = 5;
LiquidCrystal_I2C lcd (0x38, 16, 2); // set the LCD address to 0x20 for a 16 chars and 2 line display
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);

void setup()
{
  pinMode(Speaker, OUTPUT);
  //Speaker variable

  pinMode(PIR, INPUT);
  //PIR Sensor variable

  pinMode(Switch, INPUT);
  digitalWrite(Switch, HIGH);
  //Reed Switch variable

  pinMode(P_R, INPUT);
  //Photo Resistor variable

  Wire.begin();
  Serial.begin(9600);

  kpd.init();
  //keypad variable

  lcd.init();
  lcd. backlight();
  displayEntryCodeScreen();

}

void loop()
{

  checkPass();



  if (alarmActive == 1)
  {
    pirPin = digitalRead(PIR);
    phPin = analogRead(P_R);
    reedPin = digitalRead(Switch);

    if (reedPin == HIGH && alarmStatus == 0)
    {

      zone = 0;
      alarmTriggered();
      alarmStatus = 1;

    }

    else if (pirPin == HIGH && alarmStatus == 0)
    {

      zone = 1;
      alarmStatus = 1;
      alarmTriggered();
    }

    else if (phPin < 700 && alarmStatus == 0)
    {

      zone = 2;
      alarmStatus = 1;
      alarmTriggered();
    }
  }

}


void checkPass()
{
  char key = kpd.get_key();
  if (int(key) != 0)
  {
    if (key == ourCode[currentPosition])
    {
      ++currentPosition;
      if (currentPosition == 4 && alarmActive == 0 )
      {
        armAlarm();
        count2=5;
        currentPosition = 0;
      }
      else if (currentPosition == 4  && alarmActive == 1 )
      {
        disarmAlarm();
        counter = 10;
        currentPosition = 0;
      }
    }
    else if (key != ourCode[currentPosition] && alarmActive == 0)
    {
      invalidCode_1();
      currentPosition = 0;
    }
    else if (key != ourCode[currentPosition] && alarmActive == 1)
    {
      invalidCode_2();
      currentPosition = 0;
    }
  }
  lcd.setCursor(0, 4);
  currentpoistion == 1 ? lcd.print("*") : lcd.print("---");
  currentpoistion == 2 ? lcd.print("**") : lcd.print("--");
  currentpoistion == 3 ? lcd.print("***") : lcd.print("-");
  currentpoistion == 4 ? lcd.print("****") : void;



}

void armAlarm()// alarm armed and countdown start
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   Alarm ARMED    ");
  noTone(Speaker);
  delay(1000);
  countDown_1();

}

void countDown_1()// alarm countdown
{
  unsigned long currentTime = millis();
  static unsigned long lastTime;

  lcd.setCursor(0, 0);
  lcd.print("Countdown Start  ");

  if (currentTime2 - lastTime2 > 1000)
  {
    count2--;
    lastTime2 = currentTime2;
  }


  if (count2 != 0 )
  {
    lcd.setCursor(0, 6);
    lcd.print("timer: ");
    lcd.print (counter);
  }

  if (count2 == 0)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("<<System READY>>");
    alarmActive = 1;
  } // when countdown reach zero


}

void countDown_2()
{
  static int count = 10;
  unsigned long currentTime = millis();
  static unsigned long lastTime;

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("-Enter Password-");


  if (currentTime - lastTime > 1000)
  {
    count--;
    lastTime = currentTime;
  }

  if (count != 0)
  {
    checkPass();
  }

  if (count == 0)
  {
    lcd.setCursor(0, 0);
    lcd.print("Countdown Ended ");
    lcd.setCursor(0, 1);
    lcd.println("Alarm Triggered!");
    tone(Speaker, 1200);
  }
  lcd.setCursor(0, 6);
  lcd.print("timer: ");
  lcd.print (counter);

}



void disarmAlarm()// code for disarming alarm
{
  lcd.setCursor(0, 0);
  lcd.print(" Alarm DISARMED ");
  lcd.setCursor(0, 1);
  lcd.println(" Access GRANTED  ");
  noTone(Speaker);
  alarmStatus = 0;
  alarmActive = 0;

  delay(1000);
  displayEntryCodeScreen();
}

void invalidCode_1()// when alarm is not armed even if password input is incorrect no tone will be sound
{
  lcd.setCursor(0, 0);
  lcd.print("  Access DENIED ");
  lcd.setCursor(0, 1);
  lcd.println("  Invalid code  ");

}

void invalidCode_2()// when alarm is not armed even if password input is incorrect no tone will be sound
{

  lcd.setCursor(0, 0);
  lcd.print("  Access DENIED ");
  lcd.setCursor(0, 1);
  lcd.println("  Invalid code  ");
  tone (Speaker, 1200);
}

void displayEntryCodeScreen()// code for displaying the entry code screen
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("-Enter Password-");
}



void alarmTriggered() // when alarm is triggered, which zone will be printed on the LCD and tone will play
{

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Alarm Triggered!");

  if (zone == 0)// if zone one is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Countdown Start ");

  }

  if (zone == 1)// if zone one is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Motion in Zone 1 ");
    tone(Speaker, 1200);
  }
  if (zone == 2)// if zone two is triggered
  {
    lcd.setCursor(0, 1);
    lcd.print("Motion in Zone 2 ");
    tone(Speaker, 1200);
  }
}