In the example below, I have two functions that write numbers to two different files on an SD card. I'm using the SdFat library.
Is there any way I can combine the two functions into one, so that I can call (for example): writeToFile(int t, FileName "filename1.txt");?
In the example below, to write a value to the "temperature.txt" file, I would like to be able to call: writeToFile(7, "temperature.txt"); In my sketch I have about a dozen functions that are writing to a dozen different files, so it would be very handy to be able to combine them into one function.
I think the main part I'm stuck on is I have no idea what the filename variable would be, or how to pass the filename to the function.
#include <SdFat.h>
SdFat sd;
void setup() {
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
sd.initErrorHalt();
}
setTemperature(7);
setFanSpeed(4);
}
void setTemperature(int t) {
SdFile rdfile("temperature.txt", O_WRITE);
rdfile.rewind();
rdfile.println(t);
rdfile.close();
}
void setFanSpeed(int t) {
SdFile rdfile("fanspeed.txt", O_WRITE);
rdfile.rewind();
rdfile.println(t);
rdfile.close();
}
readFileParameter(const char *filename, int value). I would like to call:readFileParameter("temperature.txt", currentTemp);, and have it store the value from the file into the integer currentTemp. What would I have to use instead of "int value", in order for it to change a global variable (like currentTemp)? I'm sorry that this is probably very basic stuff that I'm missing. – Jerry Aug 06 '15 at 16:59void readFile(const char *filename, int &value). I guess the '&' causes it to modify the variable that is passed to it? It's a little too complex for my current understanding, but so far it seems to work. It would be nice if anyone points out if it's incorrect. :) – Jerry Aug 06 '15 at 18:03