Error opening datalog file

Hello,
I am trying to store temperature and humidity from 4 sensors (2 am2301 and 2 am2302) to check their precision.
System manages to store the first 2 lines and fails afterwards. I think it may have to do with memory or resources in general, that’s why I have also implemented the creation of the data string with two ways, one by printing each of each elemts and second, by creating the string by concatinating smaller parts. I also disabled the lcd prints.
Any ideas why my code fails?
Note that I am using an UNO with RTC/SD logger shield
I never managed to make this shield work with my Mega board ( I saw the tutorials in adafruit, changed the pinoutsetc…but no luck :frowning: )
Thank you!

//
#include <LiquidCrystal.h>
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include "RTClib.h"


#define DHTPIN_2301 14     // what pin we're connected to
#define DHTPIN_2302 15

#define DHTPIN_2301b 16     
#define DHTPIN_2302b 17

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  //Pins for the LCD display, as taken from arduino.cc
//LiquidCrystal lcd(19, 18, 16, 17, 15, 14);  //Pins changed so that the don't conflict with sd


// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTTYPE_1 DHT21   // DHT 21 (AM2301)
#define DHTTYPE_2 DHT22   //DHT22 (AM2302)



// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht_2301(DHTPIN_2301, DHTTYPE_1);
DHT dht_2302(DHTPIN_2302, DHTTYPE_2);

DHT dht_2301b(DHTPIN_2301b, DHTTYPE_1);
DHT dht_2302b(DHTPIN_2302b, DHTTYPE_2);

//const int chipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile root;

//RTC
RTC_DS1307 RTC; // define the Real Time Clock object

int previous = 0;
int pos = 0;

File dataFile;
const int chipSelect = 10;   //FOR SD/RTC logger

void setup() {
  Serial.begin(9600); 
  dht_2301.begin();
  dht_2302.begin();
  
  dht_2301b.begin();
  dht_2302b.begin();
  

//SD Card
 while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("Initializing SD...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
   // pinMode(SS, OUTPUT);  //FOR MEGA
   pinMode(10, OUTPUT); 
  
   // see if the card is present and can be initialized:
//  if (!SD.begin(10, 11, 12, 13))  //FOR MEGA
 if (!SD.begin(chipSelect)) {   //for UNO
 Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("SD card OK!");
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("Error opening datalog.txt - step 1");
    // Wait forever since we cant write data
    while (1) ;
    }

    // connect to RTC
    Wire.begin();  
    if (!RTC.begin()) {
    Serial.println("RTC failed");
    }else {
    Serial.println("RTC OK!");
    }
}


//a counter to increase after each reading    
int counter = 0; 
int totaltime = 0;
float h_2301 = 0;
float t_2301 = 0;
float h_2302 = 0;
float t_2302 = 0;

float h_2301b = 0;
float t_2301b = 0;
float h_2302b = 0;
float t_2302b = 0;

int i = 0;
float dew = 0;

  
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  
  
  h_2301 = dht_2301.readHumidity();
  t_2301 = dht_2301.readTemperature();

  h_2302 = dht_2302.readHumidity();
  t_2302 = dht_2302.readTemperature();
  
  h_2301b = dht_2301.readHumidity();
  t_2301b = dht_2301.readTemperature();

  h_2302b = dht_2302.readHumidity();
  t_2302b = dht_2302.readTemperature();
  
  
  dew = dewPoint(t_2301, h_2301);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t_2301) || isnan(h_2301) || isnan(t_2301b) || isnan(h_2301b)) {

    Serial.println("Failed to read from DHT21");
  }
  if (isnan(t_2302) || isnan(h_2302) || isnan(t_2302b) || isnan(h_2302b)) {

    Serial.println("Failed to read from DHT22");
  }    
  else {
 
    
     // fetch the time
    DateTime now = RTC.now();

    Serial.print(now.day(), DEC);
    Serial.print("/"); 
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.year(), DEC);
    Serial.print(" ");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.print(now.second(), DEC);
    Serial.print("\" \t");

    Serial.print("2301 H: "); 
    Serial.print(h_2301);
    Serial.print(" % ");
    Serial.print("T: "); 
    Serial.print(t_2301);
    Serial.print(" *C\t"); 
    Serial.print("2302 H: "); 
    Serial.print(h_2302);
    Serial.print(" % ");
    Serial.print("T: "); 
    Serial.print(t_2302);
    Serial.print(" *C\t"); 
    Serial.print("2301b H: "); 
    Serial.print(h_2301b);
    Serial.print(" % ");
    Serial.print("T: "); 
    Serial.print(t_2301b);
    Serial.print(" *C\t"); 
    Serial.print("2302b H: "); 
    Serial.print(h_2302b);
    Serial.print(" % ");
    Serial.print("T: "); 
    Serial.print(t_2302b);
    Serial.print(" *C\r\n"); 

  String sen1 = floatToString(h_2301, t_2301);
  String sen2 = floatToString(h_2302, t_2302);
  String sen3 = floatToString(h_2301b, t_2301b);
  String sen4 = floatToString(h_2302b, t_2302b);
  
//This is the second way -  not fully implemented... 
 String dataString="";
  dataString = "01(h/t): "+sen1+", 02: "+sen2+", 01_B: "+sen3+", 02_B: "+sen4+"\r\n";
  //dataString = sen1+sen2+sen3+sen4;
  Serial.print(dataString);
  
  // if the file is available, write to it:
    if (dataFile) {

        dataFile.print('"');
        dataFile.print(now.day(), DEC);
        dataFile.print("/");
        dataFile.print(now.month(), DEC);
        dataFile.print("/");
        dataFile.print(now.year(), DEC);
        dataFile.print(" ");
        dataFile.print(now.hour(), DEC);
        dataFile.print(":");
        dataFile.print(now.minute(), DEC);
        dataFile.print(":");
        dataFile.print(now.second(), DEC);
        dataFile.print("\"  ");
    
        
        dataFile.print("2301 H: ");
        dataFile.print(h_2301);
        dataFile.print(", T: ");
        dataFile.print(t_2301);
        dataFile.print(" 2302 H: ");
        dataFile.print(h_2302);
        dataFile.print(", T: ");
        dataFile.print(t_2302);
        dataFile.print("2301-B H: ");
        dataFile.print(h_2301);
        dataFile.print(", T: ");
        dataFile.print(t_2301);
        dataFile.print(" 2302-B H: ");
        dataFile.print(h_2302);
        dataFile.print(", T: ");
        dataFile.print(t_2302);
        dataFile.print("\r\n");
        dataFile.close();
      }  
      // if the file isn't open, pop up an error:
      else {
      Serial.println("Error opening datalog.txt");
       } 
     }
      
      dataFile.flush();
      delay(2000);
  }


//function to create strings out of floats to concatinate them
//in one line for storing / printing
String floatToString(float val, float val2){
 
  char floatnum[6];
  char floatnumb[6];
  
  String priceAsString;
  dtostrf(val,1,2,floatnum);
  dtostrf(val2,1,2,floatnumb);
  priceAsString=String(floatnum)+", "+String(floatnumb);
  return priceAsString;
  
}



/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)E
{
        return celsius + 273.15;
}


// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

What board are you using?

The String class is famous for making swiss cheese on the little RAM an UNO has, using char array's is much more reliable.

As I cannot compile the sketch (not the right libs) what does the compilation output looks like? free RAM & sketch size?

If you do only Serial and LCD, does the sketch work then?

Finally, can you minimize the sketch so it still shows the problem?

Hello,
First of all thank you for taking time to answer my question.
Today morning I am trying to optimize my code a little bit and as you can see I changed all the discrete Serial and file prints to one single print of a buffer I am creating with all the data (sensor measurements).
My sketch memory footprint is <26 our of 32K which is the limit for UNO.
My question is why my UNO prints both buffers when I only do one println (this of date)? —line 234
And secondly, why does it fail after one or two iterations of the code prints the error message of line 247
???

Any help is highly appreciated!

//
#include <LiquidCrystal.h>
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include "RTClib.h"

byte thermometer_icon[8] = //icon for termometer
{
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
};

byte humidity_icon[8] = //icon for water droplet
{
    B00100,
    B00100,
    B01010,
    B01010,
    B10001,
    B10001,
    B10001,
    B01110,
};

#define DHTPIN_2301 14     // what pin we're connected to
#define DHTPIN_2302 15

#define DHTPIN_2301b 16     
#define DHTPIN_2302b 17

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  //Pins for the LCD display, as taken from arduino.cc
//LiquidCrystal lcd(19, 18, 16, 17, 15, 14);  //Pins changed so that the don't conflict with sd


// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTTYPE_1 DHT21   // DHT 21 (AM2301)
#define DHTTYPE_2 DHT22   //DHT22 (AM2302)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht_2301(DHTPIN_2301, DHTTYPE_1);
DHT dht_2302(DHTPIN_2302, DHTTYPE_2);

DHT dht_2301b(DHTPIN_2301b, DHTTYPE_1);
DHT dht_2302b(DHTPIN_2302b, DHTTYPE_2);

//const int chipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile root;

//RTC
RTC_DS1307 RTC; // define the Real Time Clock object


int previous = 0;
int pos = 0;

File dataFile;
const int chipSelect = 10;   //FOR SD/RTC logger

void setup() {
  Serial.begin(9600); 
  dht_2301.begin();
  dht_2302.begin();
  
  dht_2301b.begin();
  dht_2302b.begin();
  
//  lcd.begin(16, 2);
//  lcd.setCursor(0, 0);
//  lcd.print("Temp/Humi Logger");
//  lcd.setCursor(0, 1);

//  //delay(2000);
//    
//  lcd.createChar(1,thermometer_icon);
//  lcd.createChar(2,humidity_icon);
//
//  lcd.clear(); 



//SD Card
 while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 // lcd.print("Initializing SD...");
  Serial.println("Initializing SD...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
   // pinMode(SS, OUTPUT);  //FOR MEGA
   pinMode(10, OUTPUT); 
  
   // see if the card is present and can be initialized:
//  if (!SD.begin(10, 11, 12, 13))  //FOR MEGA
 if (!SD.begin(chipSelect)) {   //for UNO
 Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("SD card OK!");
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("Error opening datalog.txt - step 1");
    // Wait forever since we cant write data
    while (1) ;
    }

    // connect to RTC
    Wire.begin();  
    if (!RTC.begin()) {
    Serial.println("RTC failed");
    }else {
    Serial.println("RTC OK!");
    }
    Serial.println("2301h/t 2302h/t 2301Bh/t 2302Bh/t");
}


//a counter to increase after each reading    
int counter = 0; 
int totaltime = 0;
float h_2301 = 0;
float t_2301 = 0;
float h_2302 = 0;
float t_2302 = 0;

float h_2301b = 0;
float t_2301b = 0;
float h_2302b = 0;
float t_2302b = 0;

int i = 0;
float dew = 0;

  
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  
  
  h_2301 = dht_2301.readHumidity();
  t_2301 = dht_2301.readTemperature();

  h_2302 = dht_2302.readHumidity();
  t_2302 = dht_2302.readTemperature();
  
  h_2301b = dht_2301b.readHumidity();
  t_2301b = dht_2301b.readTemperature();

  h_2302b = dht_2302b.readHumidity();
  t_2302b = dht_2302b.readTemperature();
  
  
  dew = dewPoint(t_2301, h_2301);
//  t = t* 1.8 + 32;
 // printLine(1);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t_2301) || isnan(h_2301) || isnan(t_2301b) || isnan(h_2301b)) {
//    lcd.setCursor(0, 0);
//    lcd.print("Failed to read from DHT21");
    Serial.println("Failed to read from DHT21");
  } else if (isnan(t_2302) || isnan(h_2302) || isnan(t_2302b) || isnan(h_2302b)) {
//    lcd.setCursor(0, 0);
//    lcd.print("Failed to read from DHT22");
    Serial.println("Failed to read from DHT22");
  }    
  else {
 
    
     // fetch the time
    DateTime now = RTC.now();


  /*  
    lcd.clear(); 
    lcd.setCursor(0, 0);
    lcd.write(2);
    lcd.print(h_2301,1);
    lcd.print("% ");
    lcd.write(1);
    lcd.print(t_2301,1);
    lcd.print((char)223); //degree sign
    lcd.print("C"); //degree sign
    lcd.setCursor(0, 1);
    lcd.write(2);
    lcd.print(h_2302,1);
    lcd.print("% ");
    lcd.write(1);
    lcd.print(t_2302,1);
    lcd.print((char)223); //degree sign
    lcd.print("C"); //degree sign
    */


  char date[20];
  char dataString[48];

  sprintf(date,"%02i/%02i/%04i %02i/%02i/%02i ", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
  
   
  //dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
  dtostrf(h_2301, 3, 2, &dataString[0]);
  dataString[5] =' ';
  dtostrf(t_2301, 3, 2, &dataString[6]);
  dataString[11] =' ';
  dtostrf(h_2302, 3, 2, &dataString[12]);
  dataString[17] =' ';
  dtostrf(t_2302, 3, 2, &dataString[18]);
  dataString[23] =' ';
  dtostrf(h_2301b, 3, 2, &dataString[24]);
  dataString[29] =' ';
  dtostrf(t_2301b, 3, 2, &dataString[30]);
  dataString[35] =' ';
  dtostrf(h_2302b, 3, 2, &dataString[36]);
  dataString[41] =' ';
  dtostrf(t_2302b, 3, 2, &dataString[42]);
  Serial.println(date);  //With this Serial.println UNO prints both buffers (date and dataString 

// Serial.print(dataString);
// Serial.print("\r\n");


  // if the file is available, write to it:
    if (dataFile) {
            
         dataFile.print(date);
         dataFile.print(dataString);
         dataFile.close();
      }  
      // if the file isn't open, pop up an error:  --- --- > This error keeps coming up after 2 loops :(
      else {
      Serial.println("Error opening datalog.txt");
       } 
     }
      
     // dataFile.flush();
      delay(2000);
  }



/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}


// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

I managed to make it work - this is the final code,
my mistake was after the line 242.

//

#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include "RTClib.h"

byte thermometer_icon[8] = //icon for termometer
{
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
};

byte humidity_icon[8] = //icon for water droplet
{
    B00100,
    B00100,
    B01010,
    B01010,
    B10001,
    B10001,
    B10001,
    B01110,
};

#define DHTPIN_2301 14     // what pin we're connected to
#define DHTPIN_2302 15

#define DHTPIN_2301b 16     
#define DHTPIN_2302b 17



// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTTYPE_1 DHT21   // DHT 21 (AM2301)
#define DHTTYPE_2 DHT22   //DHT22 (AM2302)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht_2301(DHTPIN_2301, DHTTYPE_1);
DHT dht_2302(DHTPIN_2302, DHTTYPE_2);

DHT dht_2301b(DHTPIN_2301b, DHTTYPE_1);
DHT dht_2302b(DHTPIN_2302b, DHTTYPE_2);

//const int chipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile root;

//RTC
RTC_DS1307 RTC; // define the Real Time Clock object

int previous = 0;
int pos = 0;

File dataFile;
const int chipSelect = 10;   //FOR SD/RTC logger

void setup() {
  Serial.begin(57600); 
  dht_2301.begin();
  dht_2302.begin();
  
  dht_2301b.begin();
  dht_2302b.begin();
  
//  lcd.begin(16, 2);
//  lcd.setCursor(0, 0);
//  lcd.print("Temp/Humi Logger");
//  lcd.setCursor(0, 1);
//  //delay(2000);
//    
//  lcd.createChar(1,thermometer_icon);
//  lcd.createChar(2,humidity_icon);
//
//  lcd.clear(); 



//SD Card
 while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 // lcd.print("Initializing SD...");
  Serial.println("Initializing SD...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
   // pinMode(SS, OUTPUT);  //FOR MEGA
   pinMode(10, OUTPUT); 
  
   // see if the card is present and can be initialized:
//  if (!SD.begin(10, 11, 12, 13))  //FOR MEGA
 if (!SD.begin(chipSelect)) {   //for UNO
 Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("SD card OK!");
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  dataFile.println("dd/mm/yyyy hh:mm:ss 2301 h/t    2302 h/t    2301B h/t   2302B h/t");
  if (! dataFile) {
    Serial.println("Error opening datalog.txt - Case 1");
    // Wait forever since we cant write data
    while (1) ;
    }

    // connect to RTC
    Wire.begin();  
    if (!RTC.begin()) {
    Serial.println("RTC failed");
    }else {
    Serial.println("RTC OK!");
    }
    Serial.println("2301 h/t   2302 h/t   2301B h/t   2302B h/t");
    
}


//a counter to increase after each reading    
int counter = 0; 
int totaltime = 0;
float h_2301 = 0;
float t_2301 = 0;
float h_2302 = 0;
float t_2302 = 0;

float h_2301b = 0;
float t_2301b = 0;
float h_2302b = 0;
float t_2302b = 0;

int i = 0;
float dew = 0;

  
void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  
  
  h_2301 = dht_2301.readHumidity();
  t_2301 = dht_2301.readTemperature();

  h_2302 = dht_2302.readHumidity();
  t_2302 = dht_2302.readTemperature();
  
  h_2301b = dht_2301b.readHumidity();
  t_2301b = dht_2301b.readTemperature();

  h_2302b = dht_2302b.readHumidity();
  t_2302b = dht_2302b.readTemperature();
  
  
  dew = dewPoint(t_2301, h_2301);
//  t = t* 1.8 + 32;
 // printLine(1);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t_2301) || isnan(h_2301) || isnan(t_2301b) || isnan(h_2301b)) {
//    lcd.setCursor(0, 0);
//    lcd.print("Failed to read from DHT21");
    Serial.println("Failed to read from DHT21");
  } else if (isnan(t_2302) || isnan(h_2302) || isnan(t_2302b) || isnan(h_2302b)) {
//    lcd.setCursor(0, 0);
//    lcd.print("Failed to read from DHT22");
    Serial.println("Failed to read from DHT22");
  }    
  else {
 
    
     // fetch the time
    DateTime now = RTC.now();


  /*  
    lcd.clear(); 
    lcd.setCursor(0, 0);
    lcd.write(2);
    lcd.print(h_2301,1);
    lcd.print("% ");
    lcd.write(1);
    lcd.print(t_2301,1);
    lcd.print((char)223); //degree sign
    lcd.print("C"); //degree sign
    lcd.setCursor(0, 1);
    lcd.write(2);
    lcd.print(h_2302,1);
    lcd.print("% ");
    lcd.write(1);
    lcd.print(t_2302,1);
    lcd.print((char)223); //degree sign
    lcd.print("C"); //degree sign
    */


  char date[20];
  char dataString[48];

  sprintf(date,"%02i/%02i/%04i %02i/%02i/%02i ", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
  
   
  //dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
  dtostrf(h_2301, 3, 2, &dataString[0]);
  dataString[5] =' ';
  dtostrf(t_2301, 3, 2, &dataString[6]);
  dataString[11] =' ';
  dtostrf(h_2302, 3, 2, &dataString[12]);
  dataString[17] =' ';
  dtostrf(t_2302, 3, 2, &dataString[18]);
  dataString[23] =' ';
  dtostrf(h_2301b, 3, 2, &dataString[24]);
  dataString[29] =' ';
  dtostrf(t_2301b, 3, 2, &dataString[30]);
  dataString[35] =' ';
  dtostrf(h_2302b, 3, 2, &dataString[36]);
  dataString[41] =' ';
  dtostrf(t_2302b, 3, 2, &dataString[42]);
  Serial.println(date);  //With this Serial.println UNO prints both buffers (date and dataString 

// Serial.print(dataString);
// Serial.print("\r\n");


  // if the file is available, write to it:

            
         dataFile.println(date);
         dataFile.flush();
         delay(5000);
     
      // if the file isn't open, pop up an error:  --- --- > This error keeps coming up after 2 loops :(
     if (!dataFile) {
      Serial.println("Error opening datalog.txt - Case 2");
       } 
     }
      

  }



/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}


// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}