Data logger: Incrementing filenames

Hello everyone,

I am using an Uno to collect data. I want to be able to write this data to an SD card and have the name of the file to have a number associated with the test after each power cycle. (Power on: test_001. Power off then power on again: test_002 etc) I found some code on a SparkFun forum that does this pretty well and I have been able to modify it successfully… for the most part. I’m wanting to name the files with initials, a four digit number, an underscore, then the 2 digit trial number, for example: XX1234_00

The code throws an error if my file name exceeds 6 characters. I can remove one letter, one number or the underscore and the code works fine. Is there a quick fix to allow a longer file name? I have already tried increasing “char filename[16]” to [32] [64] etc. but no luck.

I will include the original code as well as what I have modified. Any help would be much appreciated!

Original:

#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");
 }
  }

Modified:

#include <SD.h>

//initialize pins for SD shield
const int chipSelect = 10;

// make it long enough to hold your longest file name, plus a null terminator
char filename[16];

//initialize pins for sensors
int sensor1Pin = 0;
int sensor2Pin = 1;
int sensor3Pin = 2;
int sensor4Pin = 3;

//manually input desired filename here before each test
char *s = "TR3035_";


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.");
  int n = 0;  
  snprintf(filename, sizeof(filename), "%s%02d.txt", s, n); // includes a two-digit sequence number in the file name
  while(SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "%s%02d.txt", s, 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) {
      float sensorVal[] = {0, 0, 0, 0};
      
      //read sensor values
      sensorVal[0] = analogRead(sensor1Pin);
      sensorVal[1] = analogRead(sensor2Pin);
      sensorVal[2] = analogRead(sensor3Pin);
      sensorVal[3] = analogRead(sensor4Pin);

      //print values to the serial monitor
      Serial.print(sensorVal[0]);
      Serial.print(",");
      Serial.print(sensorVal[1]);
      Serial.print(",");
      Serial.print(sensorVal[2]);
      Serial.print(",");
      Serial.println(sensorVal[3]);

      //print values to data file
      dataFile.print(sensorVal[0]);
      dataFile.print(",");
      dataFile.print(sensorVal[1]);
      dataFile.print(",");
      dataFile.print(sensorVal[2]);
      dataFile.print(",");
      dataFile.println(sensorVal[3]);
      dataFile.close();
    }
    else {
      Serial.println("error");
 }
 delay(10);
  }

a four digit number, an underscore, then the 2 digit trial number, for example: XX1234_00

That is a 9 character filename. Is that allowed when using the SD library ?

You could use the extension

XX1234_00.txt -> XX1234.T00

HeliBob, you are correct about the SD library. I'm still pretty new to programming so sorry for the beginner oversight.

Whandall, I'm not quite sure what you mean by using the extension. Could you elaborate?

Thanks again

The file does not have to be named XX1234_00**.txt** it could be named XX1234**.T00**.

You have 8 characters for the base-name and 3 characters for the extension.

Lose the underscore? Then the name will be 8.3