Comparison of Two Temperature Value

Hi all.This project so important for me.My project has 2 DHT11, 20X4 I2C LCD , ARDUINO UNO R3 , RELAY .Project can comparison values and read the two temperature.Values are Temperature 1(T1) and Temperature 2(T2).If T1 is bigger than T2,relay will open.If T1 is lower than T2,relay will close.If T1 is equal T2 ,relay will open then will close immediately.Im competed to read 2 temperature values.But I couldn't do other 2 situations.Im waiting your support.

#include <Wire.h> 
#include <dht11.h>
#include <LiquidCrystal_I2C.h>  // I2C LIBRARY
LiquidCrystal_I2C lcd(0x27,20,4); 
#define relay 7
#define DHT1_PIN 8
#define DHT2_PIN 9
dht11 DHT11;


void setup() {
  
  //INPUT AND OUTPUT
  pinMode(relay,OUTPUT);
  digitalWrite(relay,LOW);


  //LCD Ekranın ilk açılmasında yazacaklar 
   lcd.begin();
   lcd.setCursor(0,0);
   lcd.print("PROJEM 1"); 
   lcd.setCursor(0,1);
   lcd.print("NECMETTIN ERBAKAN U.");
   lcd.setCursor(0,2);
   lcd.print("NASUH OGUZHAN SENSOY");
   lcd.setCursor(0,3);
   lcd.print("13:25-12/04/20");
   delay(3000);
   lcd.clear();
}

void loop() { 
 int chk = DHT11.read(DHT1_PIN);//FIRST TEMPERATURE VALUE(T1)
 lcd.setCursor(0,0);
 lcd.print("SICAKLIK-1:");
 lcd.println((float)DHT11.temperature,0);
 lcd.setCursor(14,0);
 lcd.println("DERECE");
 
 delay(500);
 
 int chk2 = DHT11.read(DHT2_PIN);//SECOND TEMPERATURE VALUE(T2)
 lcd.setCursor(0,1);
 lcd.print("SICAKLIK-2:");
 lcd.println((float)DHT11.temperature,0);
 lcd.setCursor(14,1);
 lcd.println("DERECE");
 delay(100);

 //COMPARISON OF TEMPERATURE VALUES(CHK AND CHK2)
 if ( chk > chk2 )//T1 is bigger than T2
   DHT11.read(DHT1_PIN);
   DHT11.read(DHT2_PIN);
   true;
   digitalWrite(relay,HIGH);
   delay(100);

 //T2 is bigger than T2 and T1 = T2 ,I couldn't this situations
}

Your if statement is malformed, you need to encapsulate the code in curlybraces. After that is fixed, you need to use else:

if (condition1)
{
    do_this_if_condition1_is_true();
}
else if (condition2)
{
    do_this_if_condition2_is_true();
}
else
{
    do_this_if_no_conditions_are_true();
}
 if ( chk > chk2 )//T1 is bigger than T2

The chk and chk2 values are not the temperatures; they relate to the success or otherwise of the dht read. The example with the library shows them as DHTLIB_OK, DHTLIB_ERROR_CHECKSUM and so on.

hannah_mackinlay:

 if ( chk > chk2 )//T1 is bigger than T2

The chk and chk2 values are not the temperatures; they relate to the success or otherwise of the dht read. The example with the library shows them as DHTLIB_OK, DHTLIB_ERROR_CHECKSUM and so on.

Thanks.Im showing correctly temperatures (with chk and chk2 ).If the temperature values are not chk and chk2,how can i compare the temperatures?

I think you're showing the correct temperatures because of the .temperature's in the lcd prints, but you're comparing the chk's.

Danois90:
Your if statement is malformed, you need to encapsulate the code in curlybraces. After that is fixed, you need to use else:

if (condition1)

{
    do_this_if_condition1_is_true();
}
else if (condition2)
{
    do_this_if_condition2_is_true();
}
else
{
    do_this_if_no_conditions_are_true();
}

Thanks.I did what you said.But the relay is constantly opening and shuts down.

We can't see your new code.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>  // I2C LIBRARY
LiquidCrystal_I2C lcd(0x27,20,4);
#include <dht11.h> // DHT11 kütüphanesini ekliyoruz.
#define relay 7 //7.pine bağlı ROLE
#define DHT1_PIN 8 // 8.pine bağlı DHT11
#define DHT2_PIN 9 //9.pine bağlı DHT11
dht11 DHT11;

void setup() {
  
  //INPUT AND OUPUT
  pinMode(relay,OUTPUT);
  digitalWrite(relay,LOW);

  //GİRİŞ EKRANI
   lcd.begin();
   lcd.setCursor(0,0);
   lcd.print("PROJEM 1"); 
   lcd.setCursor(0,1);
   lcd.print("NECMETTIN ERBAKAN U");
   lcd.setCursor(0,2);
   lcd.print("NASUH OGUZHAN SENSOY");
   lcd.setCursor(0,3);
   lcd.print("13:25-12/04/20");
   delay(3000);
   lcd.clear();
}

void loop() { 
 int chk = DHT11.read(DHT1_PIN);//FIRST TEMPERATURE VALUE
 lcd.setCursor(0,0);
 lcd.print("SICAKLIK-1:");
 lcd.println((float)DHT11.temperature,0);
 lcd.setCursor(14,0);
 lcd.println("DERECE");
 
 delay(500);
 int chk2 = DHT11.read(DHT2_PIN); //SECOND TEMPERATURE VALUE
 lcd.setCursor(0,1);
 lcd.print("SICAKLIK-2:");
 lcd.println((float)DHT11.temperature,0);
 lcd.setCursor(14,1);
 lcd.println("DERECE");
 delay(100);
 if ( chk > chk2 )
  {
    DHT11.read(DHT1_PIN);
    DHT11.read(DHT2_PIN);
    digitalWrite(relay,HIGH);

  }
 else if( chk2 > chk )
  {
    DHT11.read(DHT1_PIN);
    DHT11.read(DHT2_PIN);
    digitalWrite(relay,LOW);
   
  }
 else
  {
    DHT11.read(DHT1_PIN);
    DHT11.read(DHT2_PIN);
    digitalWrite(relay,LOW);
    delay(150);
    digitalWrite(relay,HIGH); 
 
 
}
}
 if ( chk > chk2 )

You're still not comparing the temperatures....

Why don't you do a serial print of those and see what they are? If both reads were clean, the return values chk and chk2 will both be DHTLIB_OK, and always equal.

Side issue:

Is it correct / acceptable / good practice to do two dht reads on different pins, from the same instance?

dht11 DHT11;
..
 int chk = DHT11.read(DHT1_PIN);
..
 int chk2 = DHT11.read(DHT2_PIN);

I would have been inclined to make two:

dht11 DHT11_1;
dht11 DHT11_2;

But I suppose it's ok since OP says the values displayed are correct.

The temperature printed to the LCD is nether of the chk values:

lcd.println((float)DHT11.temperature,0);

That is why your relay cannot work as you expect it to.

Danois90:
The temperature printed to the LCD is nether of the chk values:

I've been saying that for a while now.

According to my test, the return value from a good read, DHTLIB_OK, is 0.

OP you are continuing to compare the return values, which for good reads are both 0.

Guys i understand what you say, but I couldn't write the code

You already display the temperatures in the lcd.print() lines, so you know where they come from.

So before you do the display, read those .temperatures into 2 more variables:

int chk = DHT11.read(DHT1_PIN);//FIRST TEMPERATURE VALUE
float T1=DHT11.temperature;
..
int chk2 = DHT11.read(DHT2_PIN); //SECOND TEMPERATURE VALUE
float T2=DHT11.temperature;

... then compare T1 and T2 rather than chk and chk2.

(Assuming it's ok to read two values from the two pins with the same instance; see my "side issue" above.)

Please read the example sketches that come with the DHT11 library.

@aarg, do you have a view on my "side issue" earlier?

Thanks hannah_mackinlay,Danois90,TheMemberFormerlyKnownAsAWOL,aarg.I tried and I got no errors.
I can successfully do 3 situations.Thanks for all.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>  //
LiquidCrystal_I2C lcd(0x27,20,4); 
#include <dht11.h> // DHT11 kütüphanesini ekliyoruz.
#define DHT11_1  8 // 8.pine bağlı DHT11
#define DHT11_2  9 //9.pine bağlı DHT11
#define relay 7
dht11 DHT11;

void setup() {
  
  //INPUT AND OUTPUTS
  pinMode(relay,OUTPUT);
  digitalWrite(relay,LOW);
  //LCD Ekranın ilk açılmasında yazacaklar 
   lcd.begin();
   lcd.setCursor(0,0);
   lcd.println("EEM TASARIM TEKNIGI"); 
   lcd.setCursor(0,1);
   lcd.print("DHT ILE ROLE KONTROLU");
   lcd.setCursor(0,2);
   lcd.print("NASUH OGUZHAN SENSOY");
   lcd.setCursor(0,3);
   lcd.print("16:25-12/04/20");
   delay(2000);
   lcd.clear();
}

void loop() { 
 int chk = DHT11.read(8);
 float T1=DHT11.temperature;//FIRST TEMPERATURE VALUE
 lcd.setCursor(0,0);
 lcd.print("SICAKLIK-1:");
 lcd.println(T1,0);
 lcd.setCursor(14,0);
 lcd.println("DERECE");
 
 delay(500);
 int chk2 = DHT11.read(9); 
 float T2=DHT11.temperature;//SECOND TEMPERATURE VALUE
 lcd.setCursor(0,1);
 lcd.print("SICAKLIK-2:");
 lcd.println(T2,0);
 lcd.setCursor(14,1);
 lcd.println("DERECE");
 delay(100);

if ( T1 > T2 )
{
  digitalWrite(relay,HIGH);

}
else if ( T2 > T1 )
{
  digitalWrite(relay,LOW);
}
else
{
  digitalWrite(relay,HIGH);
  delay(200);
  digitalWrite(relay,LOW);
} 
 
}

Glad to have helped....

But you need to look at this logic:

else
{
  digitalWrite(relay,HIGH);
  delay(200);
  digitalWrite(relay,LOW);
}

The low is pretty much going to be ignored if the else is still invoked next time through loop(), since it will go low and immediately back to high. I wonder if a mechanical relay will even have a chance to react.

Then, assuming either that's right, or you need a delay() after the low, do you actually want the relay to oscillate high and low while the values T1 and T2 are equal. (Not that 2x floats are ever likely to be seen as identical anyway....)

hannah_mackinlay:
Glad to have helped....

But you need to look at this logic:

else

{
 digitalWrite(relay,HIGH);
 delay(200);
 digitalWrite(relay,LOW);
}




The low is pretty much going to be ignored if the else is still invoked next time through loop(), since it will go low and immediately back to high. I wonder if a mechanical relay will even have a chance to react.

Then, assuming either that's right, or you need a delay() after the low, do you actually want the relay to oscillate high and low while the values T1 and T2 are equal. (Not that 2x floats are ever likely to be seen as identical anyway....)

You are telling truth. When T1 and T2 values are equal, I can request the relay to swing high and low.

But you probably want a delay() after the low?

It's going to be difficult to test in real life, since I think it's unlikely that floats from the 2 sensors will be deemed equal; not for long anyway. Perhaps you should // out the part where it actually puts the temps into T1 and T2, and hardcode them as identical values and see if it behaves as you wish:

//float T1=DHT11.temperature;//FIRST TEMPERATURE VALUE
float T1=50; 
..
 //float T2=DHT11.temperature;//SECOND TEMPERATURE VALUE
float T2=T1;

(You may want to consider some hysteresis here, and rather check if T1 and T2 are within (say) 2 or 3 degrees of each other?)

Yes,i want a delay() after the low.Im trying this project with Proteus 8 now.