datalogger not open all files

hi all, well my project its quite simple, an arduino uno, with and sd module and a RTC for datalog 3 DHT11 sensors, all since to compile and work ok but the SD only open 4 and sometimes 5 files out of 6, temperature and humidity are saved on differentes files, but i cant manage to open all files, i got a message from the compiler that im using 78% of the dinamic memory so the bord could be unestable, maybe my code its too large and maybe i need to make it more simple.

[code]
/*
 *
 */

#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC

//define DHT pin
#define DHTPIN A0     // what pin we're connected to
#define DHTPIN A1
#define DHTPIN A2
// uncomment whatever type you're using
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// initialize DHT sensor for normal 16mhz Arduino
DHT dhta(A0, DHTTYPE);
DHT dhtb(A1, DHTTYPE);
DHT dhtc(A2, DHTTYPE);




// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4; 

// Create a file to store the data
File myFile1;
File myFile2;
File myFile3;
File humedad1;
File humedad2;
File humedad3;

// RTC
RTC_DS3231 rtc;//RTC_DS1307 rtc; //uncoment for type

void setup() {
  //initializing the DHT sensor
  dhta.begin();
  dhtb.begin();
  dhtc.begin();
 

  //initializing Serial monitor
  Serial.begin(9600);
  
  // setup for the RTC
  while(!Serial); // for Leonardo/Micro/Zero
    if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      // following line sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    if(! rtc.begin()) {//change rtc.isrunning() //for the DS1307
      Serial.println("RTC is NOT running!");
    }
    
  // setup for the SD card
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  //open file
  myFile1=SD.open("DATA1.txt", FILE_WRITE);
  humedad1=SD.open("Humedad1.txt", FILE_WRITE);
   myFile2=SD.open("DATA2.txt", FILE_WRITE);
  humedad2=SD.open("Humedad2.txt", FILE_WRITE);
   myFile3=SD.open("DATA3.txt", FILE_WRITE);
  humedad3=SD.open("Humedad3.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (myFile1) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile1.println("Date,Time,Temperature ºC");
  }
  myFile1.close();
  if (humedad1) {
    Serial.println("File opened ok");
    // print the headings for our data
    humedad1.println("Date,Time,humidity %");
  }
  humedad1.close();
  // if the file opened ok, write to it:
  if (myFile2) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile2.println("Date,Time,Temperature ºC");
  }
  myFile2.close();
  if (humedad2) {
    Serial.println("File opened ok");
    // print the headings for our data
    humedad2.println("Date,Time,humidity %");
  }
  humedad2.close();
  // if the file opened ok, write to it:
  if (myFile3) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile3.println("Date,Time,Temperature ºC");
    
  }
  myFile3.close();
  if (humedad3) {
    Serial.println("File opened ok");
    // print the headings for our data
    humedad3.println("Date,Time,humidity %");
  }
  humedad3.close();
}

void loggingTime() {
  DateTime now = rtc.now();
  myFile1 = SD.open("DATA1.txt", FILE_WRITE);
  if (myFile1) {
    myFile1.print(now.year(), DEC);
    myFile1.print('/');
    myFile1.print(now.month(), DEC);
    myFile1.print('/');
    myFile1.print(now.day(), DEC);
    myFile1.print(',');
    myFile1.print(now.hour(), DEC);
    myFile1.print(':');
    myFile1.print(now.minute(), DEC);
    myFile1.print(':');
    myFile1.print(now.second(), DEC);
    myFile1.print(",");
  }
  humedad1 = SD.open("Humedad1.txt", FILE_WRITE);
  if (humedad1) {
    humedad1.print(now.year(), DEC);
    humedad1.print('/');
    humedad1.print(now.month(), DEC);
    humedad1.print('/');
    humedad1.print(now.day(), DEC);
    humedad1.print(',');
    humedad1.print(now.hour(), DEC);
    humedad1.print(':');
    humedad1.print(now.minute(), DEC);
    humedad1.print(':');
    humedad1.print(now.second(), DEC);
    humedad1.print(",");
  }
  
  myFile2 = SD.open("DATA2.txt", FILE_WRITE);
  if (myFile2) {
    myFile2.print(now.year(), DEC);
    myFile2.print('/');
    myFile2.print(now.month(), DEC);
    myFile2.print('/');
    myFile2.print(now.day(), DEC);
    myFile2.print(',');
    myFile2.print(now.hour(), DEC);
    myFile2.print(':');
    myFile2.print(now.minute(), DEC);
    myFile2.print(':');
    myFile2.print(now.second(), DEC);
    myFile2.print(",");
  }
  humedad2 = SD.open("Humedad2.txt", FILE_WRITE);
  if (humedad2) {
    humedad2.print(now.year(), DEC);
    humedad2.print('/');
    humedad2.print(now.month(), DEC);
    humedad2.print('/');
    humedad2.print(now.day(), DEC);
    humedad2.print(',');
    humedad2.print(now.hour(), DEC);
    humedad2.print(':');
    humedad2.print(now.minute(), DEC);
    humedad2.print(':');
    humedad2.print(now.second(), DEC);
    humedad2.print(",");
  }
  
  myFile3 = SD.open("DATA3.txt", FILE_WRITE);
  if (myFile3) {
    myFile3.print(now.year(), DEC);
    myFile3.print('/');
    myFile3.print(now.month(), DEC);
    myFile3.print('/');
    myFile3.print(now.day(), DEC);
    myFile3.print(',');
    myFile3.print(now.hour(), DEC);
    myFile3.print(':');
    myFile3.print(now.minute(), DEC);
    myFile3.print(':');
    myFile3.print(now.second(), DEC);
    myFile3.print(",");
  }
  humedad3 = SD.open("Humedad3.txt", FILE_WRITE);
  if (humedad3) {
    humedad3.print(now.year(), DEC);
    humedad3.print('/');
    humedad3.print(now.month(), DEC);
    humedad3.print('/');
    humedad3.print(now.day(), DEC);
    humedad3.print(',');
    humedad3.print(now.hour(), DEC);
    humedad3.print(':');
    humedad3.print(now.minute(), DEC);
    humedad3.print(':');
    humedad3.print(now.second(), DEC);
    humedad3.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile1.close();
  myFile2.close();
  myFile3.close();
   humedad1.close();
    humedad2.close();
     humedad3.close();
  delay(9000); 
}

[/code]

since the code its too long im not showing how i read the sensors

datalogger3sensores.ino (11.6 KB)

You don't need to have all the files open at the same time. Open the first one, write to it, close it. Open the second file, write to it, close it, etc. and only use 1 File object.

ok, i have change it and now seems to be working fine, but i have another problem, when i dont open the serial of my computer, the files are not saved or even generated the files.... i need the board the to be stand alone, not use my computer to datalog the sensors

If you have new code that isn’t working, post it! The serial port should not have any interaction with your data logging but I can’t see the code. Since it is stand alone, you may want to do things like blink the built-in LED (or add more) so you can indicate what you are doing (waiting, writing, etc.)

but i have another problem, when i dont open the serial of my computer, the files are not saved or even generated the files

If you have a Uno, why do you have this code? What do you think it does?

  while(!Serial); // for Leonardo/Micro/Zero

PaulS: If you have a Uno, why do you have this code? What do you think it does?

  while(!Serial); // for Leonardo/Micro/Zero

you nare right, i was using it on first on a micro board then just figure that i would be better to have an external dc wal adapter for power supply so switch to arduino, i wasnt looking that line that just tell the arduino to wait until serial port its open to run

ok, i solve the serial port problem but i got another problem instead, the board only runs about 15 minutes, then de TX light stuck and stop collecting data, if i take out the SD then nothing happen but when i open the serial port on my pc, then the board start to responde and collect data.

does my board its going bad or my code its too heavy, or maybe the data flow its too much and the memory overrides and stuck??

When you open the serial monitor on your PC, it resets the Arduino so it will always start running from the beginning. It sounds like your program hangs somewhere - maybe memory issues, maybe SD card is full? Who knows without seeing your code.

code its quuite messy but here its the ino

datalogger3sensores.ino (11.6 KB)

Here’s a more compact version. You where still using 6 File variables. You only need 1 since you can only have one file open at a time on an SD card. Untested:

/*
   Rui Santos
   Complete Project Details http://randomnerdtutorials.com
*/

#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC

//define DHT pin
//#define DHTPIN A0     // what pin we're connected to
//#define DHTPIN A1
//#define DHTPIN A2
// uncomment whatever type you're using
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// initialize DHT sensor for normal 16mhz Arduino

DHT dhta(A0, DHTTYPE);
DHT dhtb(A1, DHTTYPE);
DHT dhtc(A2, DHTTYPE);

// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;

// Create a file to store the data
//File myFile1;
//File myFile2;
//File myFile3;
//File humedad1;
//File humedad2;
//File humedad3;

const int fileCount = 3;
const char *temperatureFiles[fileCount] = { "DATA1.txt", "DATA2.txt", "DATA3.txt" };
const char *humidityFiles[fileCount] = { "Humedad1.txt", "Humedad2.txt", "Humedad3.txt" };

// RTC
RTC_DS3231 rtc;//RTC_DS1307 rtc; //uncoment for type

void setup() {
  
  //initializing the DHT sensor
  dhta.begin();
  dhtb.begin();
  dhtc.begin();


  //initializing Serial monitor
  Serial.begin(9600);

  // setup for the RTC
  //while(!Serial); // for Leonardo/Micro/Zero
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if (! rtc.begin()) { //change rtc.isrunning() //for the DS1307
    Serial.println("RTC is NOT running!");
  }

  // setup for the SD card
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    while(1);
  }
  Serial.println("initialization done.");

  //create files
  for( int i = 0; i < fileCount; ++i ) {
    logTemperatureHeader( temperatureFiles[i] );
    logHumidityHeader( humidityFiles[i] );
  }

}

void logTemperatureHeader( const char *filename) {

  File myFile = SD.open(filename, FILE_WRITE);
  const char *msg;
  
  // if the file opened ok, write to it:
  if (myFile) {
    // print the headings for our data
    myFile.println("Date,Time,Temperature ºC");
    msg = " opened ok";
  } else {
    msg = " failed to open";
  }
  myFile.close();
  Serial.print("File "); Serial.print(filename); Serial.println(msg);
}


void logHumidityHeader( const char *filename) {

  File myFile = SD.open(filename, FILE_WRITE);
  const char *msg;
  
  // if the file opened ok, write to it:
  if (myFile) {
    // print the headings for our data
    myFile.println("Date,Time,humidity %");
    msg = " opened ok";
  } else {
    msg = " failed to open";
  }
  myFile.close();
  Serial.print("File "); Serial.print(filename); Serial.println(msg);
}

void loggingTime(const char *filename) {
  DateTime now = rtc.now();
  File myFile = SD.open(filename, FILE_WRITE);
  if (myFile) {
    myFile.print(now.year(), DEC);
    myFile.print("/");
    myFile.print(now.month(), DEC);
    myFile.print("/");
    myFile.print(now.day(), DEC);
    myFile.print(",");
    myFile.print(now.hour(), DEC);
    myFile.print(":");
    myFile.print(now.minute(), DEC);
    myFile.print(":");
    myFile.print(now.second(), DEC);
    myFile.print(",");
  } else {
    Serial.print("Failed to log time to " ); Serial.println(filename);
  }
  myFile.close();

  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.println(now.second(), DEC);
  delay(9000);
}

void loggingTemperature(DHT *dht, const char *filename) {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius
  //float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht->readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if  (isnan(f) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  //debugging purposes
  Serial.print("Temperature: ");
  Serial.print(f, 2);
  Serial.println(" *f");

  File myFile = SD.open(filename, FILE_WRITE);
  if (myFile) {
    Serial.print(filename); Serial.println(" open with success");
    myFile.print(f);
    myFile.println(",");
  } else {
    Serial.print(filename); Serial.println(" failed to open");
  }
  myFile.close();
}


void loggingHumidity(DHT *dht, const char *filename) {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius
  //float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float H = dht->readHumidity();
  if  (isnan(H) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("humidity: ");
  Serial.print(H,2);
  Serial.println(" %");

  File myFile = SD.open(filename, FILE_WRITE);
  if (myFile) {
    Serial.print(filename); Serial.println(" open with success");
    myFile.print(H);
    myFile.println(",");
  } else {
    Serial.print(filename); Serial.println(" failed to open");
  }
  myFile.close();
}


void loop() {
  for( int i = 0; i < fileCount; ++i ) {
    loggingTime(temperatureFiles[i]);
    loggingTime(humidityFiles[i]);
  }
  loggingTemperature(&dhta, temperatureFiles[0]);
  loggingHumidity( &dhta, humidityFiles[0]);
  loggingTemperature(&dhtb, temperatureFiles[1]);
  loggingHumidity( &dhtb, humidityFiles[1]);
  loggingTemperature(&dhtc, temperatureFiles[2]);
  loggingHumidity( &dhtc, humidityFiles[2]);
  delay(2000);
}

i was away for a while and i tried your code now, its quite beter, but still crashes about 10 minutes stand alone, it never crashes on serial port opened

When you run this in stand-alone, how it is powered? When you connect to your PC and have the serial port open, you are powering it through the USB. That could be a big difference. You may have to start putting more debug statements into your code that write to a file on the SD card to figure out where your program is hanging.