code compiles and uploads but doesn't return data

I’m writing a code to automate my garden involving several sensors ( soil temp, soil moisture, light and humidity). The code uploads fine but, when I look at Serial monitor or the excel file … the time stamp and date don’t change. Also I have the code being saved to an SD card as a excel fine format (CSV).The file is created but, no new values are written to it. Any help would be greatly appreciated.

*sorry I didn’t know there was a 9000 char limit

stupid_garden2.ino (12.7 KB)

Sorry about the code. I’m new here and just figured out about inserting the code. I think this is correct

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

#define W5200_CS  10
#define SDCARD_CS 4
#define DHTTYPE DHT22
#define ECHO_TO_SERIAL 1 //Sends datalogging to serial if 1, nothing if 0
#define LOG_INTERVAL 1000 //milliseconds between entries (6 minutes = 360000)
#define ONE_WIRE_BUS_PIN 2

//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress ip(192,168,1,177);
//EthernetServer server(80);
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);

const int soilTempPin1 = A0;
const int soilTempPin2 = A1;
const int soilTempPin3 = A2;
const int soilTempPin4 = A3;

const int soilMoisturePin1 = A4;
const int soilMoisturePin2 = A5;
const int soilMoisturePin3 = A6;
const int soilMoisturePin4 = A7;

const int sunlightPin1 = A8;
const int sunlightPin2 = A9;
const int sunlightPin3 = A10;
const int sunlightPin4 = A11;

const int dhtPin = 2;

const int LEDPinGreen = 6;
const int LEDPinRed = 7;
const int solenoidPin = 3;
const int wateringTime = 600000; //Set the watering time (10 min for a start)
const float wateringThreshold = 15; //Value below which the garden gets watered

DHT dht(dhtPin, DHTTYPE);
RTC_DS1307 rtc;

float soilTemp_cucumbers = 0; //Scaled value of soil temp (degrees F)
float soilTemp_herbs = 0; //Scaled value of soil temp (degrees F)
float soilTemp_peppers = 0; //Scaled value of soil temp (degrees F)
float soilTemp_tomatoes = 0; //Scaled value of soil temp (degrees F)

float soilMoisture_Cucumbers = 0; //Raw analog input of soil moisture sensor (volts)
float soilMoisture_Herbs = 0; //Raw analog input of soil moisture sensor (volts)
float soilMoisture_Peppers = 0; //Raw analog input of soil moisture sensor (volts)
float soilMoisture_tomatoes = 0; //Raw analog input of soil moisture sensor (volts)

float soilMoisture1 = 0; //Scaled value of volumetric water content in soil (percent)
float soilMoisture2 = 0; //Scaled value of volumetric water content in soil (percent)
float soilMoisture3 = 0; //Scaled value of volumetric water content in soil (percent)
float soilMoisture4 = 0; //Scaled value of volumetric water content in soil (percent)
float humidity = 0; //Relative humidity (%)
float airTemp = 0; //Air temp (degrees F)
float heatIndex = 0; //Heat index (degrees F)
float sunlight1 = 0; //Sunlight illumination in lux
float sunlight2 = 0; //Sunlight illumination in lux
float sunlight3 = 0; //Sunlight illumination in lux
float sunlight4 = 0; //Sunlight illumination in lux
bool watering = false;
bool wateredToday = false;
DateTime now;
File logfile;

/*
Soil Moisture Reference
Air = 0%
Really dry soil = 10%
Probably as low as you'd want = 20%
Well watered = 50%
Cup of water = 100%
*/

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
  // red LED indicates error
  digitalWrite(LEDPinRed, HIGH);
  
  while(1);
}

void setup() {
  
  //Initialize serial connection
  Serial.begin(9600); //Just for testing
  Serial.println("Initializing SD card...");
  
   pinMode(W5200_CS, OUTPUT);
  //disconnect the W5200
    digitalWrite(W5200_CS,HIGH);
  
  pinMode(SDCARD_CS, OUTPUT); //Pin for writing to SD card
  
  
  //pinMode(chipSelect, OUTPUT); //Pin for writing to SD card
  pinMode(LEDPinGreen, OUTPUT); //LED green pint
  pinMode(LEDPinRed, OUTPUT); //LED red pin
  pinMode(solenoidPin, OUTPUT); //solenoid pin
  digitalWrite(solenoidPin, LOW); //Make sure the valve is off
  analogReference(EXTERNAL); //Sets the max voltage from analog inputs to whatever is connected to the Aref pin (should be 3.3v)
  
  //Establish connection with DHT sensor
  dht.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(SDCARD_CS)) {
    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("Unix Time (s),Date,Soil Temp 1(F),Soil Temp 2(F),Soil Temp 3(F),Soil Temp 4(F),Air Temp (F),Soil Moisture Content 1 (%),Soil Moisture Content 2 (%),Soil Moisture Content 3 (%),Soil Moisture Content 4 (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination 1 (lux),Sunlight Illumination 2 (lux),Sunlight Illumination 3 (lux),Sunlight Illumination 4 (lux),Watering?");   //HEADER 
#if ECHO_TO_SERIAL
  Serial.println("Unix Time (s),Date,Soil Temp 1(F),Soil Temp 2(F),Soil Temp 3(F),Soil Temp 4(F),Air Temp (F),Soil Moisture Content 1 (%),Soil Moisture Content 2 (%),Soil Moisture Content 3 (%),Soil Moisture Content 4 (%),Relative Humidity (%),Heat Index (F),Sunlight Illumination 1 (lux),Sunlight Illumination 2 (lux),Sunlight Illumination 3 (lux),Sunlight Illumination 4 (lux),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));
  
  //Three blinks means start of new cycle
  digitalWrite(LEDPinGreen, HIGH);
  delay(150);
  digitalWrite(LEDPinGreen, LOW);
  delay(150);
  digitalWrite(LEDPinGreen, HIGH);
  delay(150);
  digitalWrite(LEDPinGreen, LOW);
  delay(150);
  digitalWrite(LEDPinGreen, HIGH);
  delay(150);
  digitalWrite(LEDPinGreen, LOW);
  
  //Reset wateredToday variable if it's a new day
  if (!(now.day()==rtc.now().day())) {
    wateredToday = false;
  }
  
  now = rtc.now();
  
  // log time
  logfile.print(now.unixtime()); // seconds since 2000
  logfile.print(",");
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print(",");
 #if ECHO_TO_SERIAL
  Serial.print(now.unixtime()); // seconds since 2000
  Serial.print(",");
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(",");
#endif //ECHO_TO_SERIAL
  
   //Collect Variables
    soilTemp_cucumbers = analogRead(soilTempPin1);
    soilTemp_herbs = analogRead(soilTempPin2);
    soilTemp_peppers = analogRead(soilTempPin3);
    soilTemp_tomatoes = analogRead(soilTempPin4);
  delay(20);

  soilMoisture_tomatoes = analogRead(soilMoisturePin1) * (3.3 / 1024);
  delay(20);

  //Volumetric Water Content is a piecewise function of the voltage from the sensor
  if (soilMoisture_tomatoes < 1.1) {
    soilMoisture1 = (10 * soilMoisture_tomatoes) - 1;
  }
  else if (soilMoisture_tomatoes < 1.3) {
    soilMoisture1 = (25 * soilMoisture_tomatoes) - 17.5;
  }
  else if (soilMoisture_tomatoes < 1.82) {
    soilMoisture1 = (48.08 * soilMoisture_tomatoes) - 47.5;
  }
  else if (soilMoisture_tomatoes < 2.2) {
    soilMoisture1 = (26.32 * soilMoisture_tomatoes) - 7.89;
  }
  else {
    soilMoisture1 = (62.5 * soilMoisture_tomatoes) - 87.5;
  }

  soilMoisture_Cucumbers = analogRead(soilMoisturePin2) * (3.3 / 1024);
  delay(20);

  //Volumetric Water Content is a piecewise function of the voltage from the sensor
  if (soilMoisture_Cucumbers < 1.1) {
    soilMoisture2 = (10 * soilMoisture_Cucumbers) - 1;
  }
  else if (soilMoisture_Cucumbers < 1.3) {
    soilMoisture2 = (25 * soilMoisture_Cucumbers) - 17.5;
  }
  else if (soilMoisture_Cucumbers < 1.82) {
    soilMoisture2 = (48.08 * soilMoisture_Cucumbers) - 47.5;
  }
  else if (soilMoisture_Cucumbers < 2.2) {
    soilMoisture2 = (26.32 * soilMoisture_Cucumbers) - 7.89;
  }
  else {
    soilMoisture2 = (62.5 * soilMoisture_Cucumbers) - 87.5;
  }

  soilMoisture_Herbs = analogRead(soilMoisturePin3) * (3.3 / 1024);
  delay(20);

  //Volumetric Water Content is a piecewise function of the voltage from the sensor
  if (soilMoisture_Herbs < 1.1) {
    soilMoisture3 = (10 * soilMoisture_Herbs) - 1;
  }
  else if (soilMoisture_Herbs < 1.3) {
    soilMoisture3 = (25 * soilMoisture_Herbs) - 17.5;
  }
  else if (soilMoisture_Herbs < 1.82) {
    soilMoisture3 = (48.08 * soilMoisture_Herbs) - 47.5;
  }
  else if (soilMoisture_Herbs < 2.2) {
    soilMoisture3 = (26.32 * soilMoisture_Herbs) - 7.89;
  }
  else {
    soilMoisture3 = (62.5 * soilMoisture_Herbs) - 87.5;
  }

  soilMoisture_Peppers = analogRead(soilMoisturePin4) * (3.3 / 1024);
  delay(20);

  //Volumetric Water Content is a piecewise function of the voltage from the sensor
  if (soilMoisture_Peppers < 1.1) {
    soilMoisture4 = (10 * soilMoisture_Peppers) - 1;
  }
  else if (soilMoisture_Peppers < 1.3) {
    soilMoisture4 = (25 * soilMoisture_Peppers) - 17.5;
  }
  else if (soilMoisture_Peppers < 1.82) {
    soilMoisture4 = (48.08 * soilMoisture_Peppers) - 47.5;
  }
  else if (soilMoisture_Peppers < 2.2) {
    soilMoisture4 = (26.32 * soilMoisture_Peppers) - 7.89;
  }
  else {
    soilMoisture4 = (62.5 * soilMoisture_Peppers) - 87.5;
  }

  humidity = dht.readHumidity();
  delay(20);

  airTemp = dht.readTemperature(true);
  delay(20);

  heatIndex = dht.computeHeatIndex(airTemp, humidity);

  //This is a rough conversion that I tried to calibrate using a flashlight of a "known" brightness
  sunlight1 = pow(((((150 * 3.3) / (analogRead(sunlightPin1) * (3.3 / 1024))) - 150) / 70000), -1.25);
  sunlight2 = pow(((((150 * 3.3) / (analogRead(sunlightPin2) * (3.3 / 1024))) - 150) / 70000), -1.25);
  sunlight3 = pow(((((150 * 3.3) / (analogRead(sunlightPin3) * (3.3 / 1024))) - 150) / 70000), -1.25);
  sunlight4 = pow(((((150 * 3.3) / (analogRead(sunlightPin4) * (3.3 / 1024))) - 150) / 70000), -1.25);
  delay(20);

  //Log variables for 4 soil probes
  logfile.print(soilTemp_cucumbers);
  logfile.print(",");
  logfile.print(soilTemp_herbs);
  logfile.print(",");
  logfile.print(soilTemp_peppers);
  logfile.print(",");
  logfile.print(soilTemp_tomatoes);
  logfile.print(",");

  // Only 1 air sensor
  logfile.print(airTemp);
  logfile.print(",");

  // Log variables for 4 soil Moisture probes
  logfile.print(soilMoisture_Cucumbers);
  logfile.print(",");
  logfile.print(soilMoisture_Herbs);
  logfile.print(",");
  logfile.print(soilMoisture_Peppers);
  logfile.print(",");
  logfile.print(soilMoisture_tomatoes);
  logfile.print(",");

  logfile.print(humidity);
  logfile.print(",");
  logfile.print(heatIndex);
  logfile.print(",");

  logfile.print(sunlight1);
  logfile.print(",");
  logfile.print(sunlight2);
  logfile.print(",");
  logfile.print(sunlight3);
  logfile.print(",");
  logfile.print(sunlight4);
  logfile.print(",");

#if ECHO_TO_SERIAL
  Serial.print(soilTemp_cucumbers);
  Serial.print(",");
  Serial.print(soilTemp_herbs);
  Serial.print(",");
  Serial.print(soilTemp_peppers);
  Serial.print(",");
  Serial.print(soilTemp_tomatoes);
  Serial.print(",");

  // Only 1 air sensor
  Serial.print(airTemp);
  Serial.print(",");

  // variables for 4 soil Moisture probes
  Serial.print(soilMoisture_Cucumbers);
  Serial.print(",");
  Serial.print(soilMoisture_Herbs);
  Serial.print(",");
  Serial.print(soilMoisture_Peppers);
  Serial.print(",");
  Serial.print(soilMoisture_tomatoes);
  Serial.print(",");

  Serial.print(humidity);
  Serial.print(",");
  Serial.print(heatIndex);
  Serial.print(",");

  Serial.print(sunlight1);
  Serial.print(",");
  Serial.print(sunlight2);
  Serial.print(",");
  Serial.print(sunlight3);
  Serial.print(",");
  Serial.print(sunlight4);
  Serial.print(",");
#endif
  
  if ((soilMoisture1 < wateringThreshold) && (now.hour() > 19) && (now.hour() < 22) && (wateredToday = false)) {
    //water the garden
    digitalWrite(solenoidPin, HIGH);
    delay(wateringTime);
    digitalWrite(solenoidPin, LOW);
  
    //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);
 logfile.flush();
  delay(5000);
  }
}

You never seem to open a file for writing. Hence nothing on the card.

Start with a small sketch based on examples in SD Library

You have a long program with a huge amount of code in loop() rather than in smaller functions.

Have you tested the different parts separately - writing to the SD Card, reading each sensor, sending simple numbers to Excel etc?

If functions are short so you can see all the code in a function on the editing screen at one time it is much easier to grasp the logic of a program.

Have a look at Planning and Implementing a Program

...R

Thanks for the advice. It creates a CSV file on the SD card that I then transfer to my computer to open in excel. It’s doing all those things fine and returns values that I’m going to have to adjust later. The problem is the time stamp is reading the same every time. I’ve been trying to create a shorter code to test this but to no avail. Still rather new to programming.
The file is created here

// 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!
    }

I’ve attached a picture of the excel spreadsheet to give an idea what I mean.

Again thanks for all the advice. I’m new to posting for help and always get nervous about other people seeing my coding.

*** Also not all the sensors will be showing a value as not all the sensors are attached in the excel picture**