Question on Use of RFID sensors

Hello. I have a question on how to write the code in order to do the following tasks.

How can I make an LED light turn on when you scan the card once, then turn off when you scan the same card again.

This is my current setup, it’s for a science fair and I would really appreciate any help I could get!

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

#define RST_PIN 9 // reset핀 설정
#define SS_PIN 10 // 데이터를 주고받는 역할의 핀( SS = Slave Selector )

MFRC522 mfrc(SS_PIN, RST_PIN); // 이 코드에서 MFR522를 이용하기 위해 mfrc객체를 생성해 줍니다.
int c;
int IDCARD1=0;
int IDCARD2=0;
int LEDA=0;
int LEDB=0;
int LEDC=0;

void setup() {
Serial.begin(9600); // 시리얼 통신, 속도는 9600
SPI.begin(); // SPI 초기화(SPI : 하나의 마스터와 다수의 SLAVE(종속적인 역활)간의 통신 방식)
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
}

void loop() {
if ( ! mfrc.PICC_IsNewCardPresent() || ! mfrc.PICC_ReadCardSerial() ) { // 태그 접촉이 되지 않았을때 또는 아이디가 읽혀지지 않았을때
delay(500);
return; // return
}
if(IDCARD1==0 && mfrc.uid.uidByte[0] == 176 && mfrc.uid.uidByte[1] == 54
&& mfrc.uid.uidByte[2] == 154 && mfrc.uid.uidByte[3] == 27) {
Serial.println(“1번 사용자님 안녕하세요”);
IDCARD1=IDCARD1+1;
LEDA=LEDA+1;
LEDB=LEDB+1;
LEDC=LEDC+1;
delay (500);
}

if (IDCARD2==0 && mfrc.uid.uidByte[0] == 87 && mfrc.uid.uidByte[1] == 12
&& mfrc.uid.uidByte[2] == 76 && mfrc.uid.uidByte[3] == 39) {
Serial.println(“2번 사용자님 안녕하세요”);
IDCARD2=IDCARD2+1;
LEDA=LEDA+1;
LEDB=LEDB+0;
LEDC=LEDC+1;
delay (500);
}
if (LEDA>0)
{digitalWrite(2, HIGH);}
else
{digitalWrite(2, LOW);}
if (LEDB>0)
{digitalWrite(3, HIGH);}
else
{digitalWrite(3, LOW);}
if (LEDC>0)
{digitalWrite(4, HIGH);}
else
{digitalWrite(4, LOW);}
delay (wait);

if(IDCARD1==1 && mfrc.uid.uidByte[0] == 176 && mfrc.uid.uidByte[1] == 54
&& mfrc.uid.uidByte[2] == 154 && mfrc.uid.uidByte[3] == 27) {
Serial.println(“1번 사용자님 안녕히가세요”);
IDCARD1=IDCARD1-1;
LEDA=LEDA-1;
LEDB=LEDB-1;
LEDC=LEDC-1;
delay (500);
}

if (IDCARD2==1 && mfrc.uid.uidByte[0] == 87 && mfrc.uid.uidByte[1] == 12
&& mfrc.uid.uidByte[2] == 76 && mfrc.uid.uidByte[3] == 39) {
Serial.println(“2번 사용자님 안녕히가세요”);
IDCARD2=IDCARD2-1;
LEDA=LEDA-1;
LEDB=LEDB+0;
LEDC=LEDC-1;
delay (500);
}
if (LEDA>0)
{digitalWrite(2, HIGH);}
else
{digitalWrite(2, LOW);}
if (LEDB>0)
{digitalWrite(3, HIGH);}
else
{digitalWrite(3, LOW);}
if (LEDC>0)
{digitalWrite(4, HIGH);}
else
{digitalWrite(4, LOW);}
}

start by reading the instructions on how to post code correctly here… that will help get support from the community.

Clarify if the card needs to be hardcoded or the first card presented is the one triggering the LED and then that’s the one you need to wait for to be presented again

This code is built from a modified / simplified example of the library

/*
   ----------------------------------------------------------------------------
   This is extracted from MFRC522 library example; see https://github.com/miguelbalboa/rfid
   for further details and other examples.

   NOTE: The library file MFRC522.h has a lot of useful info. Please read it.

   Released into the public domain.
   ----------------------------------------------------------------------------
   Example sketch/program which will try the most used default keys listed in
   https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys to dump the
   block 0 of a MIFARE RFID card using a RFID-RC522 reader.

   Typical pin layout used:
   -----------------------------------------------------------------------------------------
               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>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

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

void setup() {
  Serial.begin(115200);       // Initialize serial communications @ 115200 bauds
  while (!Serial);            // Needed for Arduinos based on ATMEGA32U4
  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card reader
  Serial.println(F("ready"));
}

/*   Helper routine to dump a byte array as hex values to Serial.*/
void dump_byte_array(uint8_t *buffer, uint8_t bufferSize) {
  for (uint8_t i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) return;

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

  // Found one we can read, show UID which is contained in mfrc522.uid.uidByte array, this array holds mfrc522.uid.size bytes
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(); 
}

You could use it as a starting point.

Once the card is read, it’s UID is in the mfrc522.uid.uidByte array, this array holds mfrc522.uid.size bytes. You could use the [url=http://www.cplusplus.com/reference/cstring/memcmp/]memcmp()[/url] function to compare that with some other UIDs