I can't get what's going on in my code - MFRC522 library -> Arduino core error

Hello, I wanted to save UID of MIFARE Classic chip and compare if the next scan will be the same one, so I need to edit library and there I need to express variable from for loop.

This is piece of code from MFRC522, which one is edited (library)

void MFRC522::VariableUID(Uid *uid) {
	String PEX;
	char p[4];
	//String thisString = String(13, HEX);
	
	for (byte i = 0; i < uid->size; i++) {
		if(uid->uidByte[i] < 0x10)
			Serial.print(F(" 0"));
		else
			Serial.print(F(" "));
		p[i] = (uid->uidByte[i], HEX);
	}
	PEX = (p[0], HEX) + (p[1],HEX) + (p[2],HEX) + (p[3],HEX);
	/*Serial.println(p[0], HEX);
	Serial.println(p[1], HEX);
	Serial.println(p[2], HEX);
	Serial.println(p[3], HEX);*/
	Serial.println(PEX, HEX);

It gives me just error with Arduino itself.
Output of Arduino

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp: In member function 'void MFRC522::VariableUID(MFRC522::Uid*)':

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1417:25: error: no matching function for call to 'HardwareSerial::println(String&, int)'

  Serial.println(PEX, HEX);

                         ^

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1417:25: note: candidates are:

In file included from C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Stream.h:26:0,

                 from C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/HardwareSerial.h:29,

                 from C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:232,

                 from C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:7:

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:77:12: note: size_t Print::println(const __FlashStringHelper*)

     size_t println(const __FlashStringHelper *);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:77:12: note:   candidate expects 1 argument, 2 provided

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:78:12: note: size_t Print::println(const String&)

     size_t println(const String &s);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:78:12: note:   candidate expects 1 argument, 2 provided

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:79:12: note: size_t Print::println(const char*)

     size_t println(const char[]);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:79:12: note:   candidate expects 1 argument, 2 provided

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:80:12: note: size_t Print::println(char)

     size_t println(char);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:80:12: note:   candidate expects 1 argument, 2 provided

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:81:12: note: size_t Print::println(unsigned char, int)

     size_t println(unsigned char, int = DEC);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:81:12: note:   no known conversion for argument 1 from 'String' to 'unsigned char'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:82:12: note: size_t Print::println(int, int)

     size_t println(int, int = DEC);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:82:12: note:   no known conversion for argument 1 from 'String' to 'int'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:83:12: note: size_t Print::println(unsigned int, int)

     size_t println(unsigned int, int = DEC);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:83:12: note:   no known conversion for argument 1 from 'String' to 'unsigned int'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:84:12: note: size_t Print::println(long int, int)

     size_t println(long, int = DEC);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:84:12: note:   no known conversion for argument 1 from 'String' to 'long int'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:85:12: note: size_t Print::println(long unsigned int, int)

     size_t println(unsigned long, int = DEC);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:85:12: note:   no known conversion for argument 1 from 'String' to 'long unsigned int'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:86:12: note: size_t Print::println(double, int)

     size_t println(double, int = 2);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:86:12: note:   no known conversion for argument 1 from 'String' to 'double'

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:87:12: note: size_t Print::println(const Printable&)

     size_t println(const Printable&);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:87:12: note:   candidate expects 1 argument, 2 provided

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:88:12: note: size_t Print::println()

     size_t println(void);

            ^

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Print.h:88:12: note:   candidate expects 0 arguments, 2 provided

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp: In member function 'bool MFRC522::MIFARE_SetUid(byte*, byte, bool)':

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1852:39: warning: 'bool MFRC522::MIFARE_OpenUidBackdoor(bool)' is deprecated (declared at C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1708): will move to extra class in next version [-Wdeprecated-declarations]

  if (!MIFARE_OpenUidBackdoor(logErrors)) {

                                       ^

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp: In member function 'bool MFRC522::MIFARE_UnbrickUidSector(bool)':

C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1881:34: warning: 'bool MFRC522::MIFARE_OpenUidBackdoor(bool)' is deprecated (declared at C:\Users\UserName\Documents\Arduino\libraries\MFRC522\src\MFRC522.cpp:1708): will move to extra class in next version [-Wdeprecated-declarations]

  MIFARE_OpenUidBackdoor(logErrors);

                                  ^

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Can anyone tell me, what’s wrong with my edited piece of code?

It is telling you there is no overload of a String print that takes an extra integer argument. What are you trying to do?

Yeah, Arduino core have crashed.
I need to get first card into one variable… and the next scans would be into another… that means… I could compare these two and when the compare would be exact, then it will turn LED on.

In Arduino project I call VariableUID.
I’ve made some changes, so piece of library see like this.

void MFRC522::VariableUID(Uid *uid) {
	long* PEX; 
	PEX = (long *) uid->uidByte; //long only for 4 Bytes
	byte* temp;
	temp = (byte *) PEX;
	for(byte i=0; i < 4; i++) {
	Serial.print(*(temp++), HEX);
	Serial.print(" "); 
	} 
}

Then output is quie far from real byte value ( That’s not problem), but how can I get variables from libraries? Exactly

B5D0C64B 0 A080000 9 Card UID: 4B C6 D0 B5

Thanks

Yeah, Arduino core have crashed.

What does that mean? How do you know?

I guess... but that's not the most important right now (I've got there wrong variable types). How can I get First scan to one variable, and next ones to 2nd variable.. which change every scan? Then I need to compare them by if statement, so it would be just comparing var1 with var 2. Anyway, I can't get 4 bytes to project variable(s) -- it can be array.

No experience with card readers but you should not have a need to modify the library.

My interpretation of the ReadAndWrite example of the MFRC522 library is that the uid is stored in mfrc522.uid.uidByte and mfrc522.uid.size indicates how many bytes it is.

In your sketch, you can use those to copy the uid to a new variable (use memcpy).

So you can have something like below; just to give you the idea (might have bugs).

// variable to store a uid
// no idea about the size; 16 sounds safe ;-)
byte oldUid[16];

void setup()
{
  // put your setup code here, to run once:

}

void loop()
{
  // read card
  ...
  ...

  if (mfrc522.uid.size > sizeof(oldUid))
  {
    Serial.println("OOPS, does not fit")
  }
  else
  {
    // copy to oldUid variable
    memcpy(oldUid, mfrc522.uid.uidByte, mfrc522.uid.size);
  }
}

so I need to edit library

Why ?

UKHeliBob:
Why ?

I’ve been wrong, and wanted to edit library to get UID into array. But there’s another way.

How arduino works? I wanted to give there if statement with null…

/*
 * Pin layout should be as follows:
 * Signal     Pin
 *            Arduino Uno
 * ----------------------
 * Reset      9
 * SPI SS     10
 * SPI MOSI   11
 * SPI MISO   12
 * SPI SCK    13
 */
#include <SPI.h>
#include <MFRC522.h>

byte oldUid[16];
#define SS_PIN 10
#define RST_PIN 9
MFRC522 MFRC522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus
    MFRC522.PCD_Init(); // Init MFRC522 card
    Serial.println("Scan PICC to see UID and type...");
}

void loop() {
    // Look for new cards
    if ( ! MFRC522.PICC_IsNewCardPresent()) {
      if (oldUid[0] == null && oldUid[2] == null){
        for (byte i = 0; i < 4; i++) {
          oldUid[i] = MFRC522.uid.uidByte[i]; //Make first card onto major place
        }
        return;
        }
        else
        {
          return; //Let it be, continue to get minor card scan, if there's major.
        }        
        return;
    }
    // Select one of the cards
    if ( ! MFRC522.PICC_ReadCardSerial()) {
      Serial.println(" "); 
        return;
    }
   if (MFRC522.uid.size > sizeof(oldUid))
  {
    Serial.println("This chip doesn't fit"); //Minor chip doesn't fit with major one
  }
  else
  {
    // copy to oldUid variable
    memcpy(oldUid, MFRC522.uid.uidByte, MFRC522.uid.size);
  }
}

Is this a way to get it work? Just need help with the null and memcpy (I have no idea how should it work)

How works arduino?

It is programmed in C++

It's time for you to have a word with Mr Google I think

Got it. I used to program in C#.

/*
 * Pin layout should be as follows:
 * Signal     Pin
 *            Arduino Uno
 * ----------------------
 * Reset      9
 * SPI SS     10
 * SPI MOSI   11
 * SPI MISO   12
 * SPI SCK    13
 */
#include <SPI.h>
#include <MFRC522.h>

byte oldUid[16];
#define SS_PIN 10
#define RST_PIN 9
byte Nled = 7;
MFRC522 MFRC522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus
    MFRC522.PCD_Init(); // Init MFRC522 card
    Serial.println("Scan PICC to see UID and type...");
    oldUid[0] = NULL;
    oldUid[2] = NULL;
    pinMode(Nled, OUTPUT);
}
void loop() {
    // Look for new cards
    if ( ! MFRC522.PICC_IsNewCardPresent()) {
      if (oldUid[0] == NULL && oldUid[2] == NULL){
        for (byte i = 0; i < 4; i++) {
          oldUid[i] = MFRC522.uid.uidByte[i]; //Make first card onto major place - UNTIL ARDUINO WILL BE TURNED OFF (Am I right?)
        }
        return;
        }
        else
        {          
          if (MFRC522.uid.size > sizeof(oldUid)){
            Serial.println("This chip doesn't fit"); //Minor chip doesn't fit with major one
          }
          else{
            // copy to oldUid variable
            //memcpy(oldUid, MFRC522.uid.uidByte, MFRC522.uid.size); //So this will copy scanned card to major? 
            digitalWrite(Nled, HIGH);
            delay(50);
            digitalWrite(Nled, LOW);
            delay(50);
            digitalWrite(Nled, HIGH);
            delay(50);
            digitalWrite(Nled, LOW);
            delay(50);
            
          }
          return; //Let it be, continue to get minor card scan, if there's major.
        }        
        return;
    }
    
    // Select one of the cards
    if ( ! MFRC522.PICC_ReadCardSerial()) {
      Serial.println(" "); 
        return;
    }
}

I am stucked right here… because when I scan at first time the chip, it start blinking (That’s correct), but it wouldn’t stop… never… Even when I try to scan another chip… Still blnking.
Any ideas?

One more question… what mean sizeof array? I’ve got 3 chips, which are 4 bytes for each chip… so I need to compare their uid, not size. I guess?
Thanks

      if (MFRC522.uid.size > sizeof(oldUid))

Have you tried printing sizeof(oldUid)? What do you expect it to be ?

I expect.. that I can compare them.. But it's 16 everytime.. On EVERY chip I scan.. that's wrong.. isn't it? I need to get differences of uid's. I expected something like comparing UIDs converted to DEC or HEX into a array.

But it's 16 everytime

Considering that the array is declared as 16 bytes why would that be a surprise ?

You could, of course, compare each of the bytes of the two arrays