Help needed with multiple conditions in Greenhouse project

Dear Arduino users,

since this is my first post, even though I have read and enjoyed in many posts here at the forum, where I have found plenty useful information, since I started to use arduino, I am not sure if I posted in right place.

However, I am preparing usage of arduino in existing greenhouse, which is currently controlled by PLC, but with simple controls. Motors (24V) are already in place with DPDT relays for start and direction change, and only control is done using mentioned PLC with temperature sensors for opening and closing the sidewall curtains.

My idea is to have following sensors for input: temperature, humidity, soil moisture, light, etc, and to be able to control motors to open the sidewall curtains, fans, sprinklers, light, watering, etc. And of course, to be able to control it via web interface and mobile devices. Some of mentioned idea are for the upgraded versions, but right now I need to have it running/controlling properly.

Project is based on already purchased: Arduino Mega, sensor shield, relay board, Ethernet shield, sensors, solenoid valves.

Code that I have prepared is working properly when using individual for different sensors and outputs.

However, I am currently experiencing some problems when using multiple conditions if some output is previously HIGH. I am sure that this might be trivial issue, and I have missed some obvious ‚Äúrule‚ÄĚ when writing the code, but I have spent hours reading materials and testing, but simply I cannot found solution.

Concrete problem is when output 2 (that controls the fan) is HIGH previously triggered by temp sensor, and when than when humidity is above set condition, and tries to write HIGH again to the same output, output pin on relay board is flickering.

Again, I am sure that this is trivial, and I need to revise my code again, I would really appreciate some guide on how to solve this problem.

Please found below my code:

#include <OneWire.h>
#include "DHT.h"
#include <DallasTemperature.h>

OneWire oneWire(18);

#define DHTPIN 21 
#define DHTTYPE DHT22

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1  14  // Arduino Digital Output pins for relay 1 - DPDT relay for motor control
#define Relay_2  15  // Arduino Digital Output pins for relay 2 - Fan
#define Relay_3  16  // Arduino Digital Output pins for relay 3 - Sprinkler
#define Relay_4  17  // Arduino Digital Output pins for relay 4 - Pump

int setpointD = 18; // Reference Temperature for sensor Dallas DS18B20 
int thresholdD = 7; // Temperature difference for sensor Dallas DS18B20 

int setpointDHT = 18; // Reference Temperature for sensor DHT22
int thresholdDHT = 7; // Temperature difference for sensor  Dallas DHT22

DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  sensors.begin();
  Serial.begin(9600);

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  

  //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset

  dht.begin();

}
void loop()

{
  sensors.requestTemperatures();
  float currentTempDallas1;
  currentTempDallas1 = sensors.getTempCByIndex(0);
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  //Condition for triggering motor for opening sidewall curtains using relay 1, which should be initiated if either of temp sensors is higher than set value
  if (currentTempDallas1 > setpointD+thresholdD || t > setpointDHT+thresholdDHT)
  {
    digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sidewall curtains up");
    Serial.print("\t");
  }
  if (currentTempDallas1 < setpointD || t < setpointDHT)
  {
    digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sidewall curtains down");
    Serial.print("\t");
  }

  //Condition for triggering the fan using relay 2, which should be initiated if either of temp sensors or humidity is higher than set value
  if (currentTempDallas1 > setpointD+thresholdD || h > 75)
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  if (currentTempDallas1 < setpointDHT || h < 75)
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

  //Condition for triggering the sprinkler using relay 3, which should be initiated if humidity is higher than set value
  if (h > 90)
  {
    digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sprinkler ON");
    Serial.print("\t");
  }
  else
  {
    digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sprinkler OFF");
    Serial.print("\t");
  }

    Serial.print("\t");
    Serial.print("Temperature Dallas1: ");
    Serial.print("\t");
    Serial.println(currentTempDallas1,2);

  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } 
  else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  }
  delay(1000);
}

I have tried some solution I have found using case/switch but couldn‚Äôt get it working and also some solution with simple if statements for different scenarios writing either HIGH or LOW, but each time arduino goes through the loop, it ‚Äúclicks‚ÄĚ trying to set pin even if it had same state as requested, which is not goo solution at all.

Also, I think that this arduino community if one of the greatest I have seen :slight_smile:

Many thanks again.

A

use of extra () might be wise as I never trust the precedence of operators …

if (currentTempDallas1 > setpointD+thresholdD || t > setpointDHT+thresholdDHT)

==>

if ( ( currentTempDallas1 > (setpointD+thresholdD)) || (t > (setpointDHT+thresholdDHT) ) )

etc

furthermore if you use h<75 in one place you might need h>= 75 in the other?

Dear Rob,

many thanks for your prompt reply and your involvement, as sincerely, I have read many of your topics and replies to the topics, and really enjoyed it and learned a lot from it, so I do appreciate your help.

I will try this now and test it, but with some values more realistic to trigger the change in my room now using my fingers and breath :slight_smile:

With regards to humidity condition I hoped I got it covered at the beginning of if statement:

//Condition for triggering the fan using relay 2, which should be initiated if either of temp sensors or humidity is higher than set value
if (currentTempDallas1 > setpointD+thresholdD || h > 75)
{
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
Serial.print("\t");
Serial.print(‚ÄúFan ON‚ÄĚ);
Serial.print("\t");
}
if (currentTempDallas1 < setpointD || h < 75)
{
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
Serial.print("\t");
Serial.print(‚ÄúFan OFF‚ÄĚ);
Serial.print("\t");
}

I have just noticed that I used setpoint of DHT sensor so I have changed it, but I would eventually add both temp sensors in the condition, when I get this working.

Do you think it is set properly or do use need to use some ‚Äúindirect‚ÄĚ container for storing current values?

Thanks again.

first get the sensors working, can you check it ?

#include <OneWire.h>
#include "DHT.h"
#include <DallasTemperature.h>

OneWire oneWire(18);

#define DHTPIN 21 
#define DHTTYPE DHT22

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1  14  // Arduino Digital Output pins for relay 1 - DPDT relay for motor control
#define Relay_2  15  // Arduino Digital Output pins for relay 2 - Fan
#define Relay_3  16  // Arduino Digital Output pins for relay 3 - Sprinkler
#define Relay_4  17  // Arduino Digital Output pins for relay 4 - Pump

int setpointD = 18; // Reference Temperature for sensor Dallas DS18B20 
int thresholdD = 7; // Temperature difference for sensor Dallas DS18B20 

int setpointDHT = 18; // Reference Temperature for sensor DHT22
int thresholdDHT = 7; // Temperature difference for sensor  Dallas DHT22

DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  sensors.begin();
  Serial.begin(9600);

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  

  //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    

  delay(4000); //Check that all relays are inactive at Reset

  dht.begin();

}
void loop()

{
  sensors.requestTemperatures();
  float currentTempDallas1 = sensors.getTempCByIndex(0);
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  Serial.println(currentTempDallas1 , 1);
  Serial.println(h, 1);
  Serial.println(t, 1);

  delay(2000);
}

small patch to your code,

#include <OneWire.h>
#include "DHT.h"
#include <DallasTemperature.h>

OneWire oneWire(18);

#define DHTPIN 21 
#define DHTTYPE DHT22

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1  14  // Arduino Digital Output pins for relay 1 - DPDT relay for motor control
#define Relay_2  15  // Arduino Digital Output pins for relay 2 - Fan
#define Relay_3  16  // Arduino Digital Output pins for relay 3 - Sprinkler
#define Relay_4  17  // Arduino Digital Output pins for relay 4 - Pump

int setpointD = 18; // Reference Temperature for sensor Dallas DS18B20 
int thresholdD = 7; // Temperature difference for sensor Dallas DS18B20 

int setpointDHT = 18; // Reference Temperature for sensor DHT22
int thresholdDHT = 7; // Temperature difference for sensor  Dallas DHT22

DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  sensors.begin();
  Serial.begin(9600);

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  

  //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset

  dht.begin();

}
void loop()

{
  sensors.requestTemperatures();
  float currentTempDallas1;
  currentTempDallas1 = sensors.getTempCByIndex(0);
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Condition for triggering motor for opening sidewall curtains using relay 1, 
  // which should be initiated if either of temp sensors is higher than set value
  if ((currentTempDallas1 > (setpointD + thresholdD)) || (t > (setpointDHT + thresholdDHT)))
  {
    digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sidewall curtains up");
    Serial.print("\t");
  }
  else
  {
    digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sidewall curtains down");
    Serial.print("\t");
  }

  // Condition for triggering the fan using relay 2, 
  // which should be initiated if either of temp sensors or humidity is higher than set value
  if ( (currentTempDallas1 > setpointD+thresholdD) || (h > 75) )
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  else
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

  // Condition for triggering the sprinkler using relay 3, 
  // which should be initiated if humidity is higher than set value
  if (h > 90)
  {
    digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sprinkler ON");
    Serial.print("\t");
  }
  else
  {
    digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sprinkler OFF");
    Serial.print("\t");
  }
  Serial.println();

  delay(10000);
}

As you might noticed, I have set the same condition for both relay 1 and 2 just to check if it would trigger the relay, and it should be changed to higher temp value.

However, now Relay 1 is HIGH and Relay 2 is just flickering as it goes through loop, please see picture attached.

Also, see print from serial:

Sidewall curtains up Fan ON Fan OFF Sprinkler OFF Temperature Dallas1: 26.75
Humidity: 50.80 % Temperature: 22.50 *C

But when humidity is higher, than both Relay 1 and Relay 2 is HIGH:

Sidewall curtains up Fan ON Sprinkler OFF Temperature Dallas1: 28.81
Humidity: 58.80 % Temperature: 23.00 *C

Concrete problem is when output 2 (that controls the fan) is HIGH previously triggered by temp sensor, and when than when humidity is above set condition, and tries to write HIGH again to the same output, output pin on relay board is flickering.

I would suggest only writing to any output (changing the relay status) when a condition BECOMES true, not when it IS true ‚Ķ similar to the Blink without Delay example. Therefore, your triggers would become ‚Äúone-shot‚ÄĚ and the code would never waste time repetetively writing to outputs that don‚Äôt need to be changed. As a bonus, you would be able to eliminate using delay() during which inputs cannot be monitored and code cannot be processed.

Some pseudo code that could be simplified or combined:

  //Condition for triggering the fan using relay 2, initiated if any temp sensor is higher than set value
  if ((currentTempDallas1 > setpointD + thresholdD) && (previousTempDallas1 <= setpointD + thresholdD))
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  if ((currentTempDallas1 < setpointDHT) && (previousTempDallas1 >= setpointDHT))
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

  //Condition for triggering the fan using relay 2, initiated if humidity sensor is higher than set value
  if ((h > 75) && (hPrevious <= 75))
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  if ((h < 75) && (hPrevious >= 75))
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

Fan ON Fan OFF

that means that both conditions are true

 if (currentTempDallas1 > setpointD+thresholdD || h > 75)  <<<<<<<<<<<<< TRUE
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  if (currentTempDallas1 < setpointD || h < 75)  <<<<<<<<<<<<< TRUE
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

that can be that the first condition the dallastemp is TRue and in the other the humidity part is true.
==> your logic fails.

Sorry for this later reply, but my baby daughter came home, and I had to put her to sleep.

I must say that I feel stupid now :slight_smile: , but I assumed from the beginning that I have missed something. Originally, I've wrote real values on piece of paper, but with many tests and changes I have missed logic.

I would now test it with some values and also using your other suggestions, and I think it would be best to put value higher than first condition rather than any variable, and will let you know.

And for the else statement in first condition, due to variation of the temperature, I have left another if intentionally, so basically motors will open sides when temperature (with this temp values) is higher than 25 C, and will close if temp is less than 22 (and not 25). Real values would be 18C + 7C = 25C to open, and 18 to close.

Again, I really appreciate your assistance, as I always think it is good first to learn from your and others mistakes but sometimes (like in this case), I am missing obvious logic :slight_smile:

Thank you very much and I will post results.

Sorry, but here is another maybe another stupid question. How do I declare previous state

if ((currentTempDallas1 > setpointD + thresholdD) && (previousTempDallas1 <= setpointD + thresholdD) || (h > 52))

as I have received error:

‚ÄėpreviousTempDallas1‚Äô was not declared in this scope

You could try a layout like this:

// declare variables
float currentTempDallas1;
float previousTempDallas1;
float h;
float hPrevoious;
float t;
float tPrevoious;

void setup() {
  // your setup code
}

void loop() {
  //take current readings
  sensors.requestTemperatures();
  currentTempDallas1 = sensors.getTempCByIndex(0);
  h = dht.readHumidity();
  t = dht.readTemperature();

  // your code (conditions for triggering, etc.)

  //update previous readings
  previousTempDallas1 = currentTempDallas1;
  hPrevoious = h;
  tPrevoious = t;
}

For your conditions for triggering, I would get the conditions for temperature and humidity working separately first, in separate sections of code, as you currently have failed logic when combining them as noted by robtillaart in reply#7.

Many thanks for your help. Please note that yesterday, after I've changed values to "reasonable and logic" one for testing purpose, it worked great. If conditions set earlier, would not have problem if they were used in real system with real values, but for testing I've overlapped to conditions, as Rob's sharp eye noticed :-), and again I feel dumb :slight_smile:

But, both of answers contributed to better understanding and improvement of the system, and I would try to incorporate your suggestions, as you've both said it should work better. Robs useful comment on when to make some action is great.

I will keep you posted and many thanks again.

Hi,

following the suggestions, I have modified my code to include all scenarios, but now I am having situation when one of temp, even satisfies the condition to write HIGH to Relay 2, it doesn’t. If changing temp individually, both Dallas and DHT sensor triggers the relay 2. I have tried to check the conditions in the code but doesn’t see the problem.

Please see below output from serial monitor:

As you can see from this example, Temperature Dallas1 goes down from 26.50 to 25.12, it turns fan OFF, even DHT Temp is 26.50

Temperature Dallas1: 26.50
DHT Humidity: 94.60 % DHT Temperature: 26.50 *C
Fan OFF
Temperature Dallas1: 25.12
DHT Humidity: 88.90 % DHT Temperature: 26.50 *C

In second test, if DHT Temperature decrease from 26.00 to 25.90, it turns fan OFF, which it should do, but in this case Dallas temp is lower than set value:
Temperature Dallas1: 23.19
DHT Humidity: 47.20 % DHT Temperature: 26.00 *C
Fan OFF
Temperature Dallas1: 23.19
DHT Humidity: 46.50 % DHT Temperature: 25.90 *C

Also, same situation happens if humidity goes down below conditional value, and it turns Relay 2 OFF, independently of Temp condition.
I’d checked the code and the values are set properly, and I can not found the reason for this behavior.
Could you please take a look and see if I am missing something.

Originally, I have used || ‚Äúor‚ÄĚ statement, but as proposed by Rob, I have separated the conditions (although I think he proposed it in situation of one temp sensor and humidity, but with two temp sensors it complicates situation). Do you think it would be better if I include ‚Äúor‚ÄĚ statements to same condition for writing to output pin?

Your assistance would be highly appreciated.

Many thanks in advance and please see code below:

#include <OneWire.h>
#include "DHT.h"
#include <DallasTemperature.h>

OneWire oneWire(18);

#define DHTPIN 21 
#define DHTTYPE DHT22

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1  14  // Arduino Digital Output pins for relay 1 - DPDT relay for motor control
#define Relay_2  15  // Arduino Digital Output pins for relay 2 - Fan
#define Relay_3  16  // Arduino Digital Output pins for relay 3 - Sprinkler
#define Relay_4  17  // Arduino Digital Output pins for relay 4 - Pump

int setpointD = 22; // Reference Temperature for sensor Dallas DS18B20 
int thresholdD = 3; // Temperature difference for sensor Dallas DS18B20 
int setpointDHT = 22; // Reference Temperature for sensor DHT22
int thresholdDHT = 3; // Temperature difference for sensor  Dallas DHT22

// declare variables
float currentTempDallas1;
float previousTempDallas1;
float h;
float hPrevious;
float t;
float tPrevious;

DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  sensors.begin();
  Serial.begin(9600);

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  

  //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset

  dht.begin();

}
void loop()

{
  sensors.requestTemperatures();
  currentTempDallas1 = sensors.getTempCByIndex(0);
  h = dht.readHumidity();
  t = dht.readTemperature();

  // Condition for triggering motor for opening sidewall curtains using relay 1, 
  // which should be initiated Dallas temp sensor is higher than set value
  if (((currentTempDallas1 > (setpointD + thresholdD)) && (previousTempDallas1 <= (setpointD + thresholdD))))
  {
    digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sidewall curtains up");
    Serial.print("\t");
  }
  // Condition for triggering motor for closing sidewall curtains using relay 1, 
  // which should be initiated Dallas temp sensor is lower than set value
  if ((currentTempDallas1 < setpointD) && (previousTempDallas1 >= setpointD)) 
  {
    digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sidewall curtains down");
    Serial.print("\t");
  }

  // Condition for triggering motor for opening sidewall curtains using relay 1, 
  // which should be initiated DHT22 temp sensor is higher than set value
  if ((t > (setpointDHT + thresholdDHT)) && (tPrevious <= (setpointDHT + thresholdDHT)))
  {
    digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sidewall curtains up");
    Serial.print("\t");
  }
  // Condition for triggering motor for closing sidewall curtains using relay 1, 
  // which should be initiated DHT temp sensor is lower than set value
  if ((t < setpointDHT) && (tPrevious >= setpointDHT))
  {
    digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sidewall curtains down");
    Serial.print("\t");
  }
  
  // Condition for turning ON the fan using relay 2, 
  // which should be initiated if Dallas temp sensor is higher than set value
  if ((currentTempDallas1 > 26) && (previousTempDallas1 <= 26))
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  // Condition for turning OFF the fan using relay 2, 
  // which should be initiated if Dallas temp sensor is lower than set value 
  if ((currentTempDallas1 < 26) && (previousTempDallas1 >= 26))
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }
 
  // Condition for turning ON the fan using relay 2, 
  // which should be initiated if DHT temp sensor is higher than set value
  if ((t > 26) && (tPrevious <= 26))
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  }
  // Condition for turning OFF the fan using relay 2, 
  // which should be initiated if DHT temp sensor is lower than set value 
  if ((t < 26) && (tPrevious >= 26))  
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }

  // Condition for turning ON the fan using relay 2, 
  // which should be initiated if DHT humidity sensor is higher than set value
   if ((h > 60) && (hPrevious <= 60))
  {
    digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Fan ON");
    Serial.print("\t");
  } 
  // Condition for turning OFF the fan using relay 2, 
  // which should be initiated if DHT humidity sensor is lower than set value  
   if ((h < 60) && (hPrevious >= 60))  
  {
    digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Fan OFF");
    Serial.print("\t");
  }
  
  // Condition for triggering the sprinkler using relay 3, 
  // which should be initiated if humidity is higher than set value
  if ((h > 80) && (hPrevious <= 80))
  {
    digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
    Serial.print("\t");
    Serial.print("Sprinkler ON");
    Serial.print("\t");
  }
  if ((h < 80) && (hPrevious >= 80))
  {
    digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
    Serial.print("\t");
    Serial.print("Sprinkler OFF");
    Serial.print("\t");
  }
  Serial.println();

  Serial.print("\t");
  Serial.print("Temperature Dallas1: ");
  Serial.print(currentTempDallas1,2);
  Serial.println(" *C\t");
  
  //update previous readings
  previousTempDallas1 = currentTempDallas1;
  hPrevious = h;
  tPrevious = t;

  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } 
  else {
    Serial.print(" DHT Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("DHT Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  }
  delay(1000);
}

why do you use 2 temp sensors?
do they have a separate purpose?

1 Like

Hi Rob,

they would be set at different positions in the greenhouse, but I was even thinking to live without it :slight_smile: , and to simply use only Dallas for temp (as I think it might be better).

Putting values on paper, I have just noticed that the problem is with previous values:

if ((currentTempDallas1 > 26) && (previousTempDallas1 <= 26))
{
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON

if ((currentTempDallas1 < 26) && (previousTempDallas1 >= 26))
{
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF

If currentTempDallas1 = 25.12 and previousTempDallas1 = 26.5, it turns OFF,

but for other DHT temp sensor:

if ((t > 26) && (tPrevious <= 26))
{
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
if ((t < 26) && (tPrevious >= 26))
{
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF

since t=26.50 and tPrevious=26.50, and tPrevious doesn’t meet the condition, no change have been detected, and it doesn’t digitalWrite HIGH!
So, I will have to look for another solution, and if someone has solution for this, please suggest :slight_smile:

But now, when I am thinking, even if I use only one temp sensor, since the fans is ON, if temp is higher than some value, or if humidity is also higher than some value, in case that temp is lower, going through loop, it will turn it OFF, and if humidity is still above set value, since previous condition will not be meet, it will not turn fan ON.

I think that statement with previous value will not work.

Do you maybe have some suggestion?

Thanks!

get your requirements 100% clear. There are 8 conditions:

example

        TEMP1   TEMP2   HUM         FAN     HEATER 
        ---------------------------------------------
        HIGH    HIGH    HIGH   ==>  ON      OFF
        HIGH    HIGH    LOW    ==>  ON      OFF 
        HIGH    LOW     HIGH   ==>  ON      OFF
        HIGH    LOW     LOW    ==>  ON      OFF 
        LOW     HIGH    HIGH   ==>  ON      ON
        LOW     HIGH    LOW    ==>  ON      ON
        LOW     LOW     HIGH   ==>  OFF     ON
        LOW     LOW     LOW    ==>  OFF     ON

such a table defines what the code logic should do

in the above example one can derive the following code
note that heater is just the opposite of temp1 ==>
if (TEMP1 > value) digitalWrite(HEATER, LOW)
else digitalWrite(HEATER, HIGH)
for the fan it is a bit more complex
IF (TEMP1 < value) && (temp2 < value) digitalWrite(FAN, LOW)
else digtalWrite(FAN, HIGH):
note that the humidity is in fact not used to control anything in this example

Thanks for sharing the logic diagram, as it is great "tool" for making conditions set right.

Although I have not set conditions in my scenario this way, I will try to do it.

However, I think I have finally set conditions properly, except that "previous" readings make confusion since I am writing to the same output.

Would you recommend using "or - ||" statement with "previous", as I think that way I would have it covered.

Thanks again for your assistance!

However, I think I have finally set conditions properly, except that "previous" readings make confusion since I am writing to the same output.

Feeling responsible for the "previous" confusion. This makes each condition perform a one-shot changes to the fan or heater ON/OFF status. I see you're making progress as you have the conditions working OK separately - correct?

Would you recommend using "or - ||" statement with "previous", as I think that way I would have it covered.

I would think "or - ||" statements would work if used correctly, but "and - &&" would be much more difficult.

Monitoring previous readings help for debouncing signals or speeding up code but add to complexity (and confusion). I'm thinking now that your application may not require this.

If you comment out or remove the "previous" portions and the conditions still work OK independently, then the conditions would be simpler to combine with ||, &&, or whatever logic required ... refer to robtillaart's table and example, then create your own.

Good luck - dlloyd

using previous is more of the same - just see it as another input

        PREV1   TEMP1   TEMP2   HUM         FAN     HEATER 
        ----------------------------------------------------
        HIGH    HIGH    HIGH    HIGH   ==>  ON      OFF
        HIGH    HIGH    HIGH    LOW    ==>  ON      OFF 
        HIGH    HIGH    LOW     HIGH   ==>  ON      OFF
        HIGH    HIGH    LOW     LOW    ==>  ON      OFF 
        HIGH    LOW     HIGH    HIGH   ==>  ON      ON
        HIGH    LOW     HIGH    LOW    ==>  ON      ON
        HIGH    LOW     LOW     HIGH   ==>  OFF     ON
        HIGH    LOW     LOW     LOW    ==>  OFF     ON 

        LOW     HIGH    HIGH    HIGH   ==>  ON      OFF
        LOW     HIGH    HIGH    LOW    ==>  ON      OFF 
        LOW     HIGH    LOW     HIGH   ==>  ON      ON
        LOW     HIGH    LOW     LOW    ==>  ON      ON 
        LOW     LOW     HIGH    HIGH   ==>  ON      ON
        LOW     LOW     HIGH    LOW    ==>  ON      ON
        LOW     LOW     LOW     HIGH   ==>  OFF     ON
        LOW     LOW     LOW     LOW    ==>  OFF     ON

you might use a Karnaugh solver to get the formulas right

example translation of the formula’s
F(AB) = A + B ==> if (A || B)
F(AB) = AB ==> if (A && B)
F(ABCD)= !A C D + !A B C ==> if (!A && C && D) || (!A && B && C )

try some simple formulas first …