How to use RTC value as SD card file name?

I have been following a few previous posts, but unable to get the RTC value to the file name.

My code below is a bit of patchwork from a few tutorials:

// We need the SPI and SD libraries
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

byte  second, minute, hour, weekDay, day, month, year;

// Is card inserted
#define SDmissing 7

// Some random input value
#define dataPin A0


// Logging sequence number (could be date/time from RTC)
int logNo = 1;
char filename[] = "00000000.CSV";
File dataLog;
//----------------------------------------------
// SETUP    SETUP    SETUP    SETUP    SETUP
//----------------------------------------------
void setup() {
  // Serial monitor window
  Serial.begin(9600);
      Wire.begin();
      RTC.begin();
  Serial.println("Setup begins");

  // SD inserted will be LOW when inserted and HIGH when missing
  pinMode(SDmissing, INPUT_PULLUP);

  // Common routine to connect to SD card
  initSDCard();

  Serial.println ("SD Card initialisation completed successfully");
}

//----------------------------------------------
// LOOP    LOOP    LOOP    LOOP    LOOP    LOOP
//----------------------------------------------
void loop() {

DateTime now = RTC.now();

  // Get some psuedo data value from somewhere
  int dataVal = analogRead(dataPin);
  String dataString = "";

    if (!digitalRead(SDmissing)) {

    // Now able to write to file

    
    dataLog = SD.open(filename, FILE_WRITE);

    // Log data
    if (dataLog) {
      dataString = String(logNo);
      dataString += " data value now ";
      dataString += String(dataVal);

      Serial.print(",");
      Serial.println(dataString);
      dataLog.println(dataString);

      // Close file
      dataLog.close();

      // Increment log msg no
      logNo++;
    }
    else {
      initSDCard();
    }
  }
  else {
    initSDCard();
  }
}

//----------------------------------------------
// Common SD card initialisation routine
//----------------------------------------------
    void getFileName(){
    sprintf(filename, "%02d%02d%02d.csv", year, month, day);
  // Artificial delay to reduce data written to card!
  delay(1000);
    }
    
void initSDCard()
{
  // Expected configuration for SPI/SD library:
  // MOSI – pin 11
  // MISO – pin 12
  // SCK (CLK) – pin 13
  // CS – pin 10

  while (!SD.begin()) {
    Serial.println("------------------------------");
    Serial.println("SD Card not found / responding");
    Serial.println("Insert a formatted SD card");
    Serial.println("------------------------------");
    SD.end();
    delay(1000);
  }

  return;
}

I have a system where the SD card can be removed and re-inserted, which collects the data without the need for restarting the arduino uno board. My aim is to get the current RTC value as the initial file name.

Anyone able to point me in the right direction on how to achieve this?

Are you able to get the date,time values that you want from the RTC?

Your code seems a bit garbled but essentially the same as mine, although I'm not sure removing the card "without restarting" is such a great idea.

You can check the clock and set the filename in Setup.
You can check the date in the loop thereby only needing to set the filername if the day has changed, i.e. after midnight e.g.

   GetClock();
  if (today != day)
  {
   today = day;
   getFileName(); 
  }

ieee488:
Are you able to get the date,time values that you want from the RTC?

I am able to get the correct date/time from the RTC, but had removed the serial print portion of the code to simplify.

If serial print function is used i get the following:

"Setup Begins
SD Card initialisation completed successfully
21/02/2018 11:46:54, 1 data value now 476
21/02/2018 11:46:55, 2 data value now 476
21/02/2018 11:46:56, 3 data value now 476" etc.

Nick_Pyner:
Your code seems a bit garbled but essentially the same as mine, although I'm not sure removing the card "without restarting" is such a great idea.

You can check the clock and set the filename in Setup.
You can check the date in the loop thereby only needing to set the filername if the day has changed, i.e. after midnight e.g.

   GetClock();

if (today != day)
  {
  today = day;
  getFileName();
  }

Thanks Nick, will try this today. Would you be able to send over your code for me to compare to make sure i'm going along the right lines?

Thanks in advance!

Strip out whatg you don't need - which is most of it. Note that no RTC library is used, but the variables are the same or similar.

/*
This programme reads temperatures from  three DS18B20 sensors at one 
second intervals for transmission over bluetooth - no timestamping

Every tenth reading is recorded on SD card in real numbers to two decimal places. Each line is timestamped.

A new file is created at midnight using the date as the filename.

Daily files can be read from SD via bluetooth by sending MMDD

  Serial.begin(115200);

Credit to Hacktronics, Mellis & Igoe, Garage Box, Zoomkat, Bildr.org, etc.
Kudos to lar3ry in Saskatchewan, Stanley in KL, Coding Badly in Tx.
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include "Wire.h"                
#include <SD.h>
#include <SPI.h>      

#define DS1307_ADDRESS 0x68

char filename[] = "00000000.CSV";
File myFile;
char dumpName[] = "00000000.CSV";
File dumpFile;

// Green group Dee Why (red or amber LED shields)
byte InThermo[8] =  {0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0x9F };
byte OutThermo[8] = {0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {0x28, 0x54, 0xF7, 0x2D, 0x04, 0x00, 0x00, 0x68}; 
  
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
byte  second, minute, hour, weekDay, day, month, year;
int k=0;

const int chipSelect = 4;

float tempC, InTemp, OutTemp, DrainTemp;

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";

char charBuf [13];

String readString;
String StringOne;

void setup() {
  Serial.begin(115200);
  
  delay(300);//Wait for newly restarted system to stabilize
  pinMode(0, INPUT_PULLUP);   // just a precaution for bluetooth
  pinMode(53, OUTPUT);//MEGA use poin 10 if Uno
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed");
    // don't do anything more:
    return;
  }
  Serial.println("CARD OK");
      GetClock();
      getFileName();
  }

void loop() {
   GetClock();
  if (today != day)
  {
   today = day;
   getFileName(); 
   header();
   daily();

  if (thismonth !=month)
  {
    thismonth=month;
    dailyheader();
  }
  }  
    while (Serial.available()) 
  {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }// end while
  if (readString.length() >0) 
  {  
    getDump();   
   readString="";  
  } // end if
  
  //get the values from the DS8B20's 
  sensors.requestTemperatures();

  InTemp = sensorValue(InThermo);
  OutTemp = sensorValue(OutThermo);  
  DrainTemp = sensorValue(DrainThermo); 
  
  k=k+1;  
  if (k>9 )
  {  
           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.println(DrainTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
  k=0;
  }
  delay(1000);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

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

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

void getDump() {
   StringOne = "2015" + readString + ".csv";
   StringOne.toCharArray(charBuf, 13);  
     Serial.println("");
     Serial.println(charBuf);
  File dumpFile = SD.open(charBuf);
  if (dumpFile) 
    {
    while (dumpFile.available())
    {
      Serial.write(dumpFile.read());
    }
    dumpFile.close();
    }  
  else {
    Serial.println("error opening file");
       }
}