Trying to scan 2 or 1 card to allow access and any other card denies access

Is there anyway to make the code less complicated?

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Please present card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "1A 80 FD DA"||"DB 93 46 E0") 
  {
    Serial.println("Please scan other card for access");
    delay(3000);
     if ( ! mfrc522.PICC_IsNewCardPresent()) 
     {
      Serial.println("Try again");
      return;
     }
     if ( ! mfrc522.PICC_ReadCardSerial())
     {
      return;
     }
      Serial.print("UID tag :");
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++) 
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase();
      if (content.substring(1) == "DB 93 46 E0")
      {
        Serial.println("Access granted");
        delay(3000);
      }
      else
      {
        Serial.println(" Access denied");
        delay(3000);
      }
    Serial.println();
    delay(3000);
  }
} 

Welcome

Yes, don't use String :slight_smile:

The card id is a 4-bytes number, so if you combine those bytes, then you can easily compare the card id to a number

And this is wrong

if (content.substring(1) == "1A 80 FD DA"||"DB 93 46 E0")

You can't do multiple comparisons like that

C++ isn't that close to English. You have to write this as something like:

  if (content.substring(1) == "1A 80 FD DA"
    || content.substring(1) == "DB 93 46 E0") 
  {

Thanks for the welcome
I am new to coding and do not know much.
Do you have a suggestion for not using string?
Could you provide an example code?

Post your new, changed, code. Without it, we can't help you more.

Whoops sorry for the confusion, I simply forgot to compile and upload my new code.
Everything is working as it seems now. Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.