Scan a card and have the option to choose

I want to make an RFID scanner to scan the card first.
Then, display a prompt of doing action A or action B
If user input is A, shows A
if user input is B, requests for scanning another card.

Problem:
I have no idea how to code for a user input can someone show me an example or make one that can fit into my code?

#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"
||content.substring(1) "DB 93 46 E0") 
  {
    Serial.println("Select A or B");
// Request user input here
//if(==A) message A
//else continue to code below
    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);
  }
} 

See Serial input basics - updated

Take a look in this post.

Just a small note about that. I know it's how code examples found around read and handle card IDs, but I still wonder why converting the RFID code into hex "Strings" as it's something we should avoid on Arduino, instead of using "C strings" or "char pointers" where the card ID can be stored in hex (and IMHO no need of spaces).

Or, even better and simpler, a byte array like the one already stored by the library.
Just add the "allowed UIDs" array, change the loop, and add a couple of useful functions like this (I don't have the reader so I can't test the code, you should do it but IMHO in theory this a better approach):

#define N_ALLOWED 2
// Assuming fixed UID size to 4 bytes
#define UID_SIZE 4
// Allowed UID array
byte allowedID[N_ALLOWED][UID_SIZE] = { 
  {0x1A, 0x80, 0xFD, 0xDA},
  {0xDB, 0x93, 0x46, 0xE0} 
};
...
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 :");
  printID(mfrc522.uid);
  Serial.println();

  byte id = searchID(mfrc522.uid);
  Serial.print("Message : ");
  if (id != -1)
  { // ID found, do something..
...
  }
  else {
    // Invalid card, do something else...
...
  }
}

void printID(Uid uid) {
  char buf[3]; // buffer for hex byte serial output
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
    sprintf(buf, "%02X ", mfrc522.uid.uidByte[i]);
    Serial.print(buf);
  }
}

byte searchID(Uid uid) {
  // Search inside the allowed IDs array
  for(int i=0; i<N_ALLOWED; ++i) {
    // Compare the bytes
    bool found = true;
    for(int b=0; b<UID_SIZE) {
      if (allowedID[i][b] != uid.uidByte[b]) {
        // Not good, go to the next one
        found = false;
        break;
      }
    }
    if (found) {
      // Found! Return the index of the allowed ID
      return i;
    }
  }
  return -1; // Not found: index = -1
}

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