Try to tire an rfid with a name .please help

Hi, am working on a time attendance rfid system and i what to identify is card with a name and display it in the serial monitor when that particular card is swiped.
This is the example code am working on… i want to add 5cards each with a name that will be stord in the sd…

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include <Wire.h>
#include "RTClib.h" // for the RTC

// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define select pin for SD card module
#define CS_SD 4 

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Creates an LC object. Parameters 

// Create a file to store the data
File myFile;

//Variable to hold the tag's UID
String uidString;

// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID);

// Instance of the class for RTC
RTC_DS1307 rtc;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Pins for LEDs and buzzer
const int redLED = 6;
const int greenLED = 7;
const int buzzer = 5;

void setup() {
// Set LEDs and buzzer as outputs
 pinMode(redLED, OUTPUT);  
 pinMode(greenLED, OUTPUT);
 pinMode(buzzer, OUTPUT);
 
// Init Serial port
 Wire.begin();
 Serial.begin(9600);
 while(!Serial); // for Leonardo/Micro/Zero
  //LCD Initializations
 lcd.begin(16, 2);
  // Initiate  SPI bus
   SPI.begin(); 
  // Initiate MFRC522
  rfid.PCD_Init();

 // Setup for the SD card
 Serial.print("Initializing SD card...");
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("SD Check");
 if(!SD.begin(CS_SD)) {
   Serial.println("initialization failed!");
   lcd.setCursor(0,1);
   lcd.print("SD Not Found");
   return;
 }
 Serial.println("initialization done.");
     lcd.setCursor(0,1);
   lcd.print("SD Found");
   delay(200);
   lcdprintmain();

 // Setup for the RTC  
 if(!rtc.begin()) {
   Serial.println("Couldn't find RTC");
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("RTC no Found");
   delay(2000);
   lcdprintmain();
   while(1);
   
 }
 else {
   // following line sets the RTC to the date & time this sketch was compiled
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
 }
 if(!rtc.isrunning()) {
   Serial.println("RTC is NOT running!");
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("RTC Running");
   delay(2000);
   lcdprintmain();
 }
}

void loop() {
 //look for new cards
 if(rfid.PICC_IsNewCardPresent()) {
   readRFID();
   logCard();
 }
 delay(10);
}

void readRFID() {
 rfid.PICC_ReadCardSerial();
 Serial.print("Tag UID: ");
 uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
   String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
 Serial.println(uidString);
     lcd.clear();
   lcd.setCursor(0,0);
   lcd.print(uidString);
  if (uidString == "118 211 198 31") //change here the UID of the card/cards that you want to give access
 {
   Serial.println("Authorized access");
   Serial.println();
   lcd.setCursor(0,1);
   lcd.print("Authorized access");
// Sound the buzzer/greenLed on when a card is read
 tone(buzzer, 1000);
 digitalWrite(greenLED,HIGH); 
 delay(100);        
 noTone(buzzer);
 digitalWrite(greenLED,LOW);
 
 
 delay(3000);
 lcdprintmain();
 }
  else   {
   Serial.println(" Access denied");
   lcd.setCursor(0,1);
   lcd.print("Access denied");
   tone(buzzer, 3000);
   digitalWrite(redLED,HIGH); 
   delay(100);        
   noTone(buzzer);
   digitalWrite(redLED,LOW);
   delay(3000);
   lcdprintmain();
 }
}


void logCard() {
 // Enables SD card chip select pin
 digitalWrite(CS_SD,LOW);
 
 // Open file
 myFile=SD.open("DATA.txt", FILE_WRITE);

 // If the file opened ok, write to it
 if (myFile) {
   Serial.println("File opened ok");
   myFile.print(uidString);
   myFile.print(", ");   
   
   // Save time on SD card
   DateTime now = rtc.now();
   myFile.print(now.year(), DEC);
   myFile.print('/');
   myFile.print(now.month(), DEC);
   myFile.print('/');
   myFile.print(now.day(), DEC);
   myFile.print(',');
   myFile.print(now.hour(), DEC);
   myFile.print(':');
   myFile.println(now.minute(), DEC);
   
   // Print time on Serial monitor
   Serial.print(now.year(), DEC);
   Serial.print('/');
   Serial.print(now.month(), DEC);
   Serial.print('/');
   Serial.print(now.day(), DEC);
   Serial.print(' ');
   Serial.print(now.hour(), DEC);
   Serial.print(':');
   Serial.println(now.minute(), DEC);
   Serial.println("sucessfully written on SD card");
   myFile.close();

   // Save check in time;
   userCheckInHour = now.hour();
   userCheckInMinute = now.minute();
 }
 else {
   Serial.println("error opening data.txt");  
 }
 // Disables SD card chip select pin  
 digitalWrite(CS_SD,HIGH);
}





void lcdprintmain(){
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.write("IMpact Device");
 lcd.setCursor(0,1);
 lcd.write(" Scan Your Card");
}

void lcdaccessdenied(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write(" Access Denied  "); // print Access denied
}

void lcdprintwelcome(String Name){
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("  Welcome Home  ");
   lcd.setCursor(0,1);
   lcd.print(Name);
}

Please read the forum guide in the sticky post to find out how to use this forum correctly, including how to post code correctly. Then modify your post above and correct it.

This is the part that recognizes the ID:

  if (uidString == "118 211 198 31") //change here the UID of the card/cards that you want to give access
 {
   Serial.println("Authorized access");
   Serial.println();
   lcd.setCursor(0,1);
   lcd.print("Authorized access");
// Sound the buzzer/greenLed on when a card is read
 tone(buzzer, 1000);
 digitalWrite(greenLED,HIGH); 
 delay(100);        
 noTone(buzzer);
 digitalWrite(greenLED,LOW);
 
 delay(3000);
 lcdprintmain();
 }

You can add a section to recognize multiple ID's and assigne each a name:

  boolean authorized = false;
  char *name = "";
   if (uidString == "118 211 198 31") //change here the UID of the card/cards that you want to give access
 {
   authorized = true;
   name = "John"; 
  }

   if (uidString == "118 211 198 32") //change here the UID of the card/cards that you want to give access
 {
   authorized = true;
   name = "Paul"; 
  }

   if (uidString == "118 211 198 33") //change here the UID of the card/cards that you want to give access
 {
   authorized = true;
   name = "George"; 
  }

Then use the "authorized" flag in place of the original comparison:

  if (authorized)
 {
   Serial.print("Authorized access by ");
   Serial.println(name);
   Serial.println();
   lcd.setCursor(0,1);
   lcd.print("Authorized: ");
   lcd.print(name);
// Sound the buzzer/greenLed on when a card is read

@Mospact thanks for fixing your post.

Do you want to store the rfid codes and the names on the sd card, so you can update the sd card with more names and ids? Otherwise, for only 5 names, it seems pointless to use the sd card at all.

PaulRB:
@Mospact thanks for fixing your post.

Do you want to store the rfid codes and the names on the sd card, so you can update the sd card with more names and ids? Otherwise, for only 5 names, it seems pointless to use the sd card at all.

yes i want to store the rfid code, name and time when ever the card is swiped.

johnwasser:
This is the part that recognizes the ID:

  if (uidString == "118 211 198 31") //change here the UID of the card/cards that you want to give access

{
  Serial.println("Authorized access");
  Serial.println();
  lcd.setCursor(0,1);
  lcd.print("Authorized access");
// Sound the buzzer/greenLed on when a card is read
tone(buzzer, 1000);
digitalWrite(greenLED,HIGH);
delay(100);        
noTone(buzzer);
digitalWrite(greenLED,LOW);

delay(3000);
lcdprintmain();
}




You can add a section to recognize multiple ID's and assigne each a name:


boolean authorized = false;
 char *name = "";
  if (uidString == "118 211 198 31") //change here the UID of the card/cards that you want to give access
{
  authorized = true;
  name = "John";
 }

if (uidString == "118 211 198 32") //change here the UID of the card/cards that you want to give access
{
  authorized = true;
  name = "Paul";
 }

if (uidString == "118 211 198 33") //change here the UID of the card/cards that you want to give access
{
  authorized = true;
  name = "George";
 }




Then use the "authorized" flag in place of the original comparison:


if (authorized)
{
  Serial.print("Authorized access by ");
  Serial.println(name);
  Serial.println();
  lcd.setCursor(0,1);
  lcd.print("Authorized: ");
  lcd.print(name);
// Sound the buzzer/greenLed on when a card is read

thanks @Brattain Member.

Before i saw you replay i was trying to store the rfid and name in this format but fog stuck with the code is here is the code

char* allowedTags[] = {
  "1234y688678",         // Tag 1
  "2900940E95",         // Tag 2
  "ABC123DE45",         // Tag 3
};

// List of names to associate with the matching tag IDs
char* tagName[] = {
  "john",      // Tag 1
  "Mark ",      // Tag 2
  "james",       // Tag 3
};

Mospact:
yes i want to store the rfid code, name and time when ever the card is swiped.

That is a separate question. My question was do you want to store the list of IDs and names of authorized persons on the SD card? So if you want to authorise a new person, you can update a file on the SD card instead of updating the Arduino sketch?

PaulRB:
That is a separate question. My question was do you want to store the list of IDs and names of authorized persons on the SD card? So if you want to authorise a new person, you can update a file on the SD card instead of updating the Arduino sketch?

update from the arduino sketch

Mospact:
update from the arduino sketch

Sorry, that does not make any sense. What do you mean?

Mospact:
Before i saw you replay i was trying to store the rfid and name in this format but fog stuck with the code is here is the code

char* allowedTags[] = {

“1234y688678”,         // Tag 1
 “2900940E95”,         // Tag 2
 “ABC123DE45”,         // Tag 3
};

// List of names to associate with the matching tag IDs
char* tagName = {
 “john”,      // Tag 1
 "Mark ",      // Tag 2
 “james”,       // Tag 3
};

That would work even better.

 boolean authorized = false;
  char *name = "";

  for (int i=0; i < TagCount; i++)
  {
     if (uidString == allowedTags[i])
     {
       authorized = true;
       name = tagName[i]; 
     }
  }

PaulRB:
Sorry, that does not make any sense. What do you mean?

What i mean is that i want to store the activities of the card owner in the Sd. If there is need to add a new user i will have to include Card id and name in the arduino code

Mospact:
If there is need to add a new user i will have to include Card id and name in the arduino code

Ok, if that is what you want. But you do not have to do it that way.

PaulRB:
Ok, if that is what you want. But you do not have to do it that way.

Can you please hint me on the appropriate way to do it. Thanks

Doing it your way is not inappropriate. But it has the disadvantage that if you wish to add to or remove from the list of persons with authorised access, you will need to change the sketch and re-upload it to the Arduino. But if you store that information in a small file on the SD card, then you can put the SD card into a pc and update it using Excel or whatever.

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


#define CS_RFID 10
#define RST_RFID 9

#define CS_SD 4 
#define RELAY 3

File myFile;


MFRC522 rfid(CS_RFID, RST_RFID); 


RTC_DS1307 rtc;





const int redLED = 6;
const int greenLED = 5;


void setup() {
    digitalWrite(RELAY, HIGH);
 
  pinMode(redLED, OUTPUT);  
  pinMode(greenLED, OUTPUT);

   pinMode(RELAY, OUTPUT);

  Serial.begin(9600);
  while(!Serial); 
  

  SPI.begin(); 
 
  rfid.PCD_Init(); 


  Serial.print("SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("done.");


  if(!rtc.begin()) {
    Serial.println("No RTC");
    while(1);
  }
  else {
    
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if(!rtc.isrunning()) {
    Serial.println("NO RTC");
  }
}

void loop() {

  if(rfid.PICC_IsNewCardPresent()) 
  {
    readRFID();
 
  }
  delay(40);
}

void readRFID() {
   
  rfid.PICC_ReadCardSerial();
  Serial.print("Tag UID: ");
  String content= "";
  for (byte i = 0; i < rfid.uid.size; i++) 
    {
     Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(rfid.uid.uidByte[i], HEX);
     content.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(rfid.uid.uidByte[i], HEX));
  }
  Serial.println();

  content.toUpperCase();
   boolean authorized = false;
  char *name = "";


     if (content.substring(1) == "11 76 04 A3")  
  {
   authorized = true;
   name = "Jim E. Espinola"; 
  }

     if (content.substring(1) == "12 5C 1B 1C")  
  {
   authorized = true;
   name = "MaryFrance Hubilla"; 
  }
  
     if (content.substring(1) == "12 10 EE 1C")  
  {
   authorized = true;
   name = "JA Estipona"; 
  }

     if (content.substring(1) == "92 8D C6 1C")  
  {
   authorized = true;
   name = "Rona Aguila"; 
  }
       if (content.substring(1) == "02 EE 64 1C")  
  {
   authorized = true;
   name = "Homer Ereño"; 
  }
       if (content.substring(1) == "A2 30 BD 1C")  
  {
   authorized = true;
   name = "Raffy Torrella"; 
  }



  if (authorized) {
   Serial.println(name);
    digitalWrite(greenLED, HIGH);
     digitalWrite(RELAY, LOW);
  delay(3000);        
   digitalWrite(greenLED, LOW);
          digitalWrite(RELAY, HIGH);
  delay(500);
  digitalWrite(CS_SD,LOW);
  myFile=SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
  
    myFile.print(name);
    myFile.print(" _ ");   
    String content= "";
    
   
    DateTime now = rtc.now();
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print('/');
    myFile.print(now.year(), DEC);
    myFile.print('-');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    
  
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print('-');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
  
    myFile.close();


  }

  
  digitalWrite(CS_SD,HIGH);
}}

Mine can only store up to 6 names? can it still be improved?

YuKanatSiMi:
Mine can only store up to 6 names? can it still be improved?

Yes, see my suggestion in post #13 above