RFID-RC522 not always detecting

Hi all,

i’m buidling a puzzle in a treasurehunt game. I have a Arduino Nano, 3 neopixel leds and a RFID-RC522 sensor and then about 20 tags. The tags have to pass the sensor in a specific order. Try the wrong one and the leds go red, pass the right one and they turn green.

When i power the setup up, the neopixels start with their progammed start colour, the red light on rfid reader turns on (D1) and i can get started with the 20 tags.

But now this: it often happens that i power up and everything looks normal, but the reader doesnt read.

Any clues on how to solve this??

My code (newbie here so it’s a bit messy and i had to cut a bit our in between to stay within 9000 characters):

#include <AddicoreRFID.h>
#include <SPI.h>

#define uchar unsigned char
#define uint unsigned int
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 4
#define AANTALLEDS 3

uchar fifobytes;
uchar fifoValue;

AddicoreRFID myRFID; // create AddicoreRFID object to control the RFID module

const int chipSelectPin = 10;
const int NRSTPD = 5;
int teller;
String coordinaat ="N5203265E00423071";
String opbouw ="";
String teststring ="";

byte kr1;
byte kr2;
byte kr3;
String kr1Sw;
String kr2Sw;

//Maximum length of the array
#define MAX_LEN 16


  // Parameter 1 = number of pixels in strip
  // Parameter 2 = Arduino pin number (most are valid)
  // Parameter 3 = pixel type flags, add together as needed:
  //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  Adafruit_NeoPixel strip = Adafruit_NeoPixel(AANTALLEDS, PIN, NEO_RGB + NEO_KHZ800);

void setup() {   
        
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
  
  Serial.begin(9600);                        // RFID reader SOUT pin connected to Serial RX pin at 9600bps 
 
  // start the SPI library:
  SPI.begin();
  
  pinMode(chipSelectPin,OUTPUT);              // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin 
    digitalWrite(chipSelectPin, LOW);         // Activate the RFID reader
  pinMode(NRSTPD,OUTPUT);                     // Set digital pin 10 , Not Reset and Power-down
    digitalWrite(NRSTPD, HIGH);

  myRFID.AddicoreRFID_Init();  
}

void loop()
{
   uchar i, tmp, checksum1;
 uchar status;
        uchar str[MAX_LEN];
        uchar RC_size;
        uchar blockAddr; //Selection operation block address 0 to 63
        String mynum = "";
String kleur;        
String kr3Sw;
    rainbow(0,200,70,0);
    rainbow(1,200,70,0);

        str[1] = 0x4400;
 //Find tags, return tag type
 status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);
Serial.println (status);
 if (status == MI_OK)
 {
          Serial.println("RFID tag detected");
          Serial.print("Tag Type:\t\t");
          uint tagType = str[0] << 8;
          tagType = tagType + str[1];
          switch (tagType) {
            case 0x400:
              Serial.println("Mifare One (S50)");
              break;
            default:
              Serial.println("Unknown");
              break;
          }
 }

 //Anti-collision, return tag serial number 4 bytes
 status = myRFID.AddicoreRFID_Anticoll(str);
 if (status == MI_OK)
 {
          checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
          Serial.print("The tag's number is:\t");
        Serial.print(str[0]);
            Serial.print(" , ");
        Serial.print(str[1]);
            Serial.print(" , ");
        Serial.print(str[2]);
            Serial.print(" , "); 
        Serial.println(str[3]);
                   
          Serial.print("Read Checksum:\t\t");
          Serial.println(str[4]);
          Serial.print("Calculated Checksum:\t");
            Serial.println(checksum1);
 uint8_t wait;
            // Should really check all pairs, but for now we'll just use the first
            if(str[0]==20 && str[1]==200 && str[2]==201 && str[3] == 28)                      //20 , 200 , 201 , 28
            {
                teststring=opbouw + '5';
                if (coordinaat.startsWith(teststring)) {
                  opbouw=teststring;
                 kleur = "GR";
                    }
                else {
                  kleur ="RO";
                  }
 
            } else if(str[0]==100 && str[1]==253 && str[2]==209 && str[3] == 28)     {             //100 , 253 , 209 , 28
                teststring=opbouw + '1';
                if (coordinaat.startsWith(teststring)) {
                  opbouw=teststring;
                 kleur = "GR";
                    }
                else {
                  kleur ="RO";
                    }
            }
          // Serial.print(kleur);            
            Serial.println();
          //  delay(1000);
 }
            if ( kleur =="GR") {
              rainbow(0,0, 178, 0);
              rainbow(1,0, 178, 0);
              delay(2000);
              rainbow(0,200,70,0);
              rainbow(1,200,70,0);
               }
            if ( kleur =="RO"){
              rainbow(0,255,0,0);
              rainbow(1,255,0,0);
              delay(2000);
              } 
            // paars 203, 26, 180 naar 111-27-155 dat is dus -92 +1 -25
 
        myRFID.AddicoreRFID_Halt();   //Command tag into hibernation             
}

void rainbow(byte lednr, byte kl1, byte kl2, byte kl3) { // byte lednr was uint8_t wait
  uint16_t ib, jb;

      strip.setPixelColor(lednr, kl1, kl2, kl3); //ogen
       strip.setPixelColor(2, 174,0,255);    //} // achter het masker
    strip.show();
    delay(20); // 20 was wait
}

i have a hunch it could be that the pin connections as there isn't a scheme to find that connects a rfid-rc522 module (not the AN) to an Arduino micro ans then with an exaple code...

on that hunch i close this thread and start a new one with the pin problem.

pls close this thread