Hello all,
I am trying to create a simple code that will display data to a .txt file on a microSD card. Ultimately I plan on turning on and off the Arduino and would like it to make a new file with a new name each time, instead of appending it to the end of one file.
I tried to have it make a file that is just a number so that it can read the number from the file, make the respective new file, and update the number so that it may be used for next time the Arduino is on.
I am attaching my code:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
float fev = 2.5;
float fvc = 3.0;
const char* versionFileName = "VersionFile.txt";
File myFile; // Define File object for the main file
File versionFile; // Define File object for the version file
void setup() {
Serial.begin(9600);
if (SD.begin(chipSelect)) {
if (SD.exists(versionFileName)) {
// File exists, read old log number
versionFile = SD.open(versionFileName, FILE_READ); // Assign the versionFile object
int oldLogNum = versionFile.parseInt();
versionFile.close();
// Calculate new log number
int newLogNum = oldLogNum + 1;
String SEQCOUNT;
if (newLogNum < 10) {
SEQCOUNT = "00" + String(newLogNum);
} else if (newLogNum < 100) {
SEQCOUNT = "0" + String(newLogNum);
} else {
SEQCOUNT = String(newLogNum);
}
// Write new log number to file
versionFile = SD.open(versionFileName, FILE_WRITE); // Reusing versionFile
versionFile.print(newLogNum);
versionFile.close();
Serial.println("File was Updated.");
String fileName = "Trial" + SEQCOUNT + ".txt";
myFile = SD.open(fileName, FILE_WRITE); // Assign the myFile object
myFile.println("Name: John Doe");
myFile.println("Age: 22");
myFile.print("FEV1 = ");
myFile.println(fev, 3);
myFile.print("FVC = ");
myFile.println(fvc, 3);
myFile.print("Ratio = ");
myFile.println(fev/fvc, 3);
myFile.close();
} else {
// File doesn't exist, assume first run
versionFile = SD.open(versionFileName, FILE_WRITE); // Assign the versionFile object
versionFile.print("1");
versionFile.close();
Serial.println("File was Created.");
String fileName = "Trial001.txt";
myFile = SD.open(fileName, FILE_WRITE); // Assign the myFile object
myFile.println("Name: John Doe");
myFile.println("Age: 22");
myFile.print("FEV1 = ");
myFile.println(fev, 3);
myFile.print("FVC = ");
myFile.println(fvc, 3);
myFile.print("Ratio = ");
myFile.println(fev/fvc, 3);
myFile.close();
}
} else {
Serial.println("Error initializing SD card.");
}
}
void loop() {
// Your main program logic here
}
I am not even seeing anything come up on the serial monitor, but it is making a file called "Trial001.txt," and every time I re-upload the code, it will just append to the same file, not make a new file, nor is there even a "VersionFile.txt" on the microSD card. I have also tried creating a file called VersionFile.txt and putting a 1 in it, but it never is able to read or create that file.
I'm not sure if this is a hardware issue, since it is still able to create files and write to them, just not make a version file with the number.