Automatic txt file generate

Dear all

I am doing one project, in my project i want record analog sensor data in .txt file. but I want, when I run my code it automatically generates a text file and opens that file writes data on this file and after 50000 raws it closed that file and automatic open new file. and it also generates new text file when i reset my program.

Although the OP in this thread seems to have a power-related problem, his code there does actually work (I tested it :wink: ) and it may get you started. His sketch creates a new file test.txt on each reset, and that’s part of what you want to do.

Have you tried any code yourself, so far?

Some ideas to experiment with

unsigned long fileNumber = 0;
char fileName[13];  //allow for 8.3 filename plus zero termination

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  sprintf(fileName, "%08u.txt", fileNumber);  //create the filename
  Serial.print("\ncurrent filename is : ");
  Serial.println(fileName);
  //open the file for writing here
  for (unsigned long readingCount = 0; readingCount < 5000; readingCount++)
  {
    if (!(readingCount % 1000) && readingCount > 0) //progress report
    {
      Serial.print("reading count : ");
      Serial.println(readingCount);
    }
    //take sensorreading and save to file here
  }
  //close the file here
  fileNumber++;
  delay(2000);
}

In order to restart with a new filename after a reset you need to save fileNumber to EEPROM after incrementing it, read it in setup() and increment it again

Sir, I use this code but this code depends on delay if we increase or decrease delay new file generation rate also increase or decrease.

The code that was provided to you was meant to show you how you could do it.

If you do not like the delay remove it and replace it with non blocking code.

amitshishodia:
Sir, I use this code but this code depends on delay if we increase or decrease delay new file generation rate also increase or decrease.

I only used delay() to stop the Serial output whizzing past too fast to read.

The delay() is not part of the filename creation, which is what I thought you were interested in. By all means remove the delay() if you want to

Sir, I have developed some code with your help, In this code, automatic new txt file creates, write and close and save that txt file in SD card. But when I reset my program it again generates the same file and overwrites the data.

So, I want when i reset/restart my program it not overwrite data it creates new file just after my existing file.

int count = 0 ; 
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
unsigned long fileNumber = 0;
char fileName[13];  //allow for 8.3 filename plus zero termination
const int chipSelect = 4;
void setup()
{
  Serial.begin(115200);
  Serial.print("Initializing SD card...");

  // 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:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop()
{
  int count1 = count++ ;
  int count2 = count1 % 100 ;
  Serial.println(count1);
  Serial.println(count2);
  Serial.println("");

  if (count2 == 0)
  {
  sprintf(fileName, "%08u.txt", fileNumber);  //create the filename
  Serial.print("\ncurrent filename is : ");
  Serial.println(fileName);
  
  fileNumber++ ;
   
    }
  }
  
  
    File dataFile = SD.open(fileName, FILE_WRITE);
  if (dataFile) {
    dataFile.println(count1);
    dataFile.close();
  
  //open the file for writing here
  /*for (unsigned long readingCount = 0; readingCount < 5000; readingCount++)
  {
    if (!(readingCount % 1) && readingCount > 0) //progress report
    File dataFile = SD.open("%08u.txt", FILE_WRITE);
    if (dataFile) {
    dataFile.println(readingCount);
    dataFile.close();
    {
      Serial.print("reading count : ");
      Serial.println(readingCount);*/
     delay(10);
    }
    //take sensorreading and save to file here
  
  //close the file here
}

amitshishodia:
But when I reset my program it again generates the same file and overwrites the data.

UKHeliBob:
In order to restart with a new filename after a reset you need to save fileNumber to EEPROM after incrementing it, read it in setup() and increment it again

Alternatively, did you look at the code I linked in #1 to see how another member tackled the same problem?

I want when i reset/restart my program it not overwrite data it creates new file just after my existing file.

Read what I wrote more carefully

amitshishodia:
But when I reset my program it again generates the same file and overwrites the data.

So, I want when i reset/restart my program it not overwrite data it creates new file just after my existing file.

Then before creating a file, check whether it exists already. If so, and if <50,000 bytes, open it in append mode and continue writing. Or simply open a new one, with a non-existing name.