When reading .CSV file nothing shows up

I am building an automatic plant waterer and data logger, however when I view the data from the .CSV file recorded on a sd card, I get nothing. I believe something is wrong with my code. Any help is greatly appreciated.

#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "SD.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define DHTTYPE DHT11
#define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0
#define LOG_INTERVAL 30 //milliseconds between entries (2.5 min)

const int dhtPin = 9;
const int chipSelect = 10;
const int enablePin = 7;
const int in1Pin = 8;
const int in2Pin = 6;
const int wateringTime = 300; //Set the watering time (4 min for a start)
const float wateringThreshold = 1000; //Value Above which the garden gets watered

OneWire oneWire(A2);
DallasTemperature sensors(&oneWire);
DHT dht(dhtPin, DHTTYPE);
RTC_DS1307 rtc;

int soilTemp = 0; //Scaled value of soil temp (degrees F)
int soilMoisture = 0; //Scaled value of volumetric water content in soil (percent)
int humidity = 0; //Relative humidity (%)
int airTemp = 0; //Air temp (degrees F)
int heatIndex = 0; //Heat index (degrees F)
int sunlight = 0 ; //Sunlight illumination in lux
bool watering = false;
bool wateredToday = false;
DateTime now;
File logfile;


void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
 
  
  while(1);
}

void setup() {
 
  //Initialize serial connection
  Serial.begin(9600); //Just for testing
  Serial.println("Initializing SD card...");
  
  
  pinMode(chipSelect, OUTPUT); //Pin for writing to SD card
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  digitalWrite(in1Pin, LOW); 
  digitalWrite(in2Pin, LOW); 
  
  //Establish connection with DHT sensor
  dht.begin();

  //Establish connection with Soil Temp sensor
   sensors.begin();
   
  //Establish connection with real time clock
  Wire.begin();
  if (!rtc.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
  
  //Set the time and date on the real time clock if necessary
  if (! rtc.isrunning()) {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  
  //Check if SD card is present and can be initialized
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");        
  }
  
  Serial.println("Card initialized.");
    
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  
  logfile.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?");   //HEADER 
#if ECHO_TO_SERIAL
  Serial.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?");
#endif ECHO_TO_SERIAL// attempt to write out the header to the file

  now = rtc.now();
    
}

void loop() {
       
        //delay software
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
   
    if (!(now.day()==rtc.now().day())) {
    wateredToday = false;
  }
  
  now = rtc.now();
  

  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(",");
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print(",");
  logfile.print(" ");
#if ECHO_TO_SERIAL
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(",");
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(",");
  Serial.print(" ");
#endif

  //Collect Variables
  sensors.requestTemperatures();
  float soilTemp;
  soilTemp = sensors.getTempCByIndex(0);
  delay(20);
  
  soilMoisture = analogRead(A1);
  delay(20);
    
  humidity = dht.readHumidity();
  delay(20);
  
  airTemp = dht.readTemperature(true);
  delay(20);
  
  heatIndex = dht.computeHeatIndex(airTemp,humidity);
  delay(20);
  
  sunlight = analogRead(A0);
  delay(20);
  
  //Log variables
  logfile.print(soilTemp,2);
  logfile.print(",");
  logfile.print(airTemp);
  logfile.print(",");
  logfile.print(soilMoisture);
  logfile.print(",");
  logfile.print(humidity);
  logfile.print(",");
  logfile.print(heatIndex);
  logfile.print(",");
  logfile.print(sunlight);
  logfile.print(",");
#if ECHO_TO_SERIAL
  Serial.print(soilTemp,2);
  Serial.print(",");
  Serial.print(airTemp);
  Serial.print(",");
  Serial.print(soilMoisture);
  Serial.print(",");
  Serial.print(humidity);
  Serial.print(",");
  Serial.print(heatIndex);
  Serial.print(",");
  Serial.print(sunlight);
  Serial.print(",");
#endif
  
  if (soilMoisture > wateringThreshold && wateredToday == false)  {
    //water the garden
 digitalWrite(enablePin, HIGH);
 digitalWrite(in1Pin, HIGH); 
 digitalWrite(in2Pin, LOW); 
    delay(wateringTime);
 digitalWrite(in1Pin, LOW); 
 digitalWrite(in2Pin, LOW); 
 digitalWrite(enablePin, HIGH);

    //record that we're watering
    logfile.print("TRUE");
#if ECHO_TO_SERIAL
    Serial.print("TRUE");
#endif

 wateredToday = true;
  }
  else {
    logfile.print("FALSE");
    #if ECHO_TO_SERIAL
    Serial.print("FALSE");
#endif
  }
  
  logfile.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif
  delay(50);
  
  //Write to SD card
  logfile.flush();
  delay(5000);
}

You are probably right but you need to be more forthcoming about what really happens, and doesn't.

Do all the serial prints come out as expected?

Does the file you want to make not get made? Or does it get made but it's empty?

I don't see why you need to do this

#if ECHO_TO_SERIAL
 tralala
#endif

You either want to print to serial or you don't. If you don't, don't ask for it, if you do a Serial.print will suffice.

I wonder what this is about?

  //Write to SD card
  logfile.flush();

When I view my code in serial, everything comes out as expected but when I check the SD cards it just creates an empty csv file.

For the

logfile.flush();

I read that it ensures that any bytes written to the file are physically saved to the SD card.

And I will remove

#if echo to serial

trbjr:
logfile.flush();

I read that it ensures that any bytes written to the file are physically saved to the SD card.

It sounds like nonsense. It is just possible that it will do exactly the opposite - if anything.

Don't forget to remove the #endif rubbish as well, but it seems that at least you have confined the problem to the SD operations.

You might find some bits in
http://homepages.ihug.com.au/~npyner/Arduino/Bluetooth_graphic_5110_with_file_retrieve_DY.ino
useful

I have been messing around with the code and found out that if I remove

 float h = dht.readHumidity();
  float t = dht.readTemperature();
  float hic = dht.computeHeatIndex(t, h, false);

(DHT temp sensor), I will record data to the sd. Do you know why this is affecting it? And how to fix it? Thank you!

#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DHTTYPE DHT11
#define LOG_INTERVAL 1000 //milliseconds between entries (2.5 min)

const int dhtPin = 9;
const int chipSelect = 10;
const int enablePin = 7;
const int in1Pin = 8;
const int in2Pin = 6;
const int wateringTime = 300; //Set the watering time (4 min for a start)
const float wateringThreshold = 1000; //Value Above which the garden gets watered

OneWire oneWire(A2);
DallasTemperature sensors(&oneWire);
DHT dht(dhtPin, DHTTYPE);
RTC_DS1307 RTC;

int soilTemp = 0; //Scaled value of soil temp (degrees F)
int soilMoisture = 0; //Scaled value of volumetric water content in soil (percent)
int sunlight = 0 ; //Sunlight illumination in lux
bool watering = false;
bool wateredToday = false;
DateTime now;
File logfile;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}

void setup() {

  Serial.begin(9600); 
  Serial.println();

  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, LOW);
  
 // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);

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

  //Establish connection with DHT sensor
  dht.begin();

  //Establish connection with Soil Temp sensor
  sensors.begin();


  //Set the time and date on the real time clock if necessary
  if (! RTC.isrunning()) {
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }


  logfile.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?");   //HEADER
  Serial.println("Date, Time, Soil Temp(C), Air Temp(F), Soil Moisture Content(%), Relative Humidity(%), Heat Index(F) ,Sunlight Illumination, Watering?");


}

void loop() {

  delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));

  if (!(now.day() == RTC.now().day())) {
    wateredToday = false;
  }

  now = RTC.now();
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(",");
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print(",");
  logfile.print(" ");
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(",");
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(",");
  Serial.print(" ");

  //Collect Variables
  sensors.requestTemperatures();
  float soilTemp;
  soilTemp = sensors.getTempCByIndex(0);
  delay(20);

  soilMoisture = analogRead(A1);
  delay(20);
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float hic = dht.computeHeatIndex(t, h, false);
  
  sunlight = analogRead(A0);
  delay(20);

  //Log variables
  logfile.print(soilTemp, 2);
  logfile.print(",");
  logfile.print(t);
  logfile.print(",");
  logfile.print(soilMoisture);
  logfile.print(",");
  logfile.print(h);
  logfile.print(",");
  logfile.print(hic);
  logfile.print(",");
  logfile.print(sunlight);
  logfile.print(",");
  Serial.print(soilTemp, 2);
  Serial.print(",");
  Serial.print(t);
  Serial.print(",");
  Serial.print(soilMoisture);
  Serial.print(",");
  Serial.print(h);
  Serial.print(",");
  Serial.print(hic);
  Serial.print(",");
  Serial.print(sunlight);
  Serial.print(",");

  if (soilMoisture > wateringThreshold && wateredToday == false)  {
    //water the garden
    digitalWrite(enablePin, HIGH);
    digitalWrite(in1Pin, HIGH);
    digitalWrite(in2Pin, LOW);
    delay(wateringTime);
    digitalWrite(in1Pin, LOW);
    digitalWrite(in2Pin, LOW);
    digitalWrite(enablePin, HIGH);

    //record that we're watering
    logfile.print("TRUE");
#if ECHO_TO_SERIAL
    Serial.print("TRUE");
#endif

    wateredToday = true;
  }
  else {
    logfile.print("FALSE");
    Serial.print("FALSE");
  }

  logfile.println();
  Serial.println();
}

No, and no. You have already said all your serial prints come out as expected and, as far as I can see, everything you want to record is repeated on the monitor. This implies that your sensor readings were kosher, so how removing something fixes anything is beyond me.

This leaves the file management. I'm afraid I find it incomprehensible, and I don't even understand what the objective is. It appears that you are probably forming the files OK but failing to write to them, probably because everything is far more complicated than it needs to be.

It might help if you group all the SD prints together at the end of the loop, properly opened and properly closed, in a similar manner to the example I referred to - which can be a subroutine. This means you do it all in one operation i.e. you sort out those text flags as variables first, and you have something that is easily understood.

void WriteSD()
{  
        myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
.
.
  myFile.print(",");
  myFile.println(ShrTemp);
         myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
}

You don't appear to be closing anything.

you are probably forming the files OK but failing to write to them

Every file.print() is queued into a buffer and physically written to the card with either file.close or file.flush. I don't see where you call either in your latest code.

See http://www.arduino.cc/en/Reference/SD