Hi
I'm using the following code to successfully read IDs from MIFARE cards to the serial monitor.
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int readflag;
byte readCard[4];
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
lcd.begin(20, 4);
}
///////////////////////////////////////////////////////////////////////////////
// Main loop
///////////////////////////////////////////////////////////////////////////////
void loop () {
do {
readflag = checkread();
}
while (!readflag);
//If card detected do this
recordid();
}
///////////////////////////////////////////////////////////////////////////////
// Stores the ID of the card that's been detected in readCard byte array
///////////////////////////////////////////////////////////////////////////////
void recordid() {
mfrc522.PICC_IsNewCardPresent();
mfrc522.PICC_ReadCardSerial();
lcd.clear();
for (int i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print( mfrc522.uid.uidByte[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA();
}
/////////////////////////////////////////////
// Returns 1 if a card has been detected
/////////////////////////////////////////////
int checkread() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0; } //no card detected
mfrc522.PICC_HaltA();
return 1; } //card detected
I have 12 cards and the serial monitor is telling me that their ID's are (hex):
3278CE3F
F5C9FD29
2FC640
82BA7A3F
2BD7A3F
52B77A3F
B2E5640
F2DD640
E2ECCC3F
22B3640
2FD640
73D5B7AC
I understand these are being stored in the readCard byte array.
I would like to compare the current value of this byte array to a known value to determine whether an instruction is run (e.g. a blink of an LED)
Something like this:
If (readCard = 2FD640) {
turn on LED
}
I have been able to do this successfully using this if statement:
if ( (uint32_t)readCard == 0x3FCE7832)
for example card one. However, it will not work with the cards with IDs that are not 8 digits long i.e. card 11 (2FD640).
Can anyone help me implement this in code?
Many thanks.