RC522: How to execute a command once only when an RFID card is not present?

My code is super simple, but I'm at a loss how to do a simple task.

Each time an RFID card is read, I want it to trigger an event once. This is working fine in isolation.

However, I also want a different one-time event to occur each time a card is taken away. This bit seems to mess up the whole thing.

I'm using the MFRC522 Library: GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522

Could anyone tell me the method to do both within the same code? I'm kind of green as to all this.

Many thanks in advance :slight_smile:

My code is here:

#include <MFRC522.h> 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

bool executed = false;


void setup() {
  Serial.begin(9600);   // Initiate a serial communication    
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
}


void loop() { 

  if ( mfrc522.PICC_IsNewCardPresent()) {    
    if ( mfrc522.PICC_ReadCardSerial()) {
      Serial.println("Great!.. each time a card is read and re-read this text will print *ONCE ONLY*");
    }     
  } 

// BELOW I WANT TO EXECUTE A COMMAND *ONCE ONLY* EACH TIME A CARD IS TAKEN AWAY
// WHEN UNCOMMENTED, THE "Oh dear.." PRINTS CONTINUOUSLY 

//  if ( !mfrc522.PICC_IsNewCardPresent()) {  
//    Serial.println("Oh dear... this seems to keep printing... ");  
//  }

  mfrc522.PICC_HaltA();          // Halt PICC
  mfrc522.PCD_StopCrypto1();     // Stop encryption on PCD  
}