Arduino RFID loop reading

Hi everybody,
i’m using this “open door” sketch to read an RFID card with Arduino and it works fine, everytime i put a card to the reader Arduino scans it and turns on the relè which opens the door.

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH); // Make sure door is locked
  //Protocol Configuration
  Serial.begin(9600);	 // Initialize serial communications with PC
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  Serial.println("##### RFID Door Unlocker #####");
  Serial.println("");
  Serial.println("Waiting PICCs to bo scanned :)");
}

///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  int successRead;  // Variable integer to keep if we have Successful Read from Reader
  do {
    successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      programModeOn(); // 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) {
    programMode = false;  // next time will enter in normal mode
    if ( isMaster(readCard) ) {  //If master card scanned again exit program mode
      Serial.println("This is Master Card"); 
      Serial.println("Exiting Program Mode");
      return;
    }
    else {	
      if ( findID(readCard) ) { //If scanned card is known delete it
        Serial.println("I know this PICC, so removing");
        deleteID(readCard);   
        Serial.println("Exiting Program Mode");
      }
      else {                    // If scanned card is not known add it
        Serial.println("I do not know this PICC, adding...");
        writeID(readCard);  
        Serial.println("Exiting Program Mode");
      }
    }
  }
  else {
    if ( isMaster(readCard) ) {  // If scanned card's ID matches Master Card's ID enter program mode
      programMode = true;
      Serial.println("Hello Master - Entered Program Mode");
      int count = EEPROM.read(0); // Read the first Byte of EEPROM that
      Serial.print("I have ");    // stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(" record(s) on EEPROM");
      Serial.println("");
      Serial.println("Scan a PICC to ADD or REMOVE");
    }
    else {
      if ( findID(readCard) ) {        // If not, see if the card is in the EEPROM 
        Serial.println("Welcome, You shall pass");
        openDoor();       // Open the door lock for 300 ms
      }
      else {				// If not, show that the ID was not valid
        Serial.println("You shall not pass");
        failed(); 
      }
    }
  }
}

///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int 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;
  }
  // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  Serial.println("Scanned PICC's UID:");
  for (byte i = 0; i < mfrc522.uid.size; i++) {  // for size of uid.size write uid.uidByte to readCard
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

Now i want to build a system where the card needs to be positioned and left on the RFID reader area so the relè can be turned on as long as the card remain on the reader (no limit of time), if the user take off the card, the relè turns off. I’m thinking doing that by introducing a frequent time of scan so Arduino can feel or not the card every sec, but i’m not that good yet, i’m learning and i need your advise about how can i do to make it works.

Thank you all!!

Now i want to build a system where the card needs to be positioned and left on the RFID reader area so the relè can be turned on as long as the card remain on the reader (no limit of time), if the user take off the card, the relè turns off.

Whether that is even possible depends on the RFID reader that you have. The two that I have only detect the card when it becomes available. They do nothing when the card moves out of range.

You need to tell us something about the RFID reader that you have.

Hi, thanks for the answer, i have an Arduino Pro Mini with aRFID RC522

Hi, thanks for the answer, i have an Arduino Pro Mini with aRFID RC522

Unfortunately, your link doesn't work.

Here the data sheet http://www.nxp.com/documents/data_sheet/MFRC522.pdf

I don't see anything in that data sheet that says that the RFID reader works the way you want to use it.

PaulS:
I don't see anything in that data sheet that says that the RFID reader works the way you want to use it.

I read that someone using this

if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }

but i don't know if this is the right way

but i don't know if this is the right way

Send me your Arduino and RFID reader. I'll make up some code and add that snippet and test it for you.