Want to make new files in the SD card

So I’m working on a project involving a microphone that collects the values of sound pressure. I want to collect this values in a SD card but I also want to make different files of values using a button that closes a file and opens a new file.

I’m stucked here because I don’t know how to change the name of the file so I can make different files each time I use the button.

The code I’m posting here is just a code involving the data logger and SD card, it doesn’t have anything to do with the bigger code I’m working with but it is what I need to fix.

If someone could help me I would really appreciate it.

Here is the code:

#include <SD.h>

#include <SPI.h>

#define CSpin 10

File archivo;



void setup() {

  pinMode(9, INPUT_PULLUP);

  Serial.begin(9600);

  Serial.println("Inicializando tarjeta...");

  if (!SD.begin(CSpin)) {

    Serial.println("Fallo en la inicialización...");

    return;

  }

  Serial.println("Inicialización correcta");

  archivo = SD.open("data.txt", FILE_WRITE);

  

}

void loop() {

int h = 0; 

int estbot = digitalRead(9);

if (estbot == 1){

  for (int h=0; h<1; h++){

    delay(2000);

    archivo.close();

    archivo = SD.open("data.txt", FILE_WRITE);

  }  

}

if (archivo) {

    archivo.println("Probando 1, 2, 3");

    Serial.println("Escribiendo...");

    Serial.println("Escritura correcta");

    delay(500);

  } else {

    Serial.println("Error apertura y escritura");

  }

}

So I’m working on a project involving a microphone that collects the values of sound pressure. I want to collect this values in a SD card but I also want to make different files of values using a button that closes a file and opens a new file.

I’m stucked here because I don’t know how to change the name of the file so I can make different files each time I use the button.

The code I’m posting here is just a code involving the data logger and SD card, it doesn’t have anything to do with the bigger code I’m working with but it is what I need to fix.

If someone could help me I would really appreciate it.

Here is the code:

#include <SD.h>
#include <SPI.h>
#define CSpin 10
File archivo;

void setup() {
  pinMode(9, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Inicializando tarjeta...");
  if (!SD.begin(CSpin)) {
    Serial.println("Fallo en la inicialización...");
    return;
  }
  Serial.println("Inicialización correcta");
  archivo = SD.open("data.txt", FILE_WRITE);
  
}
void loop() {
int h = 0;
int estbot = digitalRead(9);
if (estbot == 1){
  for (int h=0; h<1; h++){
    delay(2000);
    archivo.close();
    archivo = SD.open("data.txt", FILE_WRITE);
  }  
}
if (archivo) {
    archivo.println("Probando 1, 2, 3");
    Serial.println("Escribiendo...");
    Serial.println("Escritura correcta");
    delay(500);
  } else {
    Serial.println("Error apertura y escritura");
  }
}

archivo = SD.open("data.txt", FILE_WRITE);You can call the file whatever you like, this means you can close this file (data.txt) and open a new one for writing, opening a file that does not yet exists for writing creates it.

This should help you on the way

void loop()
{
  static int fileCounter;
  char fileName[13];

  snprintf(fileName, sizeof(fileName), "%05d.txt", fileCounter);
  archivo = SD.open(fileName, FILE_WRITE);

  ...
  ...

}

The snprintf() will create a file name with 5 digits (00000.txt, 00001.txt, …) if you increment the file counter. That file name can be used in the call to open.

Note that there is no checking if a file already exist; after a restart of the Arduino, the counter will start from 0 again and if a file 00000.txt already exists, it will be overwritten or data will be appended (not sure, not very familiar with SD cards).

If you want a new filename every time button is pressed try something like this

const byte buttonPin = A3;
byte currentButtonState;
byte previousButtonState;
byte fileCount;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.println();
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW) //button has become pressed
  {
    char fileName[13] = "file";
    char buffer[4];
    itoa(fileCount, buffer, 10);
    strcat(fileName, buffer );
    strcat(fileName, ".txt");
    Serial.print("the filename is now ");
    Serial.println(fileName);
    fileCount++;
  }
  previousButtonState = currentButtonState;
}

@jorgehacker, do not cross-post. Threads merged.

sterretje:
This should help you on the way

void loop()

{
 static int fileCounter;
 char fileName[13];

snprintf(fileName, sizeof(fileName), “%05d.txt”, fileCounter);
 archivo = SD.open(fileName, FILE_WRITE);


 …

}



The [snprintf()](https://linux.die.net/man/3/snprintf) will create a file name with 5 digits (00000.txt, 00001.txt, ...) if you increment the file counter. That file name can be used in the call to open.

Note that there is no checking if a file already exist; after a restart of the Arduino, the counter will start from 0 again and if a file 00000.txt already exists, it will be overwritten or data will be appended (not sure, not very familiar with SD cards).

Ok so I made this code using what you said and it worked!!, but I was wondering if I could put the variable fileCounter into the eeprom memory, I’ve done some research I’ve seen that I can mess it up with this memory because printing is limited, so if you could help me with this I would appreciate it!
Here is the new code:

#include <SD.h>
#include <SPI.h>
#define CSpin 10
File archivo;
void setup() {
  int t = 0;
  pinMode(9, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Inicializando tarjeta...");
  if (!SD.begin(CSpin)) {
    Serial.println("Fallo en la inicialización...");
    return;
  }
  Serial.println("Inicialización correcta");
  archivo = SD.open("data.txt", FILE_WRITE);
}
void loop() {
  static int fileCounter;
  char fileName[13];
  int h = 0;
  int estbot = digitalRead(9);
  if (estbot == 1) {
    for (int h = 0; h < 1; h++) {
      archivo.close();
      delay(2000);
      fileCounter++;
      snprintf(fileName, sizeof(fileName), "%05d.txt", fileCounter);
      archivo = SD.open(fileName, FILE_WRITE);
    }
  }
  if (archivo) {
    int t;
    archivo.println(t);
    Serial.println("Escribiendo...");
    Serial.println("Escritura correcta");
    delay(500);
    t++;
  } else {
    Serial.println("Error apertura y escritura");
  }
}

You can; you can update the number 10,000 times; if you need more, you can read up on eeprom wear leveling.

An other solution is to just get a directory listing of the SD card when the program starts, find the highest number in the file names and use that number (incremented by 1) to create the first file name for the new run.

sterretje: You can; you can update the number 10,000 times; if you need more, you can read up on eeprom wear leveling.

An other solution is to just get a directory listing of the SD card when the program starts, find the highest number in the file names and use that number (incremented by 1) to create the first file name for the new run.

Ok thanks I will try that, but now I have another problem, I uploaded this program to the arduino nano where I'm doing all the bigger program but it doesn't work there, and it is the same code that I sent before, any idea why? Apparently the error is in the first part, in the setup, because it says "Fallo en la inicialización...", but I don't know why. I think all is connected correctly, but it is true that now the connections are different, before I used the protoboard now the connections with the nano are by wire. Please I really appreciate any idea because this is really frustrating and I'm not very familiarized with arduino.