Need help with logic of current coding for RFID activated security system

Note: The coding that is going to be presented is a modified version of http://www.instructables.com/id/Arduino-RC522-RFID-Door-Unlock/.

The problem with my code specifically is that i dont know if there is anyway to activate the alarm system indefinitely and disarm the system indefinitely without affecting the if else loops used for RFID communications. This is the part of the code(Look specifically where there is a while loop for mode and submode):

///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  
  do {
    successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      cycleLeds();              // Program Mode cycles through RGB waiting to read a new card
    }
    else {
      normalModeOn();     // Normal mode, blue Power LED is on, all others are off
    }
  }
  while (!successRead);   //the program will not go further while you not get a successful read
  if (programMode) {
    if ( isMaster(readCard) ) { //If master card scanned again exit program mode
      Serial.println(F("Master Card Scanned"));
       lcd.home ();   
       lcd.print("--------------------");  
       lcd.setCursor ( 0, 1 );// go to the first line, first character
       lcd.print("Master Card Scanned  ");  
       lcd.setCursor ( 0, 2);        // go to the 2nd line
       lcd.print("Exiting Program Mode");
       lcd.setCursor ( 0, 3);
       lcd.print("--------------------");
      Serial.println(F("Exiting Program Mode"));
      Serial.println(F("-----------------------------"));
      programMode = false;
      return;
    }
    else {
      if ( findID(readCard) ) { // If scanned card is known delete it
        Serial.println(F("I know this PICC, removing..."));
        deleteID(readCard);
        Serial.println("-----------------------------");
      }
      else {                    // If scanned card is not known add it
        Serial.println(F("I do not know this PICC, adding..."));
        writeID(readCard);
        Serial.println(F("-----------------------------"));
      }
    }
  }
  else {
    if ( isMaster(readCard) ) {   // If scanned card's ID matches Master Card's ID enter program mode
      programMode = true;
      Serial.println(F("Hello Master - Entered Program Mode"));
      int count = EEPROM.read(0);   // Read the first Byte of EEPROM that
      Serial.print(F("I have "));     // stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(F(" record(s) on EEPROM"));
      Serial.println("");
      Serial.println(F("Scan a PICC to ADD or REMOVE"));
      Serial.println(F("-----------------------------"));
    }
    else {
      Mode=0;
     while(Mode==0)
     {
      if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
        Serial.println(F("Welcome, You shall pass"));
        Submode=0;
        INITIALISE(300);  // Open the door lock for 300 ms
        Mode++;
        
      }
      else {      // If not, show that the ID was not valid
        Serial.println(F("You shall not pass"));
        denied();
      }
     }
     while(Mode==1)
     {
      if ( findID(readCard) ) {  // If not, see if the card is in the EEPROM
        Serial.println(F("Welcome, You shall pass"));
        Submode=1;
        Disarm(300);         // Open the door lock for 300 ms
      }
      else {      // If not, show that the ID was not valid
        Serial.println(F("You shall not pass"));
        denied();
      }
    }
    }
  }
}
/////////////////////////////////////////  Access Granted    ///////////////////////////////////
void INITIALISE (int setDelay) {
  digitalWrite(blueLed, LED_OFF);   // Turn off blue LED
  digitalWrite(redLed, LED_OFF);  // Turn off red LED
  digitalWrite(greenLed, LED_ON);   // Turn on green LED
  delay(1000); 
  lcd.home();   
  lcd.print("Home Security  ");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("System V1.0");
  lcd.setCursor ( 0, 2 );
  lcd.print("Alarm Initilized");// Hold green LED on for a second
  while(Submode==0)
  { 
     
    if(Sensor_Front==HIGH)
    {
      lcd.clear();
      for(int d=0;d<15301;d+1)
      {
      lcd.home ();                   // go to the first line, first character
      lcd.print("Home Security  ");  
      lcd.setCursor ( 0, 1 );        // go to the 2nd line
      lcd.print("System V1.0");
      lcd.setCursor ( 0, 2 );
      lcd.print("Front Comprimised");
      digitalWrite(alarm, HIGH);
      digitalWrite(SSR, HIGH);
      delay(20);
      lcd.clear();
      }
    }
    if(Sensor_Back==HIGH)
    {
      lcd.clear();
      for(int fade1=0;fade1<15301;fade1+1)
      {
      lcd.home ();                   // go to the first line, first character
      lcd.print("Home Security  ");  
      lcd.setCursor ( 0, 1 );        // go to the 2nd line
      lcd.print("System V1.0");
      lcd.setCursor ( 0, 2 );
      lcd.print("Back Comprimised");
      digitalWrite(alarm, HIGH);
      digitalWrite(SSR, HIGH);
      delay(20);
      lcd.clear();
      }
    }
    if(Sensor_Left==HIGH)
    {
      lcd.clear();
      for(int a=0;a<15301;a+1)
      {
      lcd.home ();                   // go to the first line, first character
      lcd.print("Home Security  ");  
      lcd.setCursor ( 0, 1 );        // go to the 2nd line
      lcd.print("System V1.0");
      lcd.setCursor ( 0, 2 );
      lcd.print("Left Comprimised");
      digitalWrite(alarm, HIGH);
      digitalWrite(SSR, HIGH);
      delay(20);
      lcd.clear();
      }
    }
    if(Sensor_Right==HIGH)
    {
      lcd.clear();
      for(int b=0;b<15301;b+1)
      {
      lcd.home ();                   // go to the first line, first character
      lcd.print("Home Security  ");  
      lcd.setCursor ( 0, 1 );        // go to the 2nd line
      lcd.print("System V1.0");
      lcd.setCursor ( 0, 2 );
      lcd.print("Right Comprimised");
      digitalWrite(alarm, HIGH);
      digitalWrite(SSR, HIGH);
      delay(20);
      }
    }
  }
while(Submode==1)
{
  lcd.home (); 
  lcd.print("Home Security  ");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("System V1.0");
  lcd.setCursor ( 0, 2 );
  lcd.print("Alarm Disarmed");
  delay(5000);
  lcd.clear();
}
  
  
}
/////////////////////////////////////////  Access Granted(Disarm)    ///////////////////////////////////
void Disarm (int setDelay) {
  lcd.home (); 
  lcd.print("Home Security  ");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("System V1.0");
  digitalWrite(blueLed, LED_OFF);   // Turn off blue LED
  digitalWrite(redLed, LED_OFF);  // Turn off red LED
  digitalWrite(greenLed, LED_ON);   // Turn on green LED
  delay(1000);            // Hold green LED on for a second
  while(Mode==1)
  { 
    Submode=0;
    Mode=0;
    
  }
  
  
}

Thank you for any help given in advance!!

A do/while construct is rarely the correct construct to use.

Posting snippets is rarely the correct thing to do.

      for(int b=0;b<15301;b+1)
      {
      lcd.home ();                   // go to the first line, first character
      lcd.print("Home Security  ");

Why do you want to do this 15 thousand times? You are not assigning a new value to b in the for statement, for the next pass through the loop.

I rarely use while(). When I see code full of while() statements, I just suspect a total re-write is in order.

i used a for loop for 15000 plus times because i want the alarm to sound for about 5 minutes before going out of the loop. Well for the while loop issue, i wanna make the Security system be turned on or off for an indefinite amount of time and the while loop is the only way i know how to do it because i am still pretty green to coding..

If you have the time you may wanna take a look at the newly revised code for the arduino.(Ps. it is about 1150 lines long due to repetitions(code to check if card being red is master card or user card) in the while loop . Currently the problems are:

  1. Bug where the screen displays 2 of the lcd.prints together.(Only happens one time when i arm-disarm security system and thereafter use the master card. After the first time the error fixes itself).

  2. Code used to sound alarm and display area of breach when sensors are triggered doesnt seem to work.(Even tried to use 5V DC to check if the code for the sensors work)

  3. My Red led seems to stay dimly lit. Checked my code and dont know why it stays lit when i didnt set it to high.

My Code Link in PasteBin due to the length of the code: RFID Alarm System - Pastebin.com

My Code Link in PasteBin due to the length of the code: RFID Alarm System - Pastebin.com

Your code is quite long, so you tossed it in the rubbish bin. Probably not a bad idea.

But, if you want us to look at it, use Reply (NOT the Quick Reply field), and use the Additional Options link below the text entry field, and attach your code here.

Here it is and thanks once again.

Test2.ino (56 KB)

I really do not like the structure of your code. In loop(), nothing happens until a tag is scanned. Alarms can't be triggered/silenced. That is not a good way to build an alarm system.

You need to do a bit of reading on event driven programming. Scanning a card is an event that needs to be handled when it happens.

You have far too much code in loop(). loop() should look something like:

void loop()
{
   if(tagScanned())
   {
      if(tagIsMaster())
      {
         // Do something when master tag is scanned
      }
      else
      {
         // Do something when another tag is scanned
      }
   }

   // Do whatever else needs doing
}

The tagScanned() method should NOT be a blocking function. It needs to return true if a tag is scanned, and false if not.

After the last comment is where code to shut the alarm off, under the right circumstances, would go.

     do {
    successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      cycleLeds();              // Program Mode cycles through RGB waiting to read a new card
    }
    else {
      normalModeOn();     // Normal mode, blue Power LED is on, all others are off
    }
  }
  while (!successRead);   //the program will not go further while you not get a successful read

Please put the monkey in a cage when you are programming. Letting the monkey randomly pound the tab key really isn't a good idea.

There is a tool on the Tools menu, Auto Format, that can fix your monkey's doings.

Putting every { on a new line also helps make the start and end of blocks and functions clearer.

      for(int b=0;b<15301;b+1)

You haven't done anything to fix this. The last clause should be b++, b+=1, or b=b+1.

Until the code all works exactly as you want, all that code to write to the LCD just gets in the way. One line to print to Serial is good. 25 lines to write to the LCD is bad.

First of all thank you for the tip on auto format haha saved me alot of time looking for where the brackets end and stuff.

As for the tagScanned() method i still dont quite get it.. because most of the MFRC522 coding was already done on the instructables and i dont really know how it works.
I do realise the problem is the bottle neck present here:

     do {
    successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      cycleLeds();              // Program Mode cycles through RGB waiting to read a new card
    }
    else {
      normalModeOn();     // Normal mode, blue Power LED is on, all others are off
    }
  }
  [b]while (!successRead);   //the program will not go further while you not get a successful read[/b]

But have no idea how to correct it with the tagScanned() method. Is it possible for you to impart your knowledge to me?

Hi anyone looking just an update.
After 5 reiterations, i got pretty much most of the code working except for one problem.
When i want to use an event(me scanning my registered RFID Tag) to get out of my alarm loop, it always seems to get out by itself even without me scanning the tag.
It seems that the command if ( findID(readCard) ) is merely just to check if the RFID is registered in the eeprom and if so carry on the loop. Is there anyway to exit the loop through the use of RFID reliably? Thank you

Test_4_GOTO_original.ino (27.6 KB)