Go Down

Topic: Increment Files with SD (Read 281 times) previous topic - next topic

mschindl

I am using an Arduino and microSD card to create a new file in a directory every time the Arduino powers on.

My code is as follows:
Code: [Select]
    if (SDDetect) {
      //// Count the amount of Files in the LogFiles Directory ////
      countFile = SD.open("LogFiles/");
      while (true) {
        File entry =  countFile.openNextFile();
        if (! entry) {
          break;
        }
        fileCount++;
      }
      fileCount++; //Add one to fileCount to creat the next file

      //// Create a new file ////
      String FileName =  String("LogFiles/Log0") + fileCount + String(".csv");
     
      logFile = SD.open(FileName, FILE_WRITE);
      logFile.println("Header1, Header2, Header3, Header4, Header5, Header6, Header7, Header8");
    }


This code works great the first time I run the code! Creates the file Log01.csv in the LogFiles directory, and writes the header to the CSV. I turn the Arudino off and turn it back on, and it creates another file Log02.csv and writes the header.

The issue is when I create a 3rd file. It creates Log03.csv and then freezes. I open up the SD card on my computer and it creates the file but did not write the header. If I delete the files in the directory and start again the same thing happens. Works great the first two times and then freezes on the 3rd. Any ideas as to why this is happening? Or is there any easier way to do what I am trying to achieve?

wildbill

OpenNextFile does indeed open the file, which consumes RAM, so when you have enough open, you run out. You need a corresponding close.

mschindl

I added a close function to the while loop.

Code: [Select]
      //// Count the amount of Files in the LogFiles Directory ////
      countFile = SD.open("LogFiles/");
      while (true) {
        File entry =  countFile.openNextFile();
        if (! entry) {
          break;
        }
        entry.close();
        fileCount++;
      }
      fileCount++; //Add one to fileCount to creat the next file

      //// Create a new file ////
      String FileName =  String("LogFiles/Log0") + fileCount + String(".csv");

      logFile = SD.open(FileName, FILE_WRITE);
      logFile.println("Header1, Header2, Header3, Header4, Header5, Header6, Header7, Header8");


This code does work and creates files with headers every time I turn the Arudino on. But now I am having the issue when there are more than two files it takes an incredibly long time to create another file and write the header. About 10 seconds.

Go Up