Trouble getting my PIR sensor to detect door opening in my security project

Hi everyone,

I’m working on my own little security project and have run into a little snag. I am fairly new to C++ but have been working through tutorials and talking with a friend of mine who knows a lot about C++ and I feel like I’ve learned a lot. Basically, I want to have an alarm system that sits idle until a door is opened. Then when the door is opened, the PIR sensor detects this and gives me a 20 second time limit to scan my RFID tag against the RFID sensor. Unfortunately, I can’t get it to detect the door is open/PIR signal and start the countdown. It just sits idle after scanning my master RFID tag. I believe I may be going wrong on around lines 70 or 122.

I have tried a lot of different shuffling of code and moving bits around to run in different areas and times but this is where I am stuck now and I’m out of ideas. I’ll paste the code below, I know it’s a lot but I just want to give the full picture.

#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>

#define RST_PIN   9
#define SS_PIN    10

// TIME VARIABLES
int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 20; //start seconds

byte readCard[4];
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
boolean successRead = false;
boolean correctTag = false;
int proximitySensor;
boolean doorOpened = false;

// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Parameters: (rs, enable, d4, d5, d6, d7)

uint8_t getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
    return 0;
  }
  tagID = "";
  for ( uint8_t i = 0; i < 4; i++) {  // The MIFARE PICCs that we use have 4 byte UID
    readCard[i] = mfrc522.uid.uidByte[i];
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

void setup() {
  // Initiating
  SPI.begin();        // SPI bus
  mfrc522.PCD_Init(); //  MFRC522
  lcd.begin(16, 2);   // LCD screen
  pinMode(8, OUTPUT);                       // Set digital pin 13 -> output

  // Prints the initial message
  lcd.print("-No Master Tag!-");
  lcd.setCursor(3, 1);
  lcd.print("SCAN NOW");
  // Waits until a master card is scanned
  while (!successRead) {
    successRead = getID();
    if ( successRead == true) {
      myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Master Tag Set!");
      tagsCount++;
    }
  }
  successRead = false;
  printNormalModeMessage();
}

void loop() {
  int proximitySensor = analogRead(A0);
  // If door is closed...
  if (proximitySensor > 200) {
      printNormalModeMessage();
      }
    if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
      return;
    }
    tagID = "";
    // The MIFARE PICCs that we use have 4 byte UID
    for ( uint8_t i = 0; i < 4; i++) {  //
      readCard[i] = mfrc522.uid.uidByte[i];
      tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
    }
    tagID.toUpperCase();
    mfrc522.PICC_HaltA(); // Stop reading

    correctTag = false;
    // Checks whether the scanned tag is the master tag
    if (tagID == myTags[0]) {
      lcd.clear();
      lcd.print("Program mode:");
      lcd.setCursor(0, 1);
      lcd.print("Add/Remove Tag");
      while (!successRead) {
        successRead = getID();
        if ( successRead == true) {
          for (int i = 0; i < 100; i++) {
            if (tagID == myTags[i]) {
              myTags[i] = "";
              lcd.clear();
              lcd.setCursor(0, 0);
              lcd.print("  Tag Removed!");
              printNormalModeMessage();
              return;
            }
          }
          myTags[tagsCount] = strdup(tagID.c_str());
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("   Tag Added!");
          printNormalModeMessage();
          tagsCount++;
          return;
        }
      }
    }
  // If door is open...
  else {
    while (!doorOpened) {
      proximitySensor = analogRead(A0);
      if (proximitySensor > 200) {
        doorOpened = true;
        timer();
      }
    }
    doorOpened = false;
    delay(500);
    printNormalModeMessage();
  }
}

void printNormalModeMessage() {
  delay(1500);
  lcd.clear();
  lcd.print(" -System active-");
}

void printDeactivatedMessage() {
  delay(1500);
  lcd.clear();
  lcd.print("-System inactive-");
}

void timer() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Present ID");
    while (hours > 0 || minutes > 0 || seconds >= 0) {
    lcd.setCursor(4, 2);
    (hours < 10) ? lcd.print("0") : NULL;
    lcd.print(hours);
    lcd.print(":");
    (minutes < 10) ? lcd.print("0") : NULL;
    lcd.print(minutes);
    lcd.print(":");
    (seconds < 10) ? lcd.print("0") : NULL;
    lcd.print(seconds);
    lcd.display();
    countdown();
    delay(1000);
    }
    successRead = false;
    // Checks whether the scanned tag is authorized
    for (int i = 0; i < 100; i++) {
      if (tagID == myTags[i]) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(" Access Granted!");
        printDeactivatedMessage();
        correctTag = true;
      }
    }
    if (correctTag == false) {
      lcd.setCursor(0, 0);
      lcd.print(" Access Denied!");
  }
}

void trigger() {
 lcd.clear();
 lcd.setCursor(2,0);
 lcd.print("Authorities");
 lcd.setCursor(4,1);
 lcd.print("alerted");
 digitalWrite(8, HIGH);                    // Pin 13 = 5 V, LED emits light
 delay(1000);
}

// countdown timer of 30 seconds
void countdown() {
 if (seconds > 0) {
 seconds -= 1;
 } else {
 if (minutes > 0) {
 seconds = 59;
 minutes -= 1;
 } else {
 if (hours > 0) {
 seconds = 59;
 minutes = 59;
 hours -= 1;
 } else {
 trigger();
 }
 }
 }
}

Thanks in advance

Looks like you're defining the int proximitysensor twice not sure what that would do. But I'd start by testing the input from the PIR sensor separately from all the other code. Your printNormalModeMessage() is delaying the program every time it hits that >200 state, cant be helping with scan time. You might want to try putting a Serial.println("I'm here"); somewhere in that while(!doorOpened) loop or after it to see if you're getting stuck somewhere.

Another thought... I'm pretty sure PIR sense heat. So if the PIR is only looking at the back of a door and the door isn't changing temperature it may never trigger.

Thanks for the help! I've made some changes and will test it when I get back from work tonight. My apologies also, I am using a Proximity Sensor CNY70, not a PIR sensor. I'll report back with the results.