Rfid project sd card not save data how to resolve this problem

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);

}

Hello, again!

What changed in your project? Did you change your software? Did you change something in the electronics? Are you using a battery that is dead?

not change any one my code have some problem im try still not have a resolve problem so sad

Have you been using serial.Print() to display what you are writing to the card? Did you close the card file before stopping the program?

no😕

Time to start taking responsibility for debugging your project.

have a read of how-to-get-the-best-out-of-this-forum
e.g.

  1. your code is unreadable - improve presentation by using code tags, e.g. click < CODE > and paste you code where it says 'type or paste code here
  2. what microcontroller are you using, UNO, MEGA, ESP32, RP2040, etc?
  3. give links to the devices you are using?
  4. upload a schematic showing device wiring and power supplies etc?

if the SD card is sharing a SPI bus with other devices there can be problems
Sharing the SPI bus among SD card and other SPI devices

also have a look at spi-miso-pin-conflict and nrf24-stops-working-when-sd-card-is-plugged-in-nodemcu-rf24-sd-mpu6050-bmp280

possibly switch to a microcontroller with two SPI interfaces?