2

I am trying the RFID RC522 reader kit with an Arduino UNO to built a controlled access system for a door.

Using the code below, I am able to read the UID of my card and light up an LED when the reader detects the card or the badge, however it will light up whatever card is presented to it even if the UID of this card is not entered as a String in my program.

My question is how can I enter hundreds of cards (depending on memory) that will unlock the door and have all the others set as unknown if not in my program.

Thank you very much for your help

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         //(or SDA) Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
// ===========================================================================
String read_rfid;
String ok_rfid_1 = "C5 12 E3 52"; // for the badge
String ok_rfid_2 = "9A 48 3A D5"; // for the card
// ==========================================================================
int led = 3;
// ===========================================================================
void setup() {
 Serial.begin(9600); // Initialize serial communications with the PC
 //while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 SPI.begin(); // Init SPI bus
 mfrc522.PCD_Init(); // Init MFRC522
 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
 Serial.println(F("Scan PICC to see UID, SAK, type, and data bleds..."));
  pinMode(led, OUTPUT);
}
// ===========================================================================
 void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid="";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
 }
// ==========================================================================
void unlock() {
  digitalWrite(led, HIGH);
  delay(2000);
  digitalWrite(led, LOW);
}
// ======================================================================
void loop() {
 // Look for new cards
 if (! mfrc522.PICC_IsNewCardPresent())
 return;
 // Select one of the cards
 if (! mfrc522.PICC_ReadCardSerial())
 return;

  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);

  if (read_rfid == ok_rfid_1 || ok_rfid_2) {
    Serial.println("Open");
    unlock(); // turns LED on
  }

  else {
    Serial.println("Stay Closed");
  }
}

1 Answer 1

1

Your IF statement is the problem. if (read_rfid == ok_rfid_1 || ok_rfid_2) always evaluates to True. Your condition should be:

if (read_rfid == ok_rfid_1 || read_rfid == ok_rfid_2)

You also need to remove the spaces in the string literals if you want this condition to have the desired effect.

This approach isn't very good though, since you have to add more comparisons to the IF condition if you want to match more cards. You should use the byte arrays as they are, without converting to Strings; it can actually be a lot simpler to compare as well as memory-safe. Store all your allowed card UIDs in a 2-D array:

#define ALLOWED 2   // no of allowed cards
byte read_card[4];   // same purpose as before
bool found = false;  // to know when a valid card match occurs
byte cards[ALLOWED][4] = {{0xC5, 0x12, 0xE3, 0x52}, 
                         {0x9A, 0x48, 0x3A, 0xD5}};

Then you should get rid of dump_byte_array() and compare cards like this:

memcpy(read_card, mfrc522.uid.uidByte, 4); // copy UID into array with shorter name ;)

for (int i = 0; i < ALLOWED; i++){
  if (memcmp(read_card, cards[i], 4) == 0){

      found = true;
      break;
  }
}

if (found){
  Serial.println("Open");
  unlock(); // turns LED on
  found = false;
}
else {
  Serial.println("Stay Closed");
}
1
  • Thanks TisteAndii, I will give it a go, byte arrays is defenitely what I should use. cheers.
    – Octopus8
    Jul 8, 2016 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.