RFID Code

Hello, everyone! This is my first question. It is quite a big one and has me very confused :confused:

So, I’m using an RFID chip (insert jaws music :slight_smile: ). I am wondering about the code (in your mind you should be thinking uh-oh right now). So here is some code (from https://www.instructables.com/id/Arduino-RFID-Reader-MFRC522-Turorial/)

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

//creating mfrc522 instance
#define RSTPIN 9
#define SSPIN 10
MFRC522 rc(SSPIN, RSTPIN);

int readsuccess;

/* the following are the UIDs of the card which are authorised
    to know the UID of your card/tag use the example code 'DumpInfo'
    from the library mfrc522 it give the UID of the card as well as 
    other information in the card on the serial monitor of the arduino*/

    //byte defcard[4]={0x32,0xD7,0x0F,0x2B}; // if you only want one card
byte defcard[][4]={{0xC6,0xFD,0xC5,0x32},{0x32,0xD7,0x0F,0xB}}; //for multiple cards
int N=2; //change this to the number of cards/tags you will use
byte readcard[4]; //stores the UID of current tag which is read

void setup() {
Serial.begin(9600);

SPI.begin();
rc.PCD_Init(); //initialize the receiver  
rc.PCD_DumpVersionToSerial(); //show details of card reader module

pinMode(6,OUTPUT); //led for authorised
pinMode(5,OUTPUT); //led for not authorised

Serial.println(F("the authorised cards are")); //display authorised cards just to demonstrate you may comment this section out
for(int i=0;i<N;i++){ 
  Serial.print(i+1);
  Serial.print("  ");
    for(int j=0;j<4;j++){
      Serial.print(defcard[i][j],HEX);
      }
      Serial.println("");
     }
Serial.println("");

Serial.println(F("Scan Access Card to see Details"));
}


void loop() {
  
readsuccess = getid();

if(readsuccess){
 
  int match=0;

//this is the part where compare the current tag with pre defined tags
  for(int i=0;i<N;i++){
    Serial.print("Testing Against Authorised card no: ");
    Serial.println(i+1);
    if(!memcmp(readcard,defcard[i],4)){
      match++;
      }
    
  }
    
  
   if(match)
      {Serial.println("CARD AUTHORISED");
        digitalWrite(6,HIGH);
        delay(2000);
        digitalWrite(6,LOW);
        delay(500); 
        
      }
     else {
      Serial.println("CARD NOT Authorised");
      digitalWrite(5,HIGH);
        delay(2000);
        digitalWrite(5,LOW);
        delay(500); 
   
      }
  
  }
}
//function to get the UID of the card
int getid(){  
  if(!rc.PICC_IsNewCardPresent()){
    return 0;
  }
  if(!rc.PICC_ReadCardSerial()){
    return 0;
  }
 
  
  Serial.println("THE UID OF THE SCANNED CARD IS:");
  
  for(int i=0;i<4;i++){
    readcard[i]=rc.uid.uidByte[i]; //storing the UID of the tag in readcard
    Serial.print(readcard[i],HEX);
    
  }
   Serial.println("");
   Serial.println("Now Comparing with Authorised cards");
  rc.PICC_HaltA();
  
  return 1;
}

Okay, I don’t know if I’m the only one, but I’m about to explode after seeing this. Could someone please explain this?

Here is my code, which unfortunately doesn’t work. But why?

// MFRC522 - Version: Latest 
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <deprecated.h>
#include <require_cpp11.h>
#include <SPI.h>

const int SS_PIN = 53;
const int RST_PIN = 5;

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


void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();

}
void loop() 
{
  
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }

  String content = "";
  
  for (int i = 0; i < 4; i++) {
    Serial.println(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
    content.concat(" ");
  }

  if (content == "99 5d 19 2b") {
    Serial.println("Match");
  }
  
}

Can someone, anyone try to explain what is going on, I’m “partly” new. I know a lot about other languages, but not this one. If you are going to answer, could you please explain everything?

Thanks! :slight_smile: