Dynamic Logging & Writing to SD Card - Help Needed

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");

}
}

If you want one file per reset, why not do that in setup()?

I don't know why your code doesn't work but you could easily make a separate copy (Save As...) and then strip out all the convoluted logic in there and just make sure you can actually write to your SD card successfully. In about 5 lines of code, I suspect!

I say this because I've had the situation where some SD cards are just not accepted by the Arduino SD shield or SD module - no, haven't a clue why this might be so, it is one of the great mysteries in life. They appear to work ok but don't actually contain any data. Bizarre, I know.

Additionally, and this is more a superstition thing with me, I never use SD.print I always use SD.write even though both methods are defined by the underlying, base Print class - it just feels wrong to 'print' to an SD card. Maybe there really are differences (now) that I'm unaware of. Who knows?

If you would like to look at a YouTube video that explains the basics of writing to an SD card, then it's video #22 on my channel (URL in signature below) - some very simple code which has always worked for me. :slight_smile:

Did you format the SD card with an SD formatter?
Did you format the SD card as DOSFAT?
Did you name the file using DOS 8.3 naming?

Trying to write code without reading the docs is like posting code here without using code tags.

Ralph_S_Bacon:
I say this because I've had the situation where some SD cards are just not accepted by the Arduino SD shield or SD module - no, haven't a clue why this might be so, it is one of the great mysteries in life. They appear to work ok but don't actually contain any data. Bizarre, I know.

Additionally, and this is more a superstition thing with me, I never use SD.print I always use SD.write even though both methods are defined by the underlying, base Print class - it just feels wrong to 'print' to an SD card. Maybe there really are differences (now) that I'm unaware of. Who knows?

Do you check the return from the write operation? It should return the number of bytes actually written.

The READWRITE example doesn't check. Maybe the effort of writing an error handler was just too much or they decided it would just confuse new people. There's a LOT of that in the examples.

Always close the file when you're done. If you don't then the FAT (file allocation table) on the SD card (or other writable media drive) will not get updated and your data will be gone.

In the case of data logging, close the file periodically and open it again.

SD.open(filepath, mode)

  • FILE_WRITE: open the file for reading and writing, starting at the end of the file.

jasmineshaw:
I don't want multiple files within the same trial - just multiple data points for the same trial).

Open for write appends data to the file. Whenever it starts up, have it open the file and write a line saying "Start". It would also be good to time-stamp the start of every line.