RTC + SDcard creating file 1 day 1 file

Hello Everyone, I'm new to arduino, this is my first post, so please help if anything goes wrong.

I am currently working on a project that requires this RTC and SDCard module. I want this SDCard to create a new log file when the day changes. (1 file 1 day). but i can not figured it out.

this is my simple sketch

#include<stdlib.h>
#include <SPI.h>
#include "Wire.h"  // I2C
#include <SD.h>
#include "RTClib.h"

RTC_DS1307 RTC;

char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

const int chipSelect = 53;

void setup()
{
 
  Serial.begin(9600);
  while (!Serial) {
  }


  if (!SD.begin(chipSelect)) {
    Serial.println("Error!\t");
    return;
  }
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));
 
}

void loop()
{
  DateTime now = RTC.now();                              // get time from RTC
  int time = now.second();

  delay(2000);

  
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (dataFile) {
    dataFile.print(daysOfTheWeek[now.dayOfTheWeek()]);
    dataFile.print(now.day(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.year(), DEC);
    dataFile.print(" , ");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(' ');
    dataFile.close();
   
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(" , ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
  }
  else {
    Serial.println("Error");
  }
}

Thank You,

i really appreciate any advice :slight_smile:

char fn[15];
sprintf(fn, "%02d-%02d-%02d.CSV", year(t) - 2000, month(t), day(t));
File file = SD.open(fn, FILE_WRITE);

more here

Juraj:

char fn[15];

sprintf(fn, "%02d-%02d-%02d.CSV", year(t) - 2000, month(t), day(t));
File file = SD.open(fn, FILE_WRITE);




more [here](https://github.com/jandrassy/Regulator/blob/master/Regulator/CsvLog.ino)

thx :slight_smile: