Create a new data.csv file each time the arduino is reloaded.

Hello, I’m new to Arduino and I need your help if possible. Thank you
I have just developed a datalogger to detect door openings and record them on a .CSV file (data.csv) in a micro sd card.

I use a micro sd module and a magnetic door opening sensor.

If possible, I would like to create a new data.csv file each time I restart the arduino.

Have a new file for each time I restart the arduino.

Example: :

Here’s the code:

// SSD Lib
#include <SPI.h>
#include <SD.h>
#define CS_PIN 10
File myFile;

int compt = 0;

void setup() {

var = 0;

  compt = compt+1; // 

Serial.print("SD card initialization in progress...");

   if (!SD.begin()) {
    Serial.println("SD card initialization failed!"); 
    Serial.println("The initialization of the SD card was successful!");
    myFile="data(compt).csv", FILE_WRITE);
    if (myFile) {   
 myFile.println("Open,Time Open,Time Close,Time,Cumulative Time,%");

void loop() {
  // put your main code here, to run repeatedly:


Thank you very much for helping me

1 Like

Somewhere deep down in that mysterious loop, there might be an RTC. If so you might find using the time or that date as a sensible way to go about this.

Have you had a look at Sparkfun's Openlog?

It's a mega328 avr that logs data to a micro sd card. The code for openlog is freely available. It normally logs data from a serial port but you could modify the code to read your magnetic sensor.

It creates a new file each time power is applied.