How to create a new log file every day without erase any one?

Hi every one, i'm not very experienced using arduino.

I want to develop a datalogger that creates a new log file every day and saves it on a 32Gb SD card, i'm using an arduino mega2560 + ethernet shield, SdFat-beta library, a RTC and Timer library. i've already made that the log file name corresponds to the current date (DDMMYYY) and i leave mi code below...Hope you can help me, greetings to all!

#include <SPI.h>
#include "SdFat.h"
#include "RTClib.h"
#include <Wire.h>

File file;
int SD_CS= 4;
RTC_DS1307 RTC;
String file_name= "";
char fn[]= "MMDDYYYY.csv";
int i=0;
SdFat SD;
int current_day=0;
int new_day=0;

void dateTime(uint16_t* date,uint16_t* time)
{
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now= RTC.now();
  
  *date= FAT_DATE(now.year(), now.month(), now.day());
  *time= FAT_DATE(now.hour(), now.minute(), now.second());
}


void setup()
{
  Serial.begin (9600);
  Wire.begin();
  RTC.begin();
  SdFile::dateTimeCallback(dateTime);
  if (!SD.begin(SD_CS))
  {
    Serial.println("Error SD card");
    
    return;
  }
  Serial.println ("SD card Ready!");
   RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now = RTC.now();
  
 // file_name = String(file_name +String(now.day(), DEC)+ String);
 if (now.day() < 10) {
      file_name = String(file_name + '0' + String(now.day(), DEC));
    }
    else {
    file_name = String(file_name +String(now.day(), DEC));
    }
    
  if (now.month() < 10) {
      file_name = String(file_name + '0' + String(now.month(), DEC));
    }
    else {
    file_name = String(file_name +String(now.month(), DEC));
    }
    file_name= String(file_name+String(now.year(),DEC));
  
  /*if (now.hour() < 10) {
      file_name = String(file_name + '0' + String(now.hour(), DEC));
    }
    else {
    file_name = String(file_name +String(now.hour(), DEC));
    }
  if (now.minute() < 10) {
      file_name = String(file_name + '0' + String(now.minute(), DEC));
    }
    else {
    file_name = String(file_name +String(now.minute(), DEC));
    }*/

  file_name = String(file_name + ".csv");
  
  for (i=0;i<=file_name.length();i++) {
    fn[i] = file_name.charAt(i);
    // Serial.print(file_name.charAt(i));
  }
  
  Serial.print("File Name: ");
  Serial.println(fn);
  
}

void loop()
{
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now= RTC.now();
  
  String data_string = "";
  
  if (now.day() < 10) {
      data_string = String(data_string + '0' + String(now.day(), DEC));
    }
    else {
    data_string = String(data_string +String(now.day(), DEC));
    }
   data_string += "/";
  if (now.month() < 10) {
      data_string = String(data_string + '0' + String(now.month(), DEC));
    }
    else {
    data_string = String(data_string +String(now.month(), DEC));
    }
  data_string += "/";
  data_string = String(data_string)+ String(now.year(), DEC);
  
  data_string += " ";
  if (now.hour() < 10) {
      data_string = String(data_string + '0' + String(now.hour(), DEC));
    }
    else {
    data_string = String(data_string +String(now.hour(), DEC));
    }
  data_string += ":";
  if (now.minute() < 10) {
      data_string = String(data_string + '0' + String(now.minute(), DEC));
    }
    else {
    data_string = String(data_string +String(now.minute(), DEC));
    }
  data_string += ":";  
  if (now.second() < 10) {
      data_string = String(data_string + '0' + String(now.second(), DEC));
    }
    else {
    data_string = String(data_string +String(now.second(), DEC));
    }
  data_string += ",";
  
  for (int analogPin = 0; analogPin < 16; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < 15) {
      data_string += ",";
      }
    }
    
    //current_day= String(now.day(),DEC);
 file= SD.open(fn,O_CREAT | O_WRITE | O_APPEND);
   if(file)
   {
     file.println(data_string);
     file.flush();
     file.close();
     Serial.println(data_string); 
     
   }
   else
   {
     Serial.println("Error");
     Serial.println(fn);
   }
   
   delay(5000);
}
1 Like

Every time you want to log, compare the current day to the logfile day. (just remember the 3 integers)
if they are note the same you must create a new log file.

you might want to remove the String class and just use a char array, way faster and less memory.

// global
char filename[16];


void loop()
{
  ....

  sprintf(filename, "%04d%02d%02d.log", now.year(), now.month(), now.day()):
  Serial.println(filename);
  ...

}

Thank you Robtillaart, i will try what you say about the char class

Now, about the comparation how i can do it? i have some ideas about it, but i'm not sure they will work

thanks again

I think you just need to check you have just passed midnight
Something like

void loop() {

   GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
..........................

Little new update:

i've been thinking and working on my code, this is the new one with some modifications, i hope it will work, it's just a matter of time until a new day arrives...wish me luck!

#include <SPI.h>
#include "SdFat.h"
#include "RTClib.h"
#include <Wire.h>

File file;
int SD_CS= 4;
RTC_DS1307 RTC;
String file_name= "";
char fn[]= "MMDDYYYY.csv";
int i=0;
SdFat SD;
//String current_date=0;
//char new_date=0;

void dateTime(uint16_t* date,uint16_t* time)
{
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now= RTC.now();
  
  *date= FAT_DATE(now.year(), now.month(), now.day());
  *time= FAT_DATE(now.hour(), now.minute(), now.second());
}


void setup()
{
  Serial.begin (9600);
  Wire.begin();
  RTC.begin();
  SdFile::dateTimeCallback(dateTime);
  if (!SD.begin(SD_CS))
  {
    Serial.println("Error SD card");
    
    return;
  }
  Serial.println ("SD card Ready!");
   RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now = RTC.now();
  
 // file_name = String(file_name +String(now.day(), DEC)+ String);
 if (now.day() < 10) {
      file_name = String(file_name + '0' + String(now.day(), DEC));
    }
    else {
    file_name = String(file_name +String(now.day(), DEC));
    }
    
  if (now.month() < 10) {
      file_name = String(file_name + '0' + String(now.month(), DEC));
    }
    else {
    file_name = String(file_name +String(now.month(), DEC));
    }
    file_name= String(file_name+String(now.year(),DEC));
  
  /*if (now.hour() < 10) {
      file_name = String(file_name + '0' + String(now.hour(), DEC));
    }
    else {
    file_name = String(file_name +String(now.hour(), DEC));
    }
  if (now.minute() < 10) {
      file_name = String(file_name + '0' + String(now.minute(), DEC));
    }
    else {
    file_name = String(file_name +String(now.minute(), DEC));
    }*/

  file_name = String(file_name + ".csv");
  
  for (i=0;i<=file_name.length();i++) {
    fn[i] = file_name.charAt(i);
    // Serial.print(file_name.charAt(i));
  }
  
  Serial.print("File Name: ");
  Serial.println(fn);
  
}

void loop()
{
  //RTC.adjust(DateTime(__DATE__,__TIME__));
  DateTime now= RTC.now();
  
  String data_string = "";
  
  if (now.day() < 10) {
      data_string = String(data_string + '0' + String(now.day(), DEC));
    }
    else {
    data_string = String(data_string +String(now.day(), DEC));
    }
   data_string += "/";
  if (now.month() < 10) {
      data_string = String(data_string + '0' + String(now.month(), DEC));
    }
    else {
    data_string = String(data_string +String(now.month(), DEC));
    }
  data_string += "/";
  data_string = String(data_string)+ String(now.year(), DEC);
  
  data_string += " ";
  if (now.hour() < 10) {
      data_string = String(data_string + '0' + String(now.hour(), DEC));
    }
    else {
    data_string = String(data_string +String(now.hour(), DEC));
    }
  data_string += ":";
  if (now.minute() < 10) {
      data_string = String(data_string + '0' + String(now.minute(), DEC));
    }
    else {
    data_string = String(data_string +String(now.minute(), DEC));
    }
  data_string += ":";  
  if (now.second() < 10) {
      data_string = String(data_string + '0' + String(now.second(), DEC));
    }
    else {
    data_string = String(data_string +String(now.second(), DEC));
    }
  data_string += ",";
  
  for (int analogPin = 0; analogPin < 16; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < 15) {
      data_string += ",";
      }
    }
    
String current_date= (fn);
Serial.print("Fecha actual: ");
Serial.println (current_date);
String new_date= "";
if (now.day() < 10) {
      new_date = String(new_date + '0' + String(now.day(), DEC));
    }
    else {
    new_date = String(new_date +String(now.day(), DEC));
    }
   //data_string += "/";
  if (now.month() < 10) {
      new_date = String(new_date + '0' + String(now.month(), DEC));
    }
    else {
    new_date = String(new_date +String(now.month(), DEC));
    }
  //data_string += "/";
  new_date = String(new_date)+ String(now.year(), DEC);
  Serial.print("Nueva Fecha: ");
  Serial.println(new_date);

Serial.println(new_date);
 
 if (current_date != new_date)
 {
 file= SD.open(fn,O_CREAT | O_WRITE | O_APPEND);
   if(file)
   {
     file.println(data_string);
     file.flush();
     file.close();
     Serial.println(data_string); 
     
   }
   else
   {
     Serial.println("Error");
     Serial.println(fn);
   }
 }
 delay(5000);
   current_date= new_date;
   //Serial.print("Nueva fecha actual: ");
   //Serial.println(current_date);
   
}

it's just a matter of time until a new day arrives...wish me luck!

you can do a simulation that is way faster ....

or a new file every minute , same trick should work

Well, my test code isn't working, i think i have problems with the "if loop", i have to check out that part, maybe the problem is there...
I'm considering use maybe a "Switch loop" with a default case (the date hasn't change so still logging on the first file) and another case when the date changes, so will open a new logfile...by the way, does anybody knows if SdFat-beta library has any function to open a new file or something like that?

For other part, of course i'll do the simulations

Thanks a lot :grin:

It all looks very convoluted and I think the following snippets are all you need

#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h>             // Nokia 5110
#include "Wire.h"                
#include <SD.h>
#include <SPI.h>             
#include "RTClib.h"              //Date As Filename
#include <string.h>              //Date As Filename 

#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

//........

void setup() {
  
  //........
        getFileName();
      lcd.setCursor(0,3); 
      lcd.println(filename);
        delay(2000);
  
    lcd.clear();
}


void loop() {
   GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
  
  //.........
  
  
void getFileName(){

DateTime now = RTC.now();
sprintf(filename, "%02d%02d%02d.csv", now.year(), now.month(), now.day());
}

Thank you Nick, i'll try what you say ;D

FWIW, I am doing a bit of work on this. I don't use a library for telling the time, but I did use RTC.lib to get date as filename. This may sound stupid but it is actually more efficient with memory. This was not the original intention, it is just that the "no library" method was the first clock I could get to work.

The one-shot test code below enables me to ditch the RTC library

/*Arduino 1.0+ Only
currently compiles to 11 308
*/
#include <SD.h>
#include "Wire.h"

#define DS1307_ADDRESS 0x68

byte  second, minute, hour, weekDay, day, month, year;
char filename[] = "00000000.CSV";


void setup(){
  Serial.begin(9600);
  Serial.println(filename);
    Wire.begin();
    GetClock();
    getFileName();
      Serial.print(filename);
  }   

void loop(){
 // nothing here 
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  day = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}
1 Like

Thank you Nick, this has been very usefull :smiley: