I'm working on a small project for a presentation. My goal is to read the UID of multiple Mifare Classic cards and compare it to a hardcoded list for different actions. All is well (the reading and comparing part that is) but 2 small problems that I have encountered are:
The first (maybe up to three, since the reader is a little bit on the fast side) read after uploading my code will return a UID of just 0s and a wrong PICC type.
After reading a PICC and then trying to read another, the reader prints the UID / PICC Type of the reader before, which is kinda not helpful when trying to light LEDs based on UID.
Here is my sketch:
/*
HEADER TEXT
GOES HERE
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
// Serielles Interface initialisieren
Serial.begin(9600);
// SPI initialisieren
SPI.begin();
mfrc522.PCD_Init();
// Für später
// pinMode (2, OUTPUT);
}
void loop()
{
// Eigene UID bilden. Dazu nehmen wir die 4(7)Byte Hex UID aus Block 0 und quetschen sie in einen schönen String.
String uID = String(mfrc522.uid.uidByte[0],HEX)+String(mfrc522.uid.uidByte[1],HEX)+String(mfrc522.uid.uidByte[2],HEX)+String(mfrc522.uid.uidByte[3],HEX);
// PICC Typen
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Nach neuen Karten suchen.
delay(250);
if ( mfrc522.PICC_IsNewCardPresent())
{
delay(250);
mfrc522.PICC_ReadCardSerial();
// Debug Ausgabe
Serial.println();
Serial.print("Die Kartennummer lautet: ");
Serial.print(uID);
Serial.println();
Serial.print("Picc Type: ");
Serial.println(mfrc522.PICC_GetTypeName(piccType));
Serial.println();
// Überprüfe auf unterstützten PICC Typ und noch mehr Debug Kram
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K&& piccType != MFRC522::PICC_TYPE_MIFARE_1K) {
Serial.println("Diese Tech-Demo benötigt MIFARE Classic Karten.");
return;
}
// Den UID String Verglichen und eine entsprechende Aktion ausführen
if(uID=="myUIDforStuff") {
Serial.println("Access granted");
}
}
}