Hi
Im relatively new to programming and arduino. For a summer camp im trying to build a simple time attendance system using the RFID module. Im also using a LCD display, two LED's and a SSD module to save all the data. So far I managed to solve all problems, everything's wired up and workin except that as I wanted to add more RFID cards to my string eventually I ran out of memory. Im hopping to store up to 50 RFID cards with a name attached to it(not on the card itself but theres a list of the UID and the according name. Is there a better way to do it? Maybe using CHAR?
Thanks for your Help!!
Heres my code:
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define select pin for SD card module
#define CS_SD 4
// Create a file to store the data
File myFile;
// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID);
// Variable to hold the tag's UID
String uidString;
// Instance of the class for RTC
RTC_DS1307 rtc;
// Define check in time
const int checkInHour = 16;
const int checkInMinute = 5;
//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;
// Rfid definiert Block der gelesen wird
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
// Set LEDs and buzzer as outputs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.init(); // initialize the lcd
lcd.noDisplay();
// Init Serial port
Serial.begin(9600);
while(!Serial); // for Leonardo/Micro/Zero
// Init SPI bus
SPI.begin();
// Init MFRC522
rfid.PCD_Init();
// Setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(CS_SD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// Setup for the RTC
if(!rtc.begin()) {
Serial.println("Couldn't find RTC");
while(1);
}
if(!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
}
void loop() {
//look for new cards
if(rfid.PICC_IsNewCardPresent()) {
readRFID();
logCard();
verifyCheckIn();
}
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);
// Sound the buzzer when a card is read
tone(buzzer, 2000);
delay(100);
noTone(buzzer);
delay(100);
}
void logCard() {
String names[] = { "Aaron", "Remy", "Twintzeps", "Armin", "Ilo", "Bolt", "Jamie", "Topo", "Floss"};
String tagliste[] = { "129 129 135 50", "113 251 237 50", "113 203 107 50", "129 180 235 50", "99 225 157 52", "99 215 242 52", "99 180 92 52", "99 227 167 52", "83 249 207 52"};
// 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(", ");
for(int x = 0; x < 50; x++) {
if (uidString == tagliste[x]) {
myFile.print(names[x]);
}
}
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();
// Print time on lcd display
lcd.backlight();
lcd.display();
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.println(now.minute(), DEC);
lcd.setCursor(2,1);
for(int x = 0; x < 50; x++) {
if (uidString == tagliste[x]) {
lcd.print(names[x]);
}
}
delay(1000);
lcd.clear();
// 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 verifyCheckIn(){
if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){
digitalWrite(greenLED, HIGH);
Serial.println("Wilkomme am Deck!!!");
lcd.println("Wilkomme am Deck!!!");
delay(1000);
digitalWrite(greenLED,LOW);
lcd.clear();
lcd.noDisplay();
lcd.noBacklight();
}
else{
digitalWrite(redLED, HIGH);
Serial.println("Du bisch z spat...");
lcd.println("Du bisch z spat...");
delay(1000);
digitalWrite(redLED,LOW);
lcd.clear();
lcd.noDisplay();
lcd.noBacklight();
}
}