// Adapted from ELEGOO's MFRC522 example, originally public-domain Tom Clement code.
#include <Arduino.h>
#include <SPI.h>      // Mega hardware SPI: MOSI=51, MISO=50, SCK=52.
#include <MFRC522.h>  // Talks to the 13.56 MHz reader; this is not secure authentication.
const uint8_t SS_PIN = 53;   // Reader SDA means SPI chip select here, not I2C data.
const uint8_t RST_PIN = 5;
MFRC522 reader(SS_PIN, RST_PIN);  // Constructor order is select, reset.
void printUid() {
  Serial.print(F("Card UID:"));
  // A UID can have more than four bytes; print every byte reported by the library.
  for (uint8_t i = 0; i < reader.uid.size; ++i) {
    Serial.print(reader.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(reader.uid.uidByte[i], HEX);  // HEX displays a byte as hexadecimal.
  }
  Serial.println();
}
void setup() {
  Serial.begin(9600);
  SPI.begin();
  reader.PCD_Init();
  Serial.println(F("Read UID only; UID is not authentication"));
}

void loop() {
  // || means OR. Return if no new tag is present or selecting it failed.
  if (!reader.PICC_IsNewCardPresent() || !reader.PICC_ReadCardSerial()) {
    delay(50); return;
  }
  printUid();
  reader.PICC_HaltA();  // Finish with this tag; remove/re-present it to read again.
  reader.PCD_StopCrypto1();
}
