Pages: [1]   Go Down
Author Topic: Write to a file on an SD card inside a folder?  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone.

I am trying to store files inside a series of folders, depending on the date.

I am using a RTC, and the standard SD library.

I have previously gotten the program to set the file name to the date, and I can figure out how to make my folders with the date, but I can not seem to figure out how to store my data logger file inside said folder.

Anyone have any suggestions?

Thank you.
Code:
#include <stdarg.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>


File root;
RTC_DS1307 RTC;
void setup()
{
  //RTC.adjust(DateTime(__DATE__, __TIME__));    //sets RTC time to PC time at compiling.
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  File logfile;

  Wire.begin();
  RTC.begin();

  Serial.println("done!");
  DateTime now = RTC.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  // create a new file
  char foldername[] = "03/03";
  foldername[0] = now.month()/10 + '0'; //To get 1st digit from month()
  foldername[1] = now.month()%10 + '0'; //To get 2nd digit from month()
  foldername[3] = now.day()/10 + '0'; //To get 1st digit from day()
  foldername[4] = now.day()%10 + '0'; //To get 2nd digit from day()

  char filename[] = "0003AGM.CSV";
  filename[0] = now.hour()/10 + '0'; //To get 1st digit from hour()
  filename[1] = now.hour()%10 + '0'; //To get 2nd digit from hour()
  filename[2] = now.minute()/10 + '0'; //To get 1st digit from minute()
  filename[3] = now.minute()%10 + '0'; //To get 2nd digit from minute()

  Serial.println(foldername);
  Serial.println(filename);

  SD.mkdir(foldername);
  if (! SD.exists(filename)) {
    // only open a new file if it doesn't exist
    logfile = SD.open("foldername/filename", FILE_WRITE);
    logfile.print("test");
    logfile.flush();
  }
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not an SD problem, it's a string problem. You are trying to open a file named "foldername/filename". This won't work since there is no folder named "foldername". In fact what you want to do is produce a string containing the value of the variable foldername, plus the file separator (/) plus the value of the variable filename. You can use sprintf to concatenate these strings into a single string that you can use as your file name. (You can also use sprintf to tidy up your code to convert the date into a folder name.) There are lots of other ways to concaqtenate strings, but I'd say that sprintf is the simplest and most powerful solution for what you're trying to do.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: