0
votes

I'm still learning the ropes so forgive me if the answer to this is obvious. I'm using the Arduino Uno and a RC522 RFID card reader. My aim is to scan a card on the reader and have the I2C OLED display show me the name of the person who has scanned their card. This is the code for the program I'm using -

#include <U8glib.h>
#include <LiquidCrystal.h>
#include <RFID.h>
#include <SPI.h>
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN, RST_PIN); 
int serNum[5];
String cardno;
int interval = 15000; // millisec 
long now = 0;
long lasttime = millis();

// change Reader ID to your name 
String readerID = "CCE3050";

void setup() { 
  Serial.begin(9600); 
  SPI.begin(); 
  rfid.init();
}

void loop() {
  now = millis();
  if (now > lasttime + interval) {
    lasttime = now; 
    Serial.print(readerID); 
    Serial.print(":"); 
    Serial.println("I am alive");
  }

  if (rfid.isCard()) {
    if (rfid.readCardSerial()) { 
      lasttime = now;

      cardno = String(rfid.serNum[0]) +
               String(rfid.serNum[1]) +
               String(rfid.serNum[2]) + 
               String(rfid.serNum[3]) + 
               String(rfid.serNum[4]);
      
      Serial.print(readerID); 
      Serial.print(":"); 
      Serial.println(cardno);
    } 
  }

  delay(5000); 
}

So far, each time a card is tagged, the card number is displayed on the serial monitor. However, I would like to assign a name to the card and display that name on the OLED whenever the card is tagged onto the reader. Is there a way to do this?

1

1 Answers

0
votes

I suggest creating a struct to hold an id and its associated name.

typedef struct { 
  String id; 
  String name; 
} User;

Create an array to hold multiple instances of the struct.

User users[MAX_USER_NUM];    // define the size

User user1 = {"123", "A"};   // {"id", "name"}
users[0] = user1;

User user2 = {"234", "B"};
users[1] = user2;

User user3 = {"345", "C"};
users[2] = user3;

You can then retrieve a user name by passing an id to a function like this.

String lookup(String id) {
  for (int i = 0; i < MAX_USER_NUM; i++) {
    if (users[i].id == id) {
      return users[i].name;
    }
  }
  return "";
}