SD card module help

Hi, just getting started using an SD card module. It works like this so far, you start with the switch off, plug the Arduino in and when I turn the switch on it will begin writing information to the SD card. When I turn it back off It just writes to the serial monitor that it is done.

I want it so when I turn it back on again it will now write to a new txt file, I need a way to change the name of the file every time the switch turns off.

#include <SPI.h>
#include <SD.h>
int buttonState = 0;
File myFile;
const int buttonPin = 2;
boolean check = true;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed! Check wiring.");
    return;
  }
  Serial.println(" initialization complete.");
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
    check = false;
    delay(1000);

    myFile = SD.open("dump.txt", FILE_WRITE);

    if (myFile) 
    {
      myFile.println("testing");
      // close the file:
      myFile.close();
    } else 
     {
      Serial.println("error opening test.txt");
     }
 

  } else  
  {
     if (check == false) {
    Serial.println("Done.");
    check = true;
     }
  }
}

If it is the Arduino that is being switched on and off then you need a way to detect that it has happened and create a new file. The setup() function only runs at startup or reset so if code in there runs the Arduino has been powered up or reset.

If you save the name of the current file to either the SD card or EEPROM when you create it then you could read it in setup(), open a new file and save its name to the card or EEPROM again ready for the next restart.

Hi sorry I didn't specify, I am not turning the Arduino off its just the switch. I tried creating a variable which will be the name of the file and every time the button is in the off state it will add 1 to it but i'm just unsure how to put it in the file name:

I tried it like this but I know it doesn't work.

myFile = SD.open(fileNumber + ".txt", FILE_WRITE);

You can use C string (char array) and the strcat() function from the string.h library to add a string to another.

You can use the stdio.h library sprintf() function to make a full string using formatting (makes a lot of code size).

You can use the stdlib.h library itoa() function to turn integers to ascii text, but it says this one is not as standard C somehow.

AVR LibC string.h doc page: avr-libc: <string.h>: Strings
AVR LibC stdio.h doc page: avr-libc: <stdio.h>: Standard IO facilities
AVR LibC stdlib.h doc page: avr-libc: <stdlib.h>: General utilities

These are standard C libraries, web pages on C language will cover them.

SD FAT supports 8.3 filenames, up to 8 chars name, period, up to 3 chars extension.
Name can be a letter or two followed by date with extent being 0 to 999 for files that day.

Thank you for the reply I will try it out do you have a link for the string.h library I can't seem to find it.

The string library is installed by default. You need to take not action to use its functions

See C string functions