New .txt file every time Arduino is switched on

Hi all,

I'm working on a frame loss logger for Futaba SBUS systems, and I have written the code for it and it works fine.

However, I'd like to have a new .txt file onto the SD card for every single flight. My code is below:

#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include "sbus.h"
#include <Embedded_Template_Library.h>
File myFile;

int holdCount = 0;

#define I2C_ADDRESS 0x3C
#define RST_PIN -1

SSD1306AsciiAvrI2c oled;



SbusRx sbus_rx(&Serial);
SbusTx sbus_tx(&Serial);
void setup() {

  Serial.begin(115200);
  while (!Serial) {}

  sbus_rx.Begin();
  sbus_tx.Begin(); // starting sbus communication


  oled.begin(&Adafruit128x32, I2C_ADDRESS);
  oled.setFont(Adafruit5x7);
  oled.clear();
  oled.print("   DPVK Systems FLC");
  delay(4000);
  oled.clear();


}


void loop() {

  if (sbus_rx.Read()) {
    for (int i = 0; i < sbus_rx.rx_channels().size(); i++) {
      Serial.print(sbus_rx.rx_channels()[i]);
      Serial.print("\t");


    }
  }

  while (sbus_rx.failsafe()) {
    holdCount = holdCount + 1;
  }


  oled.print("Lost frames: ");
  oled.println(sbus_rx.lost_frame());
  oled.print("\t");
  Serial.println("Lost frames: ");
  Serial.println(sbus_rx.lost_frame());


  oled.print("Holds: ");
  oled.println(holdCount);
  Serial.println("Holds: ");
  Serial.println(holdCount);


  myFile = SD.open("DPVK Systems.txt", FILE_WRITE);
  myFile.write("Lost frames: ");
  myFile.write(sbus_rx.lost_frame());
  myFile.write("holds: ");
  myFile.write(holdCount);

}

Hope the above is possible. Many thanks in advance.

File names on SD cards need to adhere to the 8.3 format; yours doesn't.

The below shows how you can generate a filename between 00000000.txt and 99999999.txt. I unfortunately don't have an SD card to complete the code and test.

// the filename
char filename[13];

void setup()
{
  Serial.begin(57600);
  while(!Serial);
  
  for (uint32_t cnt = 0; cnt <= 99999999; cnt++)
  {
    sprintf(filename, "%08lu.txt",cnt);
    Serial.println(filename);

    // test if file exist using https://www.arduino.cc/en/Reference/SDexists
    // use break to abort the for-loop once you have found a file that does not exist
  }
}

void loop()
{
}

okay, understood. TEXTFILE.TXT should work I assume.

Can second and onwards files be named TEXTFILE_1.txt and so on and so forth? is there a way to do that? Would be easier than the number file naming system.

But that does not adhere to the 8.3 format :smiley:

// the filename
char filename[13];
const char basename[] = "file__";
void setup()
{
  // indicates if a new filename was found
  bool newfile = false;
  
  Serial.begin(57600);
  while (!Serial);

  int maxnumber = 1;
  for (uint8_t cnt = 0; cnt < 8 - strlen(basename); cnt++)
  {
    maxnumber *= 10;
  }
  maxnumber -= 1;
  Serial.print("maxnumber = "); Serial.println(maxnumber);

  uint8_t maxdigits = 8 - strlen(basename);
  Serial.print("maxdigits = "); Serial.println(maxdigits);

  char format[15];
  snprintf(format, sizeof(format), "%%s%%0%uu.txt", maxdigits);
  Serial.print("format string = "); Serial.println(format);

  for (uint16_t cnt = 0; cnt <= maxnumber; cnt++)
  {
    snprintf(filename, sizeof(filename), format, basename, cnt);
    Serial.println(filename);

    // test if file exist using https://www.arduino.cc/en/Reference/SDexists
    // set newfile to true if a non-existing file was found
    // use break to abort the for-loop once you have found a file that does not exist
  }

  if (newfile == false)
  {
    Serial.println("Could not find an none-existing file");
    for (;;);
  }
  else
  {
    Serial.print("New file is '");
    Serial.print(filename);
    Serial.println("'");
  }
}

void loop()
{
}

You can basically use anything for the basename.

Personal opinion, but it would be a waste if you want 'unlimted' files :wink:

Oh yes, didn't realise. I think the number format is the way to go for less headache.

Correct, I agree.

Where can I integrate this into my code? wherein the file is named and created correct?

@nightfury_11, as @sterretje says:

These characters can be whatever letters and digits (and a few other printable chars) you like. File extensions generally describe the type of file, so .txt for a text file, .xls for Excel spreadsheet etc.

You could name your files TEXTFILE.001, TEXTFILE.002 etc. To could also use LOG00001.TXT, LOG00002.TXT etc.

EDIT: I should have been quicker at typing :grinning:

Oh okay, I'll name it LOGFILE00001.txt and onwards.

About the code integration? I'm not familiar with the SD library so not show how and where to paste the code.

Lol, I checked the post just as it was submitted.

That's not 8.3 format ....

Sorry, my bad. I meant LOG0001.txt

how can I put this above code into my code?

Not familiar with the SD library hence not very sure of how to go about it.

I can take an educated guess of it being where I have opened the file in my code, but I'm not too sure.

How do you determine that? If it's on a power-on / reset, you can put it in setup().

Now we finally know what you want to call your files, we don't have to go through most of the complicated stuff in my second eample;

// the filename
char filename[13];

void setup()
{
  // indicates if a new filename was found
  bool newfile = false;

  Serial.begin(57600);
  while (!Serial);

  for (uint16_t cnt = 0; cnt <= 9999; cnt++)
  {
    snprintf(filename, sizeof(filename), "LOG%04u.txt", cnt);
    Serial.println(filename);

    // test if file exist using https://www.arduino.cc/en/Reference/SDexists
    // set newfile to true if a non-existing file was found
    // use break to abort the for-loop once you have found a file that does not exist
  }

  if (newfile == false)
  {
    Serial.println("Could not find an non-existing file");
    for (;;);
  }
  else
  {
    Serial.print("New file is '");
    Serial.print(filename);
    Serial.println("'");
  }
}

void loop()
{
}

The Files example in the SD library has a few lines relating to SD.exists().

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

Yes, a power on is when I want to create a new file. Will put it into setup.

I need to paste your above longer code into setup correct?

The code in post #11 will do.

Okay, I'll add it in setup.

Many thanks for the help! Much appreciated.