I am working on a project where I am trying to create six data loggers using an Arduino Uno, an RFID MFRC522 module, an RTC module, and a microSD card module. My goal is to save the time and the name (UID) of the triggered RFID tag onto an SD card. The data loggers work perfectly for a few hours, but after 6-7 hours of inactivity (no triggered tags), four out of the six data loggers fail to save the data onto the SD card. The remaining two data loggers continue to work flawlessly. I suspect that the issue could be related to the power supply or possibly something in the code.
This is my code:
#include <SPI.h>
#include <MFRC522.h>
#include <virtuabotixRTC.h>
#include <SD.h>
#include <string.h>
#pragma execution_character_set("utf-8")
//MODULE REAL-TIME-CLOCK
virtuabotixRTC myRTC(5, 7, 8);
#define SS_PIN 10
#define RST_PIN 9
#define Led_pin 6
#define Led_pinRED 2
//PINS RFID
MFRC522 mfrc522(SS_PIN, RST_PIN);
int buzzerPin = 3;
// //SELECT PIN SD MODULE
const int chipSelect = 4;
void setup() {
pinMode(Led_pin, OUTPUT);
pinMode(Led_pinRED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
SPI.begin();
//myRTC.setDS1302Time(30, 36, 11, 4, 24, 5, 2023);
mfrc522.PCD_Init();
while (!Serial) {
;
}
while (!SD.begin(chipSelect)) {
// Serial.println("Initialization failed!");
delay(1000);
tone(buzzerPin, 100, 600);
digitalWrite(Led_pinRED, HIGH);
delay(1000);
digitalWrite(Led_pinRED, LOW);
}
Serial.println("Initialization done.");
digitalWrite(Led_pinRED, LOW);
tone(buzzerPin, 500, 600);
digitalWrite(Led_pin, HIGH);
delay(1000);
digitalWrite(Led_pin, LOW);
}
void loop() {
myRTC.updateTime();
//READ RFID ID
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
} else {
if (mfrc522.PICC_ReadCardSerial()) {
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();
tone(buzzerPin, 3000, 600);
digitalWrite(Led_pin, HIGH);
delay(1000);
digitalWrite(Led_pin, LOW);
File uidsfile = SD.open("uids.txt");
if (!uidsfile) {
Serial.println("Failed to open uids.txt file!");
return;
}
uidsfile.seek(0); // Start from the beginning of the file
int lineNum = 1;
String line;
// line.trim();
while (uidsfile.available()) {
line = uidsfile.readStringUntil('\n');
line.trim();
line.toUpperCase();
line.replace(" ", "");
content.toUpperCase();
content.replace(" ", "");
// Serial.println(line);
// Serial.println(content);
if (line == content) {
Serial.println("UID found at line: " + String(lineNum));
break;
}
lineNum++;
}
uidsfile.close();
int currentLine = 1;
File namefile = SD.open("names.txt");
while (namefile.available()) {
String line = namefile.readStringUntil('\n');
if (currentLine == lineNum) {
line.trim(); // Optional: Remove leading and trailing whitespaces
Serial.println(line);
File dataFile = SD.open("data.txt", FILE_WRITE);
dataFile.print(line);
dataFile.close();
break; // Exit the loop after printing the target line
}
currentLine++;
}
namefile.close();
File dataFile = SD.open("data.txt", FILE_WRITE);
if(currentLine == 1)
{
dataFile.print(" ");
}
if(currentLine == 2 || currentLine == 4 )
{
dataFile.print(" ");
}
dataFile.print(" ");
if (myRTC.dayofmonth < 10)
dataFile.print("0");
dataFile.print(myRTC.dayofmonth);
// data file
dataFile.print("/");
if (myRTC.month < 10)
dataFile.print("0");
dataFile.print(myRTC.month);
dataFile.print("/");
dataFile.print(myRTC.year);
dataFile.print(" ");
if (myRTC.hours < 10)
dataFile.print("0");
dataFile.print(myRTC.hours);
dataFile.print(":");
if (myRTC.minutes < 10)
dataFile.print("0");
dataFile.println(myRTC.minutes);
// dataFile.print(":");
// dataFile.println(myRTC.seconds);
dataFile.close();