15 RFID readers to a mega problem

Posted byu/exoticrumble
just now
15 RFID readers to MEGA problem
I have 15 rfid readers to a mega. IT works inconsistently

I left all 15 cards on the reader and usually at least every 4 mins it will flicker. Meaning unread and read a card randomly.

I have 16 readers assigned to deal with an issue as of now.

Any ideas

/ DEFINES
// Provides debugging information over serial connection if defined
#define DEBUG

// LIBRARIES
// Standard SPI library comes bundled with the Arduino IDE
#include <SPI.h>
#include <MFRC522.h>

// CONSTANTS
// The number of RFID readers
const byte numReaders = 16;
// Each reader has a unique Slave Select pin
const byte ssPins[] = {37,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
// They'll share the same reset pin
const byte resetPin = 1;
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = 8;
// this pin is for BlackLight
const byte blackLight = 6;
// The sequence of NFC tag IDs required to solve the puzzle
const String correctIDs[] = {"","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5","a0466fa5"};

boolean allRfidsActive;

// GLOBALS
// Initialise an array of MFRC522 instances representing each reader
MFRC522 mfrc522[numReaders];
// The tag IDs currently detected by each reader
String currentIDs[numReaders];


/**
 * Initialisation
 */
void setup() {

  #ifdef DEBUG
  // Initialise serial communications channel with the PC
  Serial.begin(9600);
  Serial.println(F("Serial communication started"));
  #endif
  
  // Set the lock pin as output and secure the lock
  pinMode(lockPin, OUTPUT);

  // Set black light pin
  pinMode(blackLight, OUTPUT);

  digitalWrite(blackLight, LOW);

  digitalWrite(lockPin, LOW);

  allRfidsActive = true;


  
  // Initialise the SPI bus
  SPI.begin();

  for (uint8_t i=0; i<numReaders; i++) {
  
    // Initialise the reader
    // Note that SPI pins on the reader must always be connected to certain
    // Arduino pins (on an Uno, MOSI=> pin11, MISO=> pin12, SCK=>pin13)
    // The Slave Select (SS) pin and reset pin can be assigned to any pin
    mfrc522[i].PCD_Init(ssPins[i], resetPin);
    
    // Set the gain to max - not sure this makes any difference...
    // mfrc522[i].PCD_SetAntennaGain(MFRC522::PCD_RxGain::RxGain_max);
    
  #ifdef DEBUG
    // Dump some debug information to the serial monitor
    Serial.print(F("Reader #"));
    Serial.print(i + 1);
    Serial.print(F(" initialised on pin "));
    Serial.print(String(ssPins[i]));
    Serial.print(F(". Antenna strength: "));
    Serial.print(mfrc522[i].PCD_GetAntennaGain());
    Serial.print(F(". Version : "));
    mfrc522[i].PCD_DumpVersionToSerial();
  #endif
    
    // Slight delay before activating next reader
    delay(100);
  }
  
  #ifdef DEBUG
  Serial.println(F("--- END SETUP ---"));
  #endif
}

/**
 * Main loop
 */
void loop() {

  // Assume that the puzzle has been solved
  boolean puzzleSolved = true;

  // Assume that the tags have not changed since last reading
  boolean changedValue = false;

  // Assume all RFIDs have a tag 
  allRfidsActive = true;

  // Loop through each reader
  for (uint8_t i=0; i<numReaders; i++) {

    // Initialise the sensor
    mfrc522[i].PCD_Init();
    
    // String to hold the ID detected by each sensor
    String readRFID = "";
    
    // If the sensor detects a tag and is able to read it
    if(mfrc522[i].PICC_IsNewCardPresent() && mfrc522[i].PICC_ReadCardSerial()) {
      // Extract the ID from the tag
      readRFID = dump_byte_array(mfrc522[i].uid.uidByte, mfrc522[i].uid.size);
    }

    
    if (readRFID != "") {
      //turn light[i] on
      
//      Serial.print("Turn light ");
//      Serial.print(i);
//      Serial.println(" on.");

    } else if (i > 0) {
      // turn light[i] off
      allRfidsActive = false;

    }
    
    // If the current reading is different from the last known reading
    if(readRFID != currentIDs[i]){
      // Set the flag to show that the puzzle state has changed
      changedValue = true;
      // Update the stored value for this sensor
      currentIDs[i] = readRFID;
    }
    
    // If the reading fails to match the correct ID for this sensor 
    if(currentIDs[i] != correctIDs[i]) {
      // The puzzle has not been solved
      puzzleSolved = false;
    }

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

  if (allRfidsActive == true) {
  // turn on blacklight thing
    digitalWrite(blackLight, HIGH);
  } else {
    digitalWrite(blackLight, LOW);

  }

  #ifdef DEBUG
  // If the changedValue flag has been set, at least one sensor has changed
  if(changedValue){
    // Dump to serial the current state of all sensors
    for (uint8_t i=0; i<numReaders; i++) {
      Serial.print(F("Reader #"));
      Serial.print(String(i + 1));
      Serial.print(F(" on Pin #"));
      Serial.print(String((ssPins[i])));
      Serial.print(F(" detected tag: "));
      Serial.println(currentIDs[i]);
    }
    Serial.println(F("---"));
  }
  #endif

  // If the puzzleSolved flag is set, all sensors detected the correct ID
  if(puzzleSolved){
    onSolve();
  }
 
  // Add a short delay before next polling sensors
  //delay(100); 
}

/**
 * Called when correct puzzle solution has been entered
 */
void onSolve(){

  #ifdef DEBUG
  // Print debugging message
  Serial.println(F("Puzzle Solved!"));
  #endif
  
  // Release the lock
  digitalWrite(lockPin, HIGH);

  while(true) {
    delay(1000);
  }
  
}

/**
 * Helper function to return a string ID from byte array
 */
String dump_byte_array(byte *buffer, byte bufferSize) {
  String read_rfid = "";
  for (byte i=0; i<bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
  return read_rfid;
}

No experience with RFID readers, but how do you power the Mega.
I see that the readers can draw 26mA each.
That, and the 70mA of the Mega adds up to 460mA.
About the limit for USB, and a problem with external power.
The Mega could be rebooting.
In that case you should see "Serial communication started" every time.
Leo..

The arduino Mega as far as I can tell is not resetting at all

Since you are using a Mega, I would start by getting rid of all your String variables and replace them with C strings (NULL terminated char array) since 'String's can fragment your memory and cause corruption, especially when using it like this

String dump_byte_array(byte *buffer, byte bufferSize) {
  String read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
  return read_rfid;
}

Do you think that would cause it to fail as consistently as it is like once every 5 mins?

rumble25:
Do you think that would cause it to fail as consistently as it is like once every 5 mins?

Yes.
As would power supply issues and lack of decoupling. As you don’t seem willing to try the suggestions we are giving you I wonder why you posted in the first place?

I am working on testing different power sources at the moment as well as learning the C string as the above post mentioned. Was just curious..