Go Down

Topic: Create variable directory and incremetal files (Read 1 time) previous topic - next topic

EnricoSandoni

Hello everybody!
I'm doing a project where i need to collect data from sensors and then save these data in a SD card memory; all these things are well explained in Arduino's reference guide on the website but I however found a trouble: since in my project i collect the temperature and other parameters per each day, I also need to collect them in order into the SD, making a txt file per each day. it would be beautiful if I can also make a folder for each year, with a folder for each month in it, like this: 2013/jan,feb,mar,apr,may,jun..etc and for each month 31 or 30 files(one per day).
The trouble is that in the code line for writing a directory and also for making files, you must write only const char : SD.mkdir("a/b/c") in this i'll have only three folders;  SD.open("*.txt")
My question is: how I can use for example a for loop for increasing (for example an array, year[X]), for creating a new directory every year, and the same for month of course?

thanks for answering, and if this topic had already been treated, can you send me to that page?
thanks a lot.

PaulS

Quote
The trouble is that in the code line for writing a directory and also for making files, you must write only const char : SD.mkdir("a/b/c") in this i'll have only three folders;

That is not true. You can create any number of directory levels at once, using a dynamically defined string.

Code: [Select]
int year = 2013;
char dirName[24];
sprintf(dirName, "%d/Jan", year);
SD.mkdir((const char *)dirName);

should create a directory called Jan in one called 2013.

Quote
My question is: how I can use for example a for loop for increasing (for example an array, year[X]), for creating a new directory every year, and the same for month of course?

Why would year be an array? In the snippet about, you can increment year by 1 and get 2014/Jan created.

You could create an array of month names:
char monthNames[] = {"", "Jan", "Feb", "Mar", "Apr", ...

Then:
Code: [Select]
int month = 3;
sprintf(dirName, "%d/%s", year, monthNames[month]);
SD.mkdir((const char *)dirName);

should create a directory called Mar in one called 2013.

EnricoSandoni

@PaulS:
Thank you very much for the help, and I'm sorry for having written false things but I really didn't know that kind of code, I studied it at high school (C++), but not in a close way, so I really thank you for help, this will allow me to finish my project and do more complex project in future!

EnricoSandoni

@PaulS
Hi and thanks again for the help, I have other questions:
when i compile the code line you have given to me, at:   char monthNames[] = {"", "Jan", "Feb", "Mar", "Apr"}; the compiler says:

error: too many initializers for 'char []'

and for: SD.mkdir((const char *)dirName);  error: invalid conversion from 'const char*' to 'char*'

then I don't know if there are other errors

I put this code lines in a program just to try..

PaulS

Code: [Select]
char monthNames[] = {"", "Jan", "Feb", "Mar", "Apr"}; the compiler says:

should have been:
Code: [Select]
char *monthNames[] = {"", "Jan", "Feb", "Mar", "Apr"}; the compiler says:


Quote
and for: SD.mkdir((const char *)dirName);  error: invalid conversion from 'const char*' to 'char*'

Then, the cast is not necessary.
Code: [Select]
SD.mkdir(dirName);

EnricoSandoni

@PaulS, hello, since you have been able to help me i ask again for your help, this time i would need to know how write a file into a directory I have created before. the trouble is I have created the directory and the file with the code lines you have shown me in messages before, but when I try to create the files in the directory, i fail.

my idea is: I create a directory that it can change when i decide to do that, and i would make a file per day, and then write on time every minute. Thanks again for your time!!


I post you the skecth:



#include <SD.h>
char dirName[8],DATO[18],fileName[8],DataString[20];
unsigned long ms=0, psd=0;
int anno=2014;
byte mese=1, giorno=2, ore=3, minu=4, sec=5;
File myFile;

void setup(){
Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

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



}

void loop(){

  Serial.println("sd");
   
    sprintf(dirName, "%d/%d/%d.txt", anno,mese,giorno);  //per creare la directory
    //SD.mkdir(dirName);   
    //sprintf(fileName,"%d.txt",giorno);// per creare il nome del file
    //sprintf(DATO,"%d/%d",dirName,fileName);//per creare la location dove scrivere il file
    sprintf(DataString,"%d/%d/%d  %d:%d:%d   ",giorno,mese,anno,ore,minu,sec);// crea la stringa da stampare su sd

   
    myFile = SD.open(dirName);
    myFile = SD.open(dirName, FILE_WRITE);
    myFile.println(DataString);
     if (myFile) {
     Serial.print("Writing to test.txt...");
     myFile.println(DataString);
     //myFile.print(T,1);
     //myFile.print(" , ");
     //myFile.print(H);
     //myFile.print(" , ");
     //myFile.print(P);
     //myFile.print(" , ");
     //myFile.println(A);
     
        // close the file:
     myFile.close();
     Serial.println("done.");
     } else {
     // if the file didn't open, print an error:
     Serial.println("error opening test.txt");
     }
    giorno++;
    ore++;
    minu++;
    sec++;
 
 
 
  delay(1000);






}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy