Alarm with multiple pir sensors

/************************************************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 **************************************************************************************************
  This example runs directly on NodeMCU.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right NodeMCU module
  in the Tools -> Board menu!

  For advanced settings please follow ESP examples :
   - ESP8266_Standalone_Manual_IP.ino
   - ESP8266_Standalone_SmartConfig.ino
   - ESP8266_Standalone_SSL.ino

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!

  Comment this out to disable prints and save space/
  Rui Santos/
  Complete project details at http://randomnerdtutorials.com/ 
/**************************************************************************************************/


#include <Adafruit_Sensor.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


/********************** Frontdoor *****************************************************************/
const int switchReed = 5;      // Frontdoor D1 = gpio 5
int doorreedCounter = 0;       // counter for the number of door openings
int doorreedState = 0;         // current state of the Door
int lastdoorreedState = 0;     // previous state of the Door


/********************** Garagedoor ****************************************************************/
const int switch1Reed = 12;     // Garagedoor D6 = gpio 12
int doorreed1Counter = 0;      // counter for the number of door openings
int doorreed1State = 0;        // current state of the Door
int lastdoorreed1State = 0;    // previous state of the Door


/********************** Blynk Athentication Token *************************************************/
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "iKklxMOUfksdjfhsdgfd_Mk2hXqxH7bi";


/********************** WiFi credentials **********************************************************/
// Set password to "" for open networks.
char ssid[] = "Zi*********";
char pass[] = "J*******1";


/********************** Relay controlled with D7 **************************************************/
int relay = 13; // relay connected to pin D7 gpio 13
volatile byte relayState = LOW; //relay state


/********************** Pir HC-SR501 **************************************************************/
int PIRInterrupt = 4; // PIR Motion Sensor is connected to D2 gpio 4


/********************** Relay controlled with D7 **************************************************/
int relay1 = 14; // relay connected to pin D7 gpio 13
volatile byte relayState1 = LOW; //relay state


/********************** Pir HC-SR501 **************************************************************/
int PIRInterrupt1 = 0; // PIR Motion Sensor is connected to D3 gpio 0


/********************** Timer for Relay Stay on ***************************************************/
long lastDebounceTime = 0;  
long debounceDelay = 20000; //20 seconds


/********************** Blynk Timer event *********************************************************/
BlynkTimer timer;              // Announcing the timer


/********************** InitVariables *************************************************************/
int Alarm_arm_disarm;          //arm or disarm the alarm
int Restart;                   // restart the wemos32


/********************** Sync status buttons to App ************************************************/
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V10);
}


/********************** Activate Alarm True App ***************************************************/
BLYNK_WRITE(V10) {
  Alarm_arm_disarm = param.asInt();
}


/********************** Reset esp8266 True App ****************************************************/
BLYNK_WRITE(V11) {
  Restart = param.asInt();
}


/********************** Frontdoor switchReed V0-D1-gpio5 ******************************************/
void myTimerEvent()
{
  if (Alarm_arm_disarm == 1) {
    doorreedState = digitalRead(switchReed);

    // compare the buttonState to its previous state
    if (doorreedState != lastdoorreedState) {
      // if the state has changed, increment the counter
      if (doorreedState == LOW) {
        // if the current state is LOW then the button
        // is pressed wend from off to on:
        doorreedCounter++;
        Blynk.virtualWrite(V0, HIGH);
        Serial.print("FrontDoor Close");
        Serial.println(doorreedState);
        Serial.println(doorreedCounter);
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("FrontDoor Open");
        Blynk.virtualWrite(V0, LOW);
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastdoorreedState = doorreedState;
  }
}


/********************** Garagedoor switch2Reed V2-D8-gpio15 ***************************************/
void myTimerEvent1()
{
  if (Alarm_arm_disarm == 1) {
    doorreed1State = digitalRead(switch1Reed);

    // compare the buttonState to its previous state
    if (doorreed1State != lastdoorreed1State) {
      // if the state has changed, increment the counter
      if (doorreed1State == LOW) {
        // if the current state is LOW then the button
        // is pressed wend from off to on:
        doorreed1Counter++;
        Blynk.virtualWrite(V2, HIGH);
        Serial.print("GarageDoor Close");
        Serial.println(doorreed1State);
        Serial.println(doorreed1Counter);
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("GarageDoor Open");
        Blynk.virtualWrite(V2, LOW);
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastdoorreed1State = doorreed1State;
  }
}

/********************** Restart nodemmcu with button **********************************************/


void myTimerEvent2()
{
  if (Restart == 1) {
    ESP.restart();
  }
}


void setup() 
{
  // Pin for relay module set as output
  pinMode(relay, OUTPUT);
  pinMode(relay1, OUTPUT);
  digitalWrite(relay, HIGH);
  digitalWrite(relay1, HIGH);
  // PIR motion sensor set as an input
  pinMode(PIRInterrupt, INPUT);
  // Triggers detectMotion function on rising mode to turn the relay on, if the condition is met
  attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);
  pinMode(PIRInterrupt1, INPUT);
  // Triggers detectMotion function on rising mode to turn the relay on, if the condition is met
  attachInterrupt(digitalPinToInterrupt(PIRInterrupt1), detectMotion1, RISING);
  // Debug console
  // Debug console
  Serial.begin(9600);

  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 178, 222), 8080);
  pinMode(switchReed, INPUT_PULLUP);
  pinMode(switch1Reed, INPUT_PULLUP);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, myTimerEvent1);
  timer.setInterval(1000L, myTimerEvent2);
 }


void loop() 
{
  {
    Blynk.run();
    timer.run(); // running timer every second
  }
  // If 20 seconds have passed, the relay is turned off
  if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){
    digitalWrite(relay, HIGH);
    relayState = LOW;
    Serial.println("Motion detection is OFF");
    Serial.println("Relay is LOW");
  }
  delay(50);
}


void detectMotion() {
  Serial.println("Motion");
  if(relayState == LOW){
    digitalWrite(relay, LOW);
  }
  relayState = HIGH;  
  Serial.println("Motion detection is ON");
  Serial.println("Relay is HIGH");
  lastDebounceTime = millis();
}


void detectMotion1() {
  Serial.println("Motion 1");
  if(relayState1 == LOW){
    digitalWrite(relay1, LOW);
  }
  relayState1 = HIGH;  
  Serial.println("Motion detection 1 is ON");
  Serial.println("Relay 1 is HIGH");
  lastDebounceTime = millis();
}