I recently bought an MFRC522 RFID module to use with my Arduino Uno, which came as a kit with a couple of MiFare 1K cards.
The issue I've been having is that, I cannot scan cards consecutively (the same card or two different ones) without resetting the Sketch. I hold a card in front of the reader and it is scanned, but when I hold another card in front of the reader it just doesn't recognize it as being there in the mfrc522.PICC_IsNewCardPresent()
function. It does however work when using the example sketch "DumpInfo".
Here is the code I've written (please excuse any of my bad practices/untidiness).
/* Include Required Libraries */
#include <MFRC522.h>
#include <SPI.h>
/* Define Values for MFRC522 */
#define RST_PIN 9
#define SS_PIN 10
/* Instantiate the MFRC522 Object */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Initialize Variables */
byte resultBuffer[18];
MFRC522::MIFARE_Key key;
unsigned int integerValue = 0;
char incomingByte;
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("Ready to scan card..."));
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
}
void loop()
{
if (!mfrc522.PICC_IsNewCardPresent())
{
if (Serial.available() <= 0)
{
return;
}
integerValue = 0;
while(true)
{
incomingByte = Serial.read();
if (incomingByte == '\n') break;
if (incomingByte == -1) continue;
integerValue *= 10;
integerValue = ((incomingByte - 48) + integerValue);
}
printHeader();
Serial.println("NEW BLOCK READ VALUE :");
Serial.println(integerValue);
}
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
readAndPrint(integerValue);
}
void printHeader()
{
Serial.println(" ");
Serial.println("----------------------------------------");
}
int authenticateKeyA(int blockNumber)
{
printHeader();
Serial.println("Authenticating with key A...");
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3;
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.println("ERROR : Authentication was unsuccessful.");
return 0;
}
else
{
Serial.println("SUCCESS : Authentication was successful.");
return 1;
}
}
int authenticateKeyB(int blockNumber)
{
printHeader();
Serial.println("Authenticating with key B...");
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3;
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.println("ERROR : Authentication was unsuccessful.");
return 0;
}
else
{
Serial.println("SUCCESS : Authentication was successful.");
return 1;
}
}
int readBlock(int blockNumber, byte *resultBuffer)
{
printHeader();
Serial.println("Starting read block function...");
int authSuccess = authenticateKeyA(blockNumber);
Serial.println(" ");
if (!authSuccess)
{
Serial.println("ERROR : Terminating function.");
return 0;
}
byte bufferSize = 18;
byte status = mfrc522.MIFARE_Read(blockNumber, resultBuffer, &bufferSize);
if (status != MFRC522::STATUS_OK)
{
Serial.println("ERROR : Read failed.");
return 0;
}
else
{
Serial.print("SUCCESS : Block ");
Serial.print(blockNumber);
Serial.print(" was read successfully.");
return 1;
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
void readAndPrint(int blockNumber)
{
readBlock(blockNumber, resultBuffer);
printHeader();
Serial.println("RESULT :");
for (int j=0 ; j<16 ; j++)
{
Serial.print(resultBuffer[j], HEX);
Serial.print(" ");
}
Serial.println(" ");
delay(2000);
printHeader();
Serial.println("Ready to scan card...");
}
Before scanning the card for the first time, the serial output looks like this :
After scanning the card for the first time, the serial output looks like this :
After scanning the card for the second time, the serial output doesn't change from image 2. I tried running Serial.println("no card detected");
when the code inside if (!mfrc522.PICC_IsNewCardPresent())
is ran, just to check if the program was frozen (I'd heard someone else had an issue where the program froze after scanning the first card) - it wasn't frozen, both before and after scanning the first card, it printed out 'no card detected' repeatedly.
I would greatly appreciate any help as I have no clue as to how I should proceed.
Cheers,