Trying to get RFID MFRC522 to detect Unique ID but also detect the REMOVAL of it

I am dabbling in escape room projects. Been having a lot of fun and i came up with this idea for a chest that is locked via a magnet until three blocks (out of many possible blocks) have been placed on the lid in a specific order.
Using one arduino to do this seems possible but is very difficult, tends to run into trouble with the SPI the moment you connect a second one in. So i am taking the easy route and having each rfid sensor work off of a single arduino, which all output to a master arduino controlling the maglock.

Problem is most RFID projects have a “Detect once = Output HIGH until reset” set up, rather then going LOW once the tag is removed, which is what i am going to require.

Like the title says, i have been hunting around for some code that will allow me to use an MFRC522 to detect a tags unique ID and activate an output, but only for as long as it is detecting that tag, when the tag is removed i want the pin to go LOW again. I am using an arduino nano.

I am not yet clever enough at arduino or coding in general to be able to code this myself. So i have been hunting the forums and various internet crevices for someone who had already implemented a solution. I have come up mostly dry. However I did manage to find someone who had got the activate/deactivate part down but it was using any tag, not giving the option for only detecting a specific UID. Code for that is below, can anyone tell me how to alter this so it will only accept a specific UID? Or better yet actually post the altered code, on the off chance i have no idea how to implement your solution. I’m generally better at working backwards from things.

Or any code that will do what i’ve described above…

Thanks in advance =)

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

#define RST_PIN         9
#define SS_PIN          10
#define Green 7

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

void setup() {
  pinMode(Green, OUTPUT);
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();

}
uint8_t buf[10] = {};
MFRC522::Uid id;
MFRC522::Uid id2;
bool is_card_present = false;
uint8_t control = 0x00;
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
  char tmp[16];
  for (int i = 0; i < length; i++) {
    sprintf(tmp, "0x%.2X", data[i]);
    Serial.print(tmp); Serial.print(" ");
  }
}

//*****************************************************************************************//

void cpid(MFRC522::Uid *id) {
  memset(id, 0, sizeof(MFRC522::Uid));
  memcpy(id->uidByte, mfrc522.uid.uidByte, mfrc522.uid.size);
  id->size = mfrc522.uid.size;
  id->sak = mfrc522.uid.sak;
}

void loop() {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;

  // Look for new cards
  if ( !mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( !mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  bool result = true;
  uint8_t buf_len = 4;
  cpid(&id);
  Serial.print("NewCard ");
  PrintHex(id.uidByte, id.size);
  Serial.println("");

  while (true) {
    digitalWrite(Green, HIGH);
    control = 0;
    for (int i = 0; i < 3; i++) {
      if (!mfrc522.PICC_IsNewCardPresent()) {
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('a');
          control |= 0x16;
        }
        if (mfrc522.PICC_ReadCardSerial()) {
          //Serial.print('b');
          control |= 0x16;
        }
        //Serial.print('c');
        control += 0x1;
      }
      //Serial.print('d');
      control += 0x4;
    }

    //Serial.println(control);
    if (control == 13 || control == 14) {
      //card is still there
    } else {
      break;
    }
  }
  Serial.println("CardRemoved");
  digitalWrite(Green, LOW);
  delay(500); //change value if you want to read cards faster

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();

}

It sounds as if you are attempting to use more than one RFID device. :astonished:

Paul__B:
It sounds as if you are attempting to use more than one RFID device. :astonished:

nah just like i said up top:
"So i am taking the easy route and having each rfid sensor work off of a single arduino, which all output to a master arduino controlling the maglock."
So, more then one in the project, but only one reader per arduino, one per program. with the only difference in the program being the UID of the tag that it accepts.

Sounds like the really hard way to do it, as you now have to communicate between the Arduinox!

So how far spaced apart are the MFRC522s?

About 15cm.

OK, that sounds unlikely to cause a problem with interaction.

Anyone? I'd really appreciate it.