Switching on and off a LED using RFID

This is part of a bigger project I’m working on - I thought it would be better to break it down into bits which I can then later join up

I’ve just started working with Arduino and for this question I’m trying to switch a LED on when I scan a RFID and switch the LED off when I scan the same RFID the second time

This is my code for reading the RFID and switching on the LED - it’s basically one of the examples but simplified

 * Initial Author: ryand1011 (https://github.com/ryand1011)
 * Reads data written by a program such as "rfid_write_personal_data.ino"
 * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
 * Uses MIFARE RFID card using RFID-RC522 reader
 * Uses MFRC522 - Library
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15

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

constexpr uint8_t RST_PIN = 5;     // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 53;     // Configurable, see typical pin layout above
int led = 47;

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

void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();                                              // Init MFRC522 card
  Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read
  pinMode(led, OUTPUT);

void loop() {
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;


  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {

  Serial.println(F("**Card Detected:**"));

  byte readCard[7];
  for (int i = 0; i < mfrc522.uid.size; i++) {  // 
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  if (readCard[0]!=0)
  {digitalWrite(led, HIGH); 
  Serial.println(F("\n**End Reading**\n"));

  delay(1000); //change value if you want to read cards faster


How can I code it to switch it off when the same RFID has been scanned the second time?

Thank you

I am not sure your requirements really make sense.

Anyway every time you read an ID you need to compare that ID against all the other IDs you have already read.
If the ID is a new one you store the ID and turn the LED on.
If the ID is an existing one you just turn the ID off (and maybe you also want to delete it from the list of IDs you have read?).

One problem you may have is storing the IDs as memory is limited and you may need to use an SD card.