Having trouble with a simple "if" function

Hello! I’m having trouble to turn on a relay based on a temperature reading from a dht sensor and I don’t quite understand why, the problem seems to be inside the if function, if I remove the if functions and just make it turn on and off every 10 seconds it does it perfectly but as soon as I add the if it stops working, it must be something super silly but I can’t find the issue.

I tried using a do while loop with the same result, I tried using int instead of float for the temperature reading, tried using a different variable to indirectly compare the temperature and still doesn’t work.

Here is the code:

#include “DHT.h”

#define DHTTYPE DHT11

const int DHTPin = 2;
const int rele = 8;
int estado;
DHT dht(DHTPin, DHTTYPE);
int temp = 20;

void setup() {
Serial.begin(9600);
pinMode(rele, OUTPUT);
dht.begin();
}

void loop(){
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!

int h = dht.readHumidity();
int t = dht.readTemperature();
temp =t;
if (isnan(h) || isnan(t)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print("Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(estado);
Serial.println(” ");
Serial.println(temp);

if(temp <= 30){

digitalWrite(rele, HIGH); // relay on
estado=1;
delay(10000); // esperar 10 segundos
}
if(temp > 30){
digitalWrite(rele, LOW); // relay off
estado=0;
delay(10000); // esperar 10 segundos

}
}

Thank you for taking your time to help!

.

Ken_F:
I believe your problem is one I had fallen victim to myself.

Nope.

Your problem is that you're reading a float into an int and then testing the int to see if it is NaN. Doesn't make sense...

@lassillo94
Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here

I did read it as a float before, same problem, how should I compare the values then?

ToddL1962:
Your problem is that you're reading a float into an int and then testing the int to see if it is NaN. Doesn't make sense...

UKHeliBob:
@lassillo94
Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here

Can't find anything related to auto format code (also my first post)

It doesn't make sense to check an int for NaN I know that. You may want to print out the temp and humidity and see what they are coming back as.

There is no such thing as an int NaN.

Auto format is ctrl-t in the IDE.

Can’t find anything related to auto format code

Auto format is ctrl-t in the IDE.

Another way is to right click in the IDE editor window where it is one of the options, or you can use Tools/Auto Format from the IDE menu

if (isnan(h) || isnan(t)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
   }

Not sure how Arduino treat a "return" from the loop() function. Will this effectively stop the program and resets?

No, it returns, and loop gets called again.
Perfectly normal behaviour

ToddL1962:
It doesn't make sense to check an int for NaN I know that. You may want to print out the temp and humidity and see what they are coming back as.

hzrnbgy:

if (isnan(h) || isnan(t)) {

Serial.println("Failed to read from DHT sensor!");
     return;
  }





Not sure how Arduino treat a **"return"** from the loop() function. Will this effectively stop the program and resets?

I did print the values and they make sense, the temperature reading are fine, I also made a state variable to make it 1 when that if is working (relay on) and it does change between 1 and 0 when temperatures change so technically the if functions are working but the relay doesn't turn on, I'm very frustrated because as soon as you remove the if and just make it turn on and off every 10 seconds it works

Actually with the 'if', do you keep seeing "Failed to read from DHT sensor" in your com port.
If yes, the return statement is probably going to re-run the code from start until that 'if' statement fails.
why not remove the 'return' and put the rest of the code under 'else'?

What is the temperature print telling you?

ishtarsg:
Actually with the 'if', do you keep seeing "Failed to read from DHT sensor" in your com port.
If yes, the return statement is probably going to re-run the code from start until that 'if' statement fails.
why not remove the 'return' and put the rest of the code under 'else'?

That "if" only shows up if I disconnect the temperature sensor, I also tried removing that "if" completely just in case the return partwas causing the error as you said and knowing it wasn't necessary while the dht sensor was connected properly. So... No the problem is not there either

wildbill:
What is the temperature print telling you?

The temperature print is telling me ambient temperature correctly, both variables, t and temp have (obviously) the same value

What is that temperature and are your relays active HIGH or LOW?

wildbill:
What is that temperature and are your relays active HIGH or LOW?

It was printing 23, relay is on when HIGH

And the issue is that the relay doesn't come on?

It's common for relay modules to be active LOW. It may be that your is too. Try swapping to LOW to turn the relay on as a test or perhaps write a simpler sketch to prove to yourself you have it the right way round.