Greetings everyone, creating my first project and I'm struggling to find a usable method of having one array provide both string names and assign a data variable. This is a working function that writes the current values from a range of sensors to individual csv files. It is called after sensor data has been updated:
// strings for file location
String dir = "/mnt/sd/";
String format = ".csv";
// Sensor data
int temp;
int hum;
int light;
int moist;
int soil;
// Array of sensor names
char* Sensors[] = {"temp","hum","light","moist","soil",};
void writeLog() { // Write data to log files
int SensorData[32] = {temp, hum, light, moist, soil}; // array of current sensor data values
for (int s = 0; s <= 4; s++) { // loop through available sensors
String cur = Sensors[s]; // string for current sensor name
String curTime = time(); // grab the time
curTime.trim(); // and knock off the newline
String curFile = dir + cur + format; // concat filename string
int curLen = curFile.length() + 1; // Dynamic buffer length with null terminator
char curDest[curLen]; // prepare buffer
curFile.toCharArray(curDest, curLen); // convert to a character array
File dataFile = FileSystem.open(curDest, FILE_APPEND); // open file to write log
if (dataFile) { // assuming we can open the file
dataFile.print(curTime); // timestamp
dataFile.print(","); // comma
dataFile.println(SensorData[s]); // sensor data value
dataFile.close(); // close file
}
}
}
Note there are two arrays - one for the string value name of the sensor (Sensors) and one for the current integer values of the sensors (SensorData). This code only works because two arrays of different types are kept manually identical.
So far I have been unable to condense them into one array that can be expanded into both string and integer values. I have tried multiple ways of converting but consistently get stuck on invalid type errors. It also seems I cannot initialise a 3D array of mixed type.
What methods would you use to solve this problem and only maintain one array to provide both data types?