Getting the RFID-RC522 to work! [SOLVED]

The first thing I’d (always) do is to get rid of all that String manipulation stuff and use C strings (char[])

Instead of

    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }

I’d go for something like

  char cardID[32] = "";

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    snprintf(cardID, sizeof(cardID), "%s%02x", cardID, mfrc522.uid.uidByte[i]);
  }
// or
  char cardID[32] = "";

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    char hex[4];
    snprintf(hex, sizeof(hex), "%02x", mfrc522.uid.uidByte[i]);
    strncat(cardID, hex, sizeof(cardID));
  }
3 Likes