Lately I've received a DHT22 sensor. I've started to create a small temperature and humidity monitor station. One of the things I noticed was if you pulled out the sensor while in use, it displayed "nan". What I wanted to do was if the variable "temp" (for the temperature) equaled "nan", my LCD would display a message. But I keep getting a scope, conversion, and declaring errors. I've tried every variable and a lot of online forums, documentations, and the official Arduino reference site. My code looks like this:
#include <Adafruit_Sensor.h>#include <DHT.h>#include <LiquidCrystal.h>const int rs = 53, en = 51, d4 = 49, d5 = 52, d6 = 48, d7 = 47;LiquidCrystal lcd(rs, en, d4, d5, d6, d7);#define DHTPIN 27 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); int chk;float hum; float temp; byte bigdot[8] = { B00000, B00000, B00000, B01110, B01110, B01110, B00000, B00000};byte circle[8] = { B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};void setup() { lcd.createChar(0, bigdot); lcd.createChar(1, circle); lcd.begin(16, 2); dht.begin(); do { lcd.setCursor(5,1); lcd.write(byte(2)); lcd.setCursor(4,0); lcd.print("Loading"); delay(500); lcd.clear(); lcd.setCursor(7,1); lcd.write(byte(2)); lcd.setCursor(4,0); lcd.print("Loading"); delay(500); lcd.clear(); lcd.setCursor(9,1); lcd.write(byte(2)); lcd.setCursor(4,0); lcd.print("Loading"); delay(500); lcd.clear(); dht.readHumidity(); dht.readTemperature(); } while (hum > 90 || temp > 50.00); Serial.begin(9600);}void loop() { hum = dht.readHumidity(); temp = dht.readTemperature(); lcd.clear(); lcd.setCursor(0,0); lcd.print("Temp: "); lcd.print(temp); lcd.write(byte(1)); lcd.print("C"); lcd.setCursor(0,1); lcd.print("RH: "); lcd.print(hum); lcd.print("%"); if (temp == a) { lcd.clear(); lcd.home(); lcd.print("Whoops! An"); lcd.setCursor(0,1); lcd.print("error occured!"); }; Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, Temp: "); Serial.print(temp); Serial.println(" Celsius"); delay(2000); //Delay 2 sec. }Thanks in advance!
- please add the full error message to your postjsotola– jsotola2019-12-08 23:24:41 +00:00CommentedDec 8, 2019 at 23:24
1 Answer1
A "float" number has multiple possible states, besides just being a number (It never, though, can be compared to a string):
- A real floating point number
- Infinity
- Something that isn't a valid number
The latter is what you want - that is, the internal data of the float variable doesn't represent a valid number.
There are two functions in the standard math library that deal with this for you.
To use it:
if (isnan(temp)) { lcd.print("Invalid");}Explore related questions
See similar questions with these tags.