Autoname file

Hi guys, I've been trying to create this simple code in order to create new files everything a run my arduino code (data1.txt, data2.txt, data3.txt..)
I'm having some trouble with the code and I couldn't figure out why.
My idea is to create a num.txt file and use it to count all the files that have been made and add 1 to next one about to be created.
I know there is some simple way of doing that, but I don't know how to do it.

#include <SD.h>
File myFile;
File myData;

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");

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

myFile = SD.open("num.txt", FILE_WRITE); //TENTAR WRITE AND READ

if (SD.exists("num.txt")) {
}
else
{
if (myFile) {
Serial.print("Writing...");
myFile.println("0");
// close the file:
myFile.close();
Serial.println("Done.");
} else {
// if the file didn't open, print an error:
Serial.println("Error opening num.txt");
}
}
// re-open the file for reading:
myFile = SD.open("num.txt");
if (myFile) {
Serial.println("Reading...");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
int num = myFile.read();
int num2 = num + 1;
Seria.println(numDataa);
}

// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

if (SD.exists("num.txt")) {
SD.remove("num.txt");
}

myFile = SD.open("num.txt",FILE_WRITE);
if (myFile) {
Serial.print("Writing...");
myFile.println(num2);
// close the file:
myFile.close();
Serial.println("Done.");
} else {
// if the file didn't open, print an error:
Serial.println("Error opening num.txt");
}
sprintf(newfile, "data%02i.txt", num2);

Instead of this:

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    int num = myFile.read();
    int num2 = num + 1;
    Seria.println(numDataa);
    }

try this:

    // read from the file until there's nothing else in it:
   int num = 0;
    if (myFile.available()) {
      num = myFile.parseInt();
      Serial.println(num);
      }
    num++;
   int num2 = num;

When I try that, it says: error: 'num2' was not declared in this scope
The problem is in the last part of the code: error

myFile = SD.open("num.txt",FILE_WRITE);
if (myFile) {
Serial.print("Writing...");
myFile.print(num2);
// close the file:
myFile.close();
Serial.println("Done.");
} else {
// if the file didn't open, print an error:
Serial.println("Error opening num.txt");
}
sprintf(newfile, "data%02i.txt", num);

I was trying to create something more simple and came up with that:

void setup ()

int n=1;
while (SD.exists("data%i.txt",n))
{
n++;
}
File dataFile = SD.open("data%i.txt",n,FILE_WRITE);

I'm pretty sure the syntax is not right, but the logic seems right to me.
Any suggestions?

felipearcaro:
int n=1;
while (SD.exists("data%i.txt",n))
{
n++;
}
File dataFile = SD.open("data%i.txt",n,FILE_WRITE);

I'm pretty sure the syntax is not right, but the logic seems right to me.
Any suggestions?

That seems like the best way to do it, to me. You can use snprintf() to format the file names.

char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
int n = 0;
do
{
    snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
}
while(SD.exists(filename));
//now filename[] contains the name of a file that doesn't exist

You could use the example code from File->Examples->SD->Datalogger

felipearcaro:
I was trying to create something more simple and came up with that:
Any suggestions?

Why not do it this way (pseudo-code):

FILE *fp;
char buffer [16];
int x;
for (x = 0; x < 100; x++) { // 100 should be enough, make it bigger if needed!
    sprintf (buffer, "file%03d.txt", x); // filename format is "file000.txt" to "file099.txt"
    fp = SD.open (buffer, FILE_READ); // try to open file, read only (to avoid creating a new one)
    if (fp) { // if the file exists, close it
        SD.close (fp);
    } else { // does not exist, we found the next free one
        break;
    }
}
// here "buffer" contains the filename of the next NON-EXISTENT FILE (use to open file for write)

Hope this helps.

I tried to use the codes you guys posted with the datalogger example because this is pretty much what I want the SD for.

ThiagoH:

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
  int n = 0;
  do
     {
        snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
     }
  while(SD.exists(filename));
  //now filename[] contains the name of a file that doesn't exist

  }
void loop()
  {
  // make a string for assembling the data to log:
 char *filename;
 int sensorValue = analogRead(A0);
 float dataVoltage = sensorValue*(5.0/1023.0);
 File dataFile = SD.open(filename,FILE_WRITE);
 dataFile.println(dataVoltage);
 dataFile.close();
 Serial.println(dataVoltage);
 }

Krupski:

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
   char buffer [16];
   int x;
   for (x = 0; x < 100; x++) { // 100 should be enough, make it bigger if needed!
    sprintf (buffer, "file%03d.txt", x); // filename format is "file000.txt" to "file099.txt"
    File fp = SD.open (buffer, FILE_READ); // try to open file, read only (to avoid creating a new one)
    if (fp) { // if the file exists, close it
        fp.close();
        
    } else { // does not exist, we found the next free one
        break;
    }
}
// here "buffer" contains the filename of the next NON-EXISTENT FILE (use to open file for write)

  }
void loop()
  {
  // make a string for assembling the data to log:
 char *buffer;
 int sensorValue = analogRead(A0);
 float dataVoltage = sensorValue*(5.0/1023.0);
 File dataFile = SD.open(buffer,FILE_WRITE);
 dataFile.println(dataVoltage);
 dataFile.close();
 Serial.println(dataVoltage);
 }

I couldn't create any files on the SD card, and I really don't know why. Maybe I'm using pointers wrong, but I've seen some similar stuff on other examples and can't figure out what's wrong.
Do you guys have any idea why? :~
Thanks for answers btw!

I think I fixed it, here is the code

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
  int n = 0;
  snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
  while(SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }
  Serial.println(n);
  Serial.println(filename);
  //now filename[] contains the name of a file that doesn't exist
  
  File dataFile = SD.open(filename, FILE_WRITE);
  

  }
void loop()
  {
 }

The problem is that I can't use "filename" in the loop, and if I use the functions inside of the loop instead of the setup, it creates a lot of files because it's inside of the loop.
Is there any way of using the variable of one function inside of other?

yes, move the variable name to the top above the setup loop

Thanks Jasit, it worked.
I'm posting the code in case anyone need it

#include <SD.h>

const int chipSelect = 10;
char filename[16];

void setup()
  {
    Serial.begin(9600);  // Open serial communications and wait for port to open:
  
  


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  //char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
  int n = 0;
  snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
  while(SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }
  File dataFile = SD.open(filename,FILE_READ);
  Serial.println(n);
  Serial.println(filename);
  dataFile.close();
  //now filename[] contains the name of a file that doesn't exist
  }
void loop()
  {
    File dataFile = SD.open(filename, FILE_WRITE);
    if (dataFile) {
    int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0/1023.0);
    Serial.println(voltage);
    dataFile.println("teste");
    dataFile.println(voltage);
    dataFile.close();
    }
    else {
      Serial.println("error");
 }
  }