if else statements not executing as expected

I am working on a automatic watering plant system and I am trying to use moisturevalues from the potted plant to indicate if the soil has HIGH or LOW moisturevalues.
I want to turn on a red LED when the moisture is below the lowest accepted value called moistureLow.
I really dont get why the LED is not turning ON :confused:
My code is here:

 //Case project for SCE1213
//Plant monitoring

//Include libraries
#include <dht11.h>
#include <PhotoResistor.h>
#include <SoilMoisture.h>
#include <Ethernet.h>
#include <Time.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 120);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP): 
EthernetServer server(80);

// Declare pin numbers for measurements
//Analog inputs
int soilMoisturePin = A1;
int pResistorPin = A0;
//Digital inputs
int dht11Pin = 2;
//Digital outputs
int moistureVoltagePin = 7;
int pumpPin = 12;
int led1 = 13;
int led2 = 12;

// Declare time variables
float hours = 0;          // Change to float or long if using shorter intervals
const unsigned long interval = 0.005 * 60 * 1000UL; //0.5s for test else 30min
const unsigned long pumpInterval = 30 * 60 * 1000UL; //30min
unsigned long currentMillis = 0;
unsigned long prevMillis = 0;
unsigned long pumpMillis = 0;
unsigned long prevPumpMillis = 0;

// Declare measurement variables
int humidity = 0;
int temperature = 0;
int pResistorValue = 0;
unsigned long moistureValue = 0;

//Calibration variables
int moistureLow = 403; //Air
int moistureHigh = 1020; //Water
int lightLow = 1023; //Max voltage
int lightHigh = 0; //Min voltage


// Create class instances
dht11 DHT11;
PhotoResistor photoresistor(pResistorPin);
SoilMoisture soilmoisture(soilMoisturePin,moistureVoltagePin);

//Functions
  // Read temperature and humidity from DHT11
  void readDHT11()
    {
      int chk = DHT11.read(dht11Pin);
      humidity = DHT11.humidity;
      temperature = DHT11.temperature;
    }

      ///
  int lightIntensity()
  {
    pResistorValue = photoresistor.readValue();
    //pResistorValue = 7.251*exp(pResistorValue*0.0068); //exponential line fit to lux
    pResistorValue = 4*pow(10, -6)*pow(pResistorValue, 3) - 0.0016*pow(pResistorValue, 2) + 0.3867*pResistorValue - 0.5809;
    //pResistorValue = map(pResistorValue,lightLow,lightHigh,100,0);  // Mapping to 0-100%, remove for raw data
  }

    unsigned long moisture()
  {
    moistureValue = soilmoisture.readMoisture();
    moistureValue =  constrain(moistureValue, moistureLow, moistureHigh);  // Check constrain values for correct reading - remove for raw data
    moistureValue = map(moistureValue,moistureLow, moistureHigh,100,0);   // Map to 0-100% ? - remove for raw data
  }
  //  writetoSD.Initialize();
//Functions end

void setup() 
{
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());



  pinMode(led1, OUTPUT);
//Initialize
Serial.begin(9600);
Serial.println("Program startup....");
Serial.println("Time___moisture___light___humidity___temperature");
}

void loop() //Main program
{


  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
       Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>CP1-Automatic plant watering system width='100'</title>");
          client.println("</head>");
          client.print("<font color= 'blue'size=’20′> <h1>CP1:Sensor measurements</h1>");
          client.println("
"); //linebreak
          //client.println("<time></time>");
          client.print("<table border=1>");// Generating a table
          
          client.println("<tr>");// Defining a row in table  for units 
          
          client.print("<th>"); // Header cell
          client.print("Moisture:");// defining a cell  
          client.print("<th>");
          client.print("Lightintensity:"); // Header cell
         // client.print("<th/>");
          //client.println("
"); 
          //client.println((float)pResistorValue, 2);  
         // client.println("
");
          client.print("<th>");  // Header cell
          client.print("Temperature (C): ");
          client.print("<th>");  // Header cell
          client.print("Humidity: ");
          client.println("<tr />"); //end of unit row in the table 
          
          client.println("<tr>");// Defining a row in table for measurements continously updated measurement values 
          
          client.println("<td>"); // cell in table for moisture value
          client.println((float) moistureValue, 1); //updated moisture value
          
          client.println("<td>"); // cell in table for photoresistor value
          client.println((float)pResistorValue, 2); //updated photoresistor value
           
          client.println("<td>"); // cell in table for temperature value
          client.println((float)DHT11.temperature, 3);   //updated temperature value
          client.println("<td>"); // cell in table for humidity value
          client.println((float)DHT11.humidity, 4);  //updated humidity value
          client.println("<tr />"); // end of row 
          client.print("</table>"); //end table
          

          
          client.println("
");   
        //  }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
//           you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
  currentMillis = millis();
  pumpMillis = millis();

  if (moistureValue<=moistureLow)  // alarm detecting low moisturevalue
{
digitalWrite(led1,HIGH); // turn ON red LED
delay (1000);
}
 else   // indicating sufficent moisturevalue by turning on green light
  {
    digitalWrite(led2,HIGH); // turn ON green LED
       delay (1000);
}
  if (currentMillis - prevMillis >= interval) {
     prevMillis = currentMillis;
     hours = hours + 0.5;

    //Read DHT11 sensor data
    readDHT11();
    lightIntensity();
    moisture();
    
         Serial.println(String(hours) +"       " + String(moistureValue) + "        " + String(pResistorValue) + "        " + String(humidity) +
     "          " + String(temperature));

    float pumpTime = pumpMillis - prevPumpMillis;
     if(moistureValue < 50 && pumpTime >= pumpInterval){ //Check if low moisture value and time since pump last ran
     digitalWrite(pumpPin, HIGH);
     delay(10000); //Run pump for 10seconds
     digitalWrite(pumpPin, LOW);
     prevPumpMillis = pumpMillis; 
     }
     
     
  }

}

[code]

Have you tried printing the values being tested by the if ? This can oftten highlight problems with values not being as expected.

    Serial.println(String(hours) + "       " + String(moistureValue) + "        " + String(pResistorValue) + "        " + String(humidity) +
                   "          " + String(temperature));

Why are you messing around with Strings when you could print the values directly without conversion ?

    float pumpTime = pumpMillis - prevPumpMillis;Why a float when the values being worked on are unsigned longs

Why don't you ever turn off the LEDs ?

Please edit your post and change your quote tags around the code to code tags. It will make it easier for us to copy and analyse.

I dont understand why the program doesnt execute according to the if/else statement?

What should it do (probably obvious). What does it actually do ?

UKHeliBob:
What should it do (probably obvious). What does it actually do ?

Its pretty simple, if the moistureValue that I see beeing measured and read on the Serial Monitor is below predifined moistureLow then a red LED on my breadboard should go ON.That doesnt happend and I dont know why? It seems that the if/else loop just doesn't get executed.Something must be wrong with how I have set up the code, but I dont understand where.I tried putting this if/else statement elsewhere but it doesnt matter, nothing happens:

if (moistureValue<=moistureLow) 
{
digitalWrite(led1,HIGH); // turn ON red LED
delay (1000);
}
else   // indicating sufficient moisture value by turning on green light
  {
digitalWrite(led2,HIGH); // turn ON green LED
delay (1000);
}
[code]

Have you tried printing the values being tested by the if ? This can often highlight problems with values not being as expected.

You were previously given the advice in reply #1 to use Serial print statements in your code to examine the values of your variables.

Have you done that?

cattledog:
You were previously given the advice in reply #1 to use Serial print statements in your code to examine the values of your variables.

Have you done that?

I am quite new to this but yes I have.
I have managed to plot moistureLow value in the Serial Monoitor and it shows a value of 403 as defined.
Still when I am comparing moistureValue to moistureLow and I see on the Serial Monitor that moistureLow is way higher nothing happens.
Thats why it is confusing.
I assume I am suppose to see the serial prints on serial monitor

Have you confirmed that the Red led1 on pin 13 will blink when you run the basic "blink" example from the IDE?

If the led is indeed working as intended, you can monitor moistureValue as well as moistureLow and if you enter the conditional as follows

Serial.print("MoistureValue before conditional  ");
 Serial.printLn(moistureValue);
 Serial.print ("moistureLow before conditional  ");
 Serial.println (moistureLow);
 if (moistureValue <= moistureLow) // alarm detecting low moisturevalue
  {
    Serial.println("Entering moistureLow conditional");
    digitalWrite(led1, HIGH); // turn ON red LED
    delay (1000);
  }
  else   // indicating sufficent moisturevalue by turning on green light
  {
    digitalWrite(led2, HIGH); // turn ON green LED
    delay (1000);
  }

What do you see on the Serial monitor.

What do you see on the Serial monitor.

I see this:

MoistureValue before conditional 5
moistureLow before conditional 600

So moistureValue is way smaller then moistureLow which implies that condition is furfilled and the LED should come ON.

I have teted antoher smaller Sketch and managed to get this LED to turn on, so its not the LED anything is wrong with :-[

MoistureValue before conditional 5
moistureLow before conditional 600

Did you see "Entering moistureLow conditional" right after these statements?

cattledog:
Did you see "Entering moistureLow conditional" right after these statements?

Yes I do, it says 4 which is way lower then 600. It seems that this if/else loop just doesnt get executed somehow. I have tested the LED on other program and it blinks and works perfect. Really werid

If you have confirmed that you entered the if statement with the Serial print, then the issue is with the led.

It looks like you have the pinMode correct. Does the built in led on pin 13 illuminate when you enter the conditional statement?

You may want to try the led on a different pin than 13

cattledog:
If you have confirmed that you entered the if statement with the Serial print, then the issue is with the led.
You may want to try the led on a different pin than 13

Incredible, it seems its working now.I dont get why it wasnt by the PIN13

Hint: what else is pin 13 doing?

Now I have added another, green LED, and the funny thing is the redLED is working properly but not the green ?Is something wrong with the code?

if (moistureValue <= moistureLow) // alarm detecting low moisturevalue
  {
    Serial.println("Entering moistureLow conditional");
    digitalWrite(led1, HIGH); // turn ON red LED
    digitalWrite(led2, LOW); // turn OFF green LED
    delay (1000);
  }
  else if
    (moistureValue > moistureLow) // indicating sufficent moisturevalue by turning on green light
  {
   digitalWrite(led2, HIGH); // turn ON green LED
   digitalWrite(led1, LOW); // turn OFF red LED
    delay (1000);
  }
[code]

Now I have added another, green LED, and the funny thing is the redLED is working properly but not the green ?Is something wrong with the code?

Did you forget how to use the debug procedure you just went through? Are your values correct before the if statement? Are you entering the if statement block of code? If you are, and the led doesn't light, what could it be?

Did you forget this??

 pinMode(led2, OUTPUT);

Don't post snippets. I'm making a guess based on the code you were running in post #1, but you could be running something entirely different, and I'm wasting my time and yours.

cattledog:
Did you forget this??

 pinMode(led2, OUTPUT);

Don't post snippets. I'm making a guess based on the code you were running in post #1, but you could be running something entirely different, and I'm wasting my time and yours.

Yes I did forget that :(...and thanks a lot
I am wasn't aware regarding not posting the whole code, I see your point.I will post the whole code from now one, I just posted a snippet because I wanted to make it simpler to view for the people responding.

I am wasn't aware regarding not posting the whole code, I see your point.I will post the whole code from now one, I just posted a snippet because I wanted to make it simpler to view for the people responding.

Actually, the best thing to do when you have a problem with complex code, is to write the most simple sketch you can which compiles, runs, and demonstrates the problem. Very often, in the process of doing that you will find the problem.