Hi there,
I'm working on a school project which involves a Hall Effect Sensor measuring magnetic fields to calculate RPM of a bike wheel. Currently I'm attempting to write the code for "dynamic logging" (essentially, checking to see if the file already exists before writing to it, and if so, incrementing the file number to create a new file). I also only want to create one file at a time (i.e. a new file is only created when my Arduino board is turned on/reset, I don't want multiple files within the same trial - just multiple data points for the same trial).
The main problem I'm encountering is that it will run through void loop() without triggering any of the error messages, but doesn't actually output the desired serial messages (charfileName). It also just isn't creating the .txt file on my SD card when I go to check it later, BUT, it is outputting to the serial monitor "IT'S WORKING". Any help would be greatly appreciated!
-Jasmine
//Globals//
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
unsigned int FileNumber = 1;
char charfileName[100];
File CyclometerData; // Data object sensor data is written to
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
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:
return;
}
else {
Serial.println("Card initialized");
}
}
void loop()
{
while (!FileNumber == 0) {
String fileName = String();
fileName = "Cyclometer Trial ";
fileName += FileNumber;
fileName += ".txt";
// fileName.toCharArray(charfileName, sizeof(charfileName));
if (SD.exists(charfileName)) {
FileNumber++; //add one to FileNumber
Serial.println(FileNumber);
}
else {
// CyclometerData=SD.open(charfileName, FILE_WRITE);
Serial.println(charfileName);
FileNumber = 0;
}
}
File CyclometerData = SD.open(charfileName, FILE_WRITE);
if (CyclometerData) // if the file can be found, write to it
{
CyclometerData.println("IT'S WORKING"); //data points will go here in final code
CyclometerData.close();
Serial.println("IT'S WORKING"); // writes to the computer too
}
else {
// if CyclometerData is not found
Serial.println("Couldn't access file");
}
}