Hi all,
I'm pretty much a beginner to Arduino coding, and I'm trying to get a very simple proof of concept program working to upscale to an accelerometer datalogger. I am combining the SD example library and my own code to do this, using an Arduino MEGA 2560.
Basically, the function I want for this program is:
-
Press the button once and a new file of name "Session_XX.txt" is made on the card, where XX is an incrementing integer
-
A simple 'everything's working ok' line of text saved within the file along the lines of "Test complete"
I have used the sprintf() command to create a variable string and assigned it to a character array for the SD.open() function to use. The character array works fine, producing a file name that increments with every button press. The problem seems to lie with the SD.open() command not creating a new file with this char array as its name. Can anyone help with this? When I change to a definite file name, eg SD.open("New_File.txt", FILE_WRITE); it creates a file with this name and saves everything ok, so it shows the problem is with the char array.
The picture I have attached shows the SerialMonitor when the program is run and the button is pressed a few times. This proves that the char array increments as I want. It doesn't show anything about the dataFile being present, because the if statement looks for the presence of "NewFile_name" not the "dataFile". When the if statement is changed to see if "dataFile" has been created, it shows that there has been an error.
I have read many of the posts on here already that deal with variable filenames. It seems that people are having the same problems, then advised to use sprintf() and it somehow works for them.
I'm sure I'm missing something simple, but I don't have enough experience to know exactly what. If anyone can take the time to point me in the right direction, that'd be really appreciated. I'm in a bit of a fix here.
My entire code is shown below to give you an idea of what I'm dealing with
Many thanks
CR
#include <Debounce.h> // Extra library included to smooth spikes when the button has been pressed
#define SWITCH 3 //Define Input pin for switch
#include <SPI.h> //For SD Card Datalogging
#include <SD.h>
Debounce debouncer = Debounce( 20 , SWITCH );
int switch_state = 0;
int record_session = 0; //Integers to do with button switch
int session_num = 1; // Counter corresponds with number of file
File dataFile; // File for SD to save to
char NewFile_name[] = ""; //Array of variable length that holds the name of the file. NOTE: EVEN IF THE SIZE OF THE ARRAY IS DEFINED AND LARGER THAN THE CONTENTS BY 1, IT DOESN'T WORK
void setup() {
pinMode(SWITCH,INPUT);
Serial.begin(115200);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SS, OUTPUT);
if (!SD.begin(10,11,12,13)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop() {
if ( debouncer.update ( ) ) {// Debounce routine looks for when the button state has changed
if (debouncer.read() == 1){
if (record_session == 1){
record_session = 0;
Serial.println("Stopped recording");
}
else {
record_session = 1;
sprintf (NewFile_name, "Session_%02d.txt", session_num); //assign NewFile_name array the text "Session_XX.txt" with the session number integer as XX
dataFile = SD.open(NewFile_name, FILE_WRITE); //**PROBLEM HERE FOR SOME REASON**. Assign dataFile as a new file (create new one because no current name exists) of name NewFile_name
if(NewFile_name) { // Check that NewFile_name array is correct, have changed this to if(dataFile) and this throws problems because no file has been made
Serial.println ("File Created, saving data");
Serial.println(NewFile_name);
dataFile.println("Testing complete. Congrats"); //DOES NOT OCCUR BECAUSE FILE HAS NOT BEEN MADE. Intended to save a few lines in the file
dataFile.println("222");
dataFile.close();
}
session_num++; //increment session_num counter
if (! NewFile_name) {
Serial.println("error with NewFile_name");
// Wait forever since we cant write data
while (1) ;
}
Serial.println("recording");
}
}
}
}