Advice on RC522 RFID reading system

Hi there. Seeking advice on the best way to go about my project.

My system is very simple. I need to associate different midi commands with different RFID tags. However, I need the tags to be read as quickly as possible as they trigger musical events.

Currently, I’m using a really quick method that simply checks the first byte in the PICC code of each tag:

  if (nuidPICC[0] == 7) {        //  check first byte of PICC
    Send midi;   //(pseudocode)    
    }

This worked fine initially, but my system has expanded to where I need 100 tags. Obviously now I am getting overlapping nuidPICC[0]'s, so I need a new method.

I’m asking if anyone has any recommendations on how best to manage this system. As I said, it is quite simple in that known tags are read to trigger midi events. I think some kind of an array situation, but I’m not sure what is the fastest and most efficient way. My Arduino coding is so-so.

I don’t see anything that I perceive to be of use in the examples of the RC522 library.

Thanks in advance for any tips :slight_smile:

Here is some code to give an idea of what I’m trying to do:

#include "MIDIUSB.h"

// RFID Stuff
#include <SPI.h>
#include <MFRC522.h>
//#define MFRC522_SPICLOCK SPI_CLOCK_DIV16     // MFRC522 accept upto 10MHz
#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_1_PIN        10         // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
#define NR_OF_READERS   1
#define READER_1   0


byte ssPins[] = {SS_1_PIN};

MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

// Init array that will store new NUID 
byte nuidPICC[4];

//MIDI SECTION
int midiChannel1 = 0;  


void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

  // RFID Stuff
  SPI.begin();        // Init SPI bus
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
  }  
}



void loop() {
  ObjectsForSongs();
}



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



void ObjectsForSongs(){
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {

    // Look for new cards
    if (mfrc522[READER_1].PICC_IsNewCardPresent() && mfrc522[READER_1].PICC_ReadCardSerial()) {
        nuidPICC[0] = mfrc522[0].uid.uidByte[0];
        Serial.print("                   Reader 2: ");           
        Serial.println(nuidPICC[0]);


    if (nuidPICC[0] == 7) {        // GROOVE ARMADA
        noteOn(midiChannel1, 1, 127); //channel, note, velocity
        noteOn(midiChannel1, 1, 0);        //channel, note, velocity
        MidiUSB.flush();         
    } 

    if (nuidPICC[0] ==39) {        // BIG MATROSKA
        noteOn(midiChannel1, 2, 127); //channel, note, velocity
        noteOn(midiChannel1, 2, 0);        //channel, note, velocity
        MidiUSB.flush();         
    } 
    
    if (nuidPICC[0] == 71) {        // ACE OF SPADES
        noteOn(midiChannel1, 5, 127); //channel, note, velocity
        noteOn(midiChannel1, 5, 0);        //channel, note, velocity
        MidiUSB.flush();         
    } 



      // Halt PICC
      mfrc522[reader].PICC_HaltA();
      // Stop encryption on PCD
      mfrc522[reader].PCD_StopCrypto1();
    } //if (mfrc522[reader].PICC_IsNewC
  } //for(uint8_t reader
}



// These are midi functions specifically for the Arduino Micro Pro
void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

This worked fine initially, but my system has expanded to where I need 100 tags. Obviously now I am getting overlapping nuidPICC[0]'s, so I need a new method.

Just check all bytes of the ID, checking a few bytes needs about 1-2µs, I'm sure your midi system won't notice.