Best way to save sensor data from multiple devices on to a SD Card

Hi again,

I have an Arduino setup with a handful of various sensors connected through I2C (temp, humidity, rgb detection, etc). When I type a command into the serial monitor like: { t 1 }, this displays and refreshes the temperature readings on the serial monitor every second, which looks like this:

12:14:32 | 10/6/17 Temperature: 72.3 F 12:14:33 | 10/6/17 Temperature: 72.5 F

I would also like to save this information onto a SD card, but I'm not quite sure the best way to save the data since I have so many devices and instantiating a new Class variable like 'File myData;' for each file doesn't seem logical. Here's some pseudo-ish code to explain my setup. For this, I will show you the main.ino and the Temperature sensor.cpp files to show how the project is setup and how data is printed to Serial monitor

Main file:

#include (all includes are added here) 

void setup() {

  //Begin serial communications
  Serial.begin(SERIAL_BAUD);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  const int chipSelect = 10;
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("SD initialization failed!");
    return;
  }
  Serial.println("SD initialization done.");
  
  
  //Call setup funcs
  setupSerialParser();
  initRTC();
  initRGBSensor();
  initRGBLED();
  initTemp_HumiditySensor();
  Serial.println(F("Parser ready"));
}

void loop() {

    //check for new serial data and parse if necessary
  checkSerial();
  //handle atSci requests
  checkRequests();
  //handle Temperature/Humidity reporting
  reportTemp_Humidity();
  //handle color reporting
  reportRGB();
  
}

And then the temperature.cpp file:

/* Global Variables */
bool reportOn = 0;
unsigned long weatherTimer = 0;

float humidity = 0;
float tempf = 0;

//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather weatherSensor;

void readWeather()
{
  // Measure Relative Humidity from the HTU21D or Si7021
  humidity = weatherSensor.getRH();
  
  // Measure Temperature from the HTU21D or Si7021
  tempf = weatherSensor.getTempF();
  // Temperature is measured every time RH is requested.
  // It is faster, therefore, to read it from previous RH
  // measurement with getTemp() instead with readTemp()
}

void printWeather(){
  displayRTC();
  Serial.print("Temp:");
  Serial.print(weatherSensor.getTempF());
  Serial.print("F, ");
  Serial.print("Humidity:");
  Serial.print(weatherSensor.getRH());
  Serial.println("%");
}

void reportTemp_Humidity(){
  //if we should report temps, check timers and update.
  if (reportOn) {
    if(millis() - weatherTimer > 1000){
      printWeather();
      weatherTimer = millis();
    }
  }
  return;
}

//turns reporting on and off
void toggleWeatherReporting(bool state){
  reportOn = state;
  return;
}

I was thinking of opening the SD card and writing to the card inside the printWeather() function in parallel with the Serial.print lines, but I figured that there was a much easier way to write the device outputs to SD card. I have many .cpp files like the one above, so opening and closing the sd card for each device just felt inefficient, if that makes sense.

Thanks for any guidance!

I'm not quite sure the best way to save the data since I have so many devices and instantiating a new Class variable like 'File myData;' for each file doesn't seem logical.

If you want to save data from each sensor into a separate file, you have no choice but to have a File variable for each file that you want to be able to have open at any given time.

If you only one to have one file open at a time, because you open the file, write to it, and close the file, you only need one File object, but you'll need different variables to hold the name of the file to open.

If you want to save data from all the sensors in the same file, you only need one File object and one variable to hold the name of the file.

It really isn't clear what you want to do.

Have a think what you want to do with the saved data. For example if you save all data in one file , you could load it into a spreadsheet and seperate out say a temperature graph and a humidity graph . This might make more sense than seperate files for each sensor when it comes to Doing anything with the data -?easier to program too ..