Problems with isnan and Serial

Hello.

I am new to Arduino, and I have been trying to modify the DH11 sketch to suit my LCD screen. Everything is working perfectly, but I have a problems with the isnan and serial.

As seen on the code below, I want the serial to output a error message when the sensor is not present or working. I have been searching around for "isnan" but there is little information. When I unplug the sensor, is shows the value 0 at LCD, and nothing in Serial.

Can I also display the message on the LCD with lcd.print("Message..");??

Last question, what is the difference between print and println?

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

https://gist.github.com/amfgames/14237a35ed17e4caef5721b01aab2e0f

Thanks! :slight_smile:

Are you sure you are getting a NaN from the sensor? That is pretty arcane.

You can try if ((h==h) ==0) since NaN's cannot even compare equal to themselves.

I tried

 // Check if any reads failed and exit early (to try again).
    if (h==0) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

And this worked perfectly. Could you explain to me why the DHTester example in the library, uses the code I mentioned and does indeed report back in serial? Is it because it uses the float datatype?

Code for the example:
https://gist.github.com/amfgames/7e6b2673bf45d5c84d016b37ec5e10da

Thanks again!

If h is an integral type, it cannot be a Nan, which is strictly a floating point thing.

Please edit your posts and replace "quote" by "code" to post code properly :wink:

sterretje:
Please edit your posts and replace "quote" by "code" to post code properly :wink:

Great! Thanks for the tip! :slight_smile:

Sweetwater:
Hello.

I am new to Arduino, and I have been trying to modify the DH11 sketch to suit my LCD screen. Everything is working perfectly, but I have a problems with the isnan and serial.

As seen on the code below, I want the serial to output a error message when the sensor is not present or working. I have been searching around for "isnan" but there is little information.

When using floating pointg numbers and formatting the result into ASCII and you get 'float' formatted to "isnan", then the meaning is: "is not a number".
As simple as that.

Typically this isnan value is the result of DOING A SEVERE MISTAKE WITH FLOATING POINT MATH!

Especially after doing a "floating point math division by zero, the result will be nan (not a number).

And the difference between print() and println (short for "print line") is this:

The print line println() command will add two extra characters for two functions that could be executed by electric typewriters in former times:

  • carriage return (CR, ASCII-code 13)
  • line feed (LF, ASII -code 10)

On old electrical typewriters the "carriage return" and "linefeed" control characters would do mechanical functions.

But when used on the serial monitor, the same control codes will simply place the writing mark (cursor or caret) at the left-most writing position in the next line.
When used in the last line of the serial monitor, the previous contents in the serial monitor will scroll one line up and then start a new line with the cursor in the left-most writing position.