my rfid project habe a some problem. data not save sd card please help me
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <SD.h>
// ------------------- PIN DEFINITIONS -------------------
#define RESET_PIN 9
#define SELECT_PIN 10
#define BUZZER_PIN 8
#define SD_CS_PIN 4 // SD Card Chip Select
MFRC522 rfidReader(SELECT_PIN, RESET_PIN);
// ------------------- LCD -------------------
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ------------------- RTC -------------------
RTC_DS3231 rtc;
// ------------------- SD Card File Name -------------------
const char* dataFileName = "ATTEND.CSV";
// ------------------- USER DATA STRUCT -------------------
struct UserData {
String cardUID;
String rollNumber;
String name;
String lastTimeIn;
bool isInside;
};
// ------------------- USER LIST -------------------
UserData userList[] = {
{"6da1f721", "MALINDA 005", "MALINDA", "", false},
};
const int userCount = sizeof(userList) / sizeof(userList[0]);
// ------------------- TIME FUNCTIONS -------------------
long timeToSeconds(String t) {
int h = t.substring(0,2).toInt();
int m = t.substring(3,5).toInt();
int s = t.substring(6,8).toInt();
return h3600L + m60L + s;
}
String secondsToTime(long total) {
int h = total / 3600;
int m = (total % 3600) / 60;
int s = total % 60;
char buf[9];
sprintf(buf, "%02d:%02d:%02d", h, m, s);
return String(buf);
}
String getCurrentTime() {
DateTime now = rtc.now();
char buf[9];
sprintf(buf, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
return String(buf);
}
// ------------------- FIND USER -------------------
int findUserIndex(String cardNumber) {
for(int i=0; i<userCount; i++){
if(userList[i].cardUID == cardNumber)
return i;
}
return -1;
}
// ------------------- LCD MESSAGE CYCLE -------------------
void showMessageCycle(String message) {
// Clear the second line only
lcd.setCursor(0,1);
lcd.print(" ");
int msgLen = message.length();
int startCol = (16 - msgLen) / 2;
// Cycle 1: Show message
lcd.setCursor(startCol, 1);
lcd.print(message);
delay(500);
// Cycle 2: Show time
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(4,1);
lcd.print(getCurrentTime());
delay(500);
// Cycle 3: Show message again
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(startCol, 1);
lcd.print(message);
delay(500);
}
// ------------------- SD CARD LOGGING (MODIFIED) -------------------
void logDataToSD(String logEntry) {
// SD Card Initialisation is handled in setup(). We assume it is initialised here.
File dataFile = SD.open(dataFileName, FILE_WRITE);
if (dataFile) {
dataFile.println(logEntry);
dataFile.close();
Serial.println("SD Logged: " + logEntry);
} else {
// If the file cannot be opened (e.g., card was removed later)
Serial.println("Error opening " + String(dataFileName) + " for writing!");
}
}
// ------------------- SETUP (MODIFIED: SD check only) -------------------
void setup() {
Serial.begin(9600);
SPI.begin();
rfidReader.PCD_Init();
pinMode(BUZZER_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Init SD Card...");
// --- SD CARD CHECK IN SETUP ONLY ---
if (!SD.begin(SD_CS_PIN)) {
lcd.clear();
lcd.print("SD NOT FOUND!");
// Halt the program if the SD card is not found on startup
while (true);
}
if (!rtc.begin()) {
lcd.clear();
lcd.print("RTC ERROR");
while (true);
}