I have been trying to convert a project from a Mega 2560 to ESP32 which uses an SD card. I have had to change the way that I use the SD card to using the FS.h library. The FS.h library examples all use functions in the same way. The append function is below.
void appendFile(fs::FS& fs, const char* path, const char* message) {
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message)) {
} else {
Serial.println("Append failed");
}
file.close();
}
Using this function, only char strings can be sent to the SD card. Can you send other variables? I have gotten round this by converting all my variables to char strings.
I want to do something which seems like it should be quite simple. Something like this,
File file = fs.open(path, FILE_APPEND);
if (file.print(char date)) {}
if (file.print(int temp)) {}
if (file.print(float humidity)) {}
file.close();
This doesn’t work because the syntax is all wrong, I assume that it is to do with fs::FS& fs. The problem is that despite spending hours searching, I can’t find information on the syntax rules to be used with the FS.h.
If I could unpick the library file it may be obvious, but I am still new to Arduino’s and don’t have that skill yet.
I would be really grateful if somebody could point me in the direction of a syntax breakdown for the FS.h library.
I had wondered if I could do that. But isn't that contrary to the basic idea of functions?
If you have to create a new function for every variable type, won't this make the sketch more complex rather than simplifying it?
it depends if you are the user of the function or the developer.
Overloading a function to offer multiple behaviours whilst presenting a unified programming interface is common. Just look at the Print class for example and you'll see they have prototypes for all sorts of parameters
(and the same for println())
that's how the variable behavior is implemented based on parameters' types.
the user only knows that s/he can call print() (possibly with an optional parameter sometimes) and the right thing will happen.
the FileSystem knows how to leverage print(). The reason you feel you need multiple functions is because you want to embed in the function the opening and closing of the file. if you have multiple data to add, that's usually not a good idea.
You'd be better off to
1/ open the file for append
2/ use the standard print to write all the stuff you want
At the moment too much information is going right over my head.
I've read Simon Monk's books on programming Arduino's which have got me so far.
I think I need to start reading more generally about C programming.
Can you recomend a good book on C programming? There are hundreds of books on C programming but I am sure that some will be better than others.
Until I got an Arduino about a month ago, I have only ever done programming in Basic.