The same code will execute fine in loop() but not in setup()

Hello everybody! This is my first post so be gentle!

What i am trying to do is a somewhat complicated project with RFIDs. Everything works fine except from what I am about to describe.

What I am using: An arduino micro and a MFRC-522 and some other stuff that are not relevant to the question at hand. Not to make things too complicated I have omitted the non pertinent code and have written a new code that recreates the problem. All connections are good and everything else works just fine.
What I am trying to do is to be able to wipe the EEPROM memory containing the stored cards. To be able to do that I what to use a Master Card. Once setup runs you are asked to scan the card to wipe the memory. After 10sec without a scan the wiping is skipped.
So here is the code followed by my experience.

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

#define WIPE_DELAY 10000
#define UID_LENGTH 4
#define SS_PIN 18
#define RST_PIN 19
MFRC522 mfrc522(SS_PIN, RST_PIN);

unsigned long timeStamp = 0;
unsigned long currentTime = 0;
bool match = false;
bool programMode = false;
char successRead = 0;
byte storedCard[4];
byte readCard[4];
byte masterCard[4];

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  while (!Serial);
  mfrc522.PCD_DumpVersionToSerial();
  Serial.println(F("Do you whant to wipe the stored Cards? SETUP"));
  Serial.println(F("Scan Master Card to wipe"));
  char memoryWiped = 0;
  timeStamp = millis();
  currentTime = millis();
  while(currentTime - timeStamp <= WIPE_DELAY) {
    successRead = getID(UID_LENGTH);
      if (isMaster(readCard)) {
        Serial.println(F("Wiping EEPROM"));
        for (uint8_t x = 0; x < EEPROM.length(); x = x + 1) {
          if (EEPROM.read(x) == 0) {
          }
          else {
            EEPROM.write(x, 0);
          }
        }
        Serial.println(F("EEPROM Wiped"));
        
        memoryWiped = 1;
      }
    else {
      currentTime = millis();
    }
  }
  if(!memoryWiped) {
    Serial.println(F("Memory Wipe Skiped"));
  }
set_master();
}

void loop() {
  Serial.println(F("Do you whant to wipe the stored Cards? LOOP"));
  Serial.println(F("Scan Master Card to wipe"));
  char memoryWiped = 0;
  timeStamp = millis();
  currentTime = millis();
  while(currentTime - timeStamp <= WIPE_DELAY) {
    successRead = getID(UID_LENGTH);
      if (isMaster(readCard)) {
        Serial.println(F("Wiping EEPROM"));
        for (uint8_t x = 0; x < EEPROM.length(); x = x + 1) {
          if (EEPROM.read(x) == 0) {
          }
          else {
            EEPROM.write(x, 0);
          }
        }
        Serial.println(F("EEPROM Wiped"));
        
        memoryWiped = 1;
      }
    else {
      currentTime = millis();
    }
  }
  if(!memoryWiped) {
    Serial.println(F("Memory Wipe Skiped"));
  }

}


uint8_t getID(int UID_size) {
  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;
  }
  Serial.println(F("Scanned PICC's UID:"));
  for (uint8_t i = 0; i < UID_size; i++) {
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA();                     // Stop reading
  return 1;
}//getID

bool isMaster(byte test[]) {
  if (checkTwo(test, masterCard)) {
    return true;
  }
  else
  return false;
}//isMaster


bool checkTwo(byte a[], byte b[]) {
  if (a[0] != 0)                          // Make sure there is something in the array first
  match = true;                         // Assume they match at first
  for (uint8_t k = 0; k < 4; k++) {               // Loop 4 times
    if (a[k] != b[k])                     // IF a != b then set match = false, one fails, all fail
    match = false;
  }
  if (match) {                          // Check to see if if match is still true
    return true;                        // Return true
  }
  else  {
    return false;                       // Return false
  }
}//checkTwo


void set_master(void) {
  bool LED_state = LOW;
  if (EEPROM.read(1) != 143) {
    Serial.println(F("No Master Card Defined"));
    Serial.println(F("Scan Card to Define"));
    timeStamp = millis();
    currentTime = millis();
    do {
      successRead = getID(UID_LENGTH);
       } while (!successRead);
    for(int j=0; j<UID_LENGTH; j++) {
      EEPROM.write(2 + j, readCard[j]);
    }
    EEPROM.write(1, 143);
    Serial.println(F("Master Card Defined"));
  }
  Serial.println(F("-------------------"));
  Serial.println(F("Master Card's UID"));
  for(int i=0; i<UID_LENGTH; i++) {
    masterCard[i] = EEPROM.read(2 + i);
    Serial.print(masterCard[i], HEX);
  }
  for (uint8_t i = 0; i < 4; i++) {
    readCard[i] = 0;
  }
  Serial.println("");
  Serial.println(F("-------------------"));
  Serial.println(F("Setup Complete"));
}//set_master

What I noticed was that it seemed as if the execution never entered this if “if (isMaster(readCard))” but I was using the exact same if in my loop to change other things (in the omitted code) and it was working perfectly. What I did was write the code above that has the same copy in setup() and in loop() apart from the message to the serial port that has an extra SETUP and LOOP respectively to understand what code is running. What I found out is that in setup it does not enter the if while in loop it does. Any help would be greatly appreciated. I must admit I am perplexed.

You are calling isMaster() before setMaster(). The initial state of the arrays (readCard etc) will be blank so checkTwo() will return false.

Thank you quilkin for your reply. setMaster() is called intentionally after the check to wipe because the only reason to wipe the memory is to change previously the set master and the additional cards. The testing procedure is as follows: 1. set a master card 2. reset μC 3. try to wipe. Since the cards are saved in the EEPROM resetting does not erase the records so that is not the problem.

setMaster() is called intentionally after the check to wipe

But that is the problem. You define the master card's credentials AFTER you determine that a scanned card is, or is not, the master.

You need to call setMaster() first. That way, the master card will be whatever was master last time. Then, the card scanned to wipe EEPROM may, or may not, be the master, and EEPROM may, or may not, be wiped.

That you need to define a new master card after wiping EEPROM is irrelevant.

Think about changing your password. You are required to supply your current password. The current master card's credentials are what matters, not the credentials of the card that you want to become the master.

OK I am a complete and total IDIOT!

Now I get it, the problem isn’t that the card is not in the EEPROM, the problem is that masterCard is not set in the RAM.

I added this function

void initMasterCard(void) {
for(int i=0; i<UID_LENGTH; i++) {
		masterCard[i] = EEPROM.read(2 + i);
		Serial.print(masterCard[i], HEX);
	}
}

and naturally everything is fine! :-[

Thank you guys for the nudge!

OK I am a complete and total IDIOT!

That puts you one up on me. I have some parts missing. 8)