Using Isnan - Programming Questions - Arduino Forum

using isnan Projects Programming August 31, 2017, 6:21pm 1

Is it necessary to include the <math.h> library when using this function?

August 31, 2017, 6:22pm 2

BertZ: Is it necessary to include the <math.h> library when using this function?

Wouldn't it have been faster to create a sketch and try using the function without including that header file?

1 Like August 31, 2017, 6:32pm 3

Even faster would be to just include it.

1 Like August 31, 2017, 7:00pm 4

Yes, but Arduino.h includes it for you, therefore no. :slight_smile:

August 31, 2017, 8:11pm 5

Thanks, now I have to look elsewhere why this is not working.

August 31, 2017, 8:25pm 6

Or, you could post your code with a description of the problem.

August 31, 2017, 8:27pm 7

isnan() is C99. I am not sure what version of C the Arduino IDE uses.

August 31, 2017, 8:29pm 8

You can also try this trick:

#define ISNAN(X) (!((X)==(X)))

since NAN's can't even be equal to each other.

August 31, 2017, 8:34pm 9

Some (all?) the bmp180 examples use isnan(). One of the examples compiles with or without math.h, so what's the problem?

now I have to look elsewhere why this is not working

Looks like we have a dreaded X/Y problem here.

Pete

August 31, 2017, 9:56pm 10

Or, you could post your code with a description of the problem.

OK, here is the situation. I am making a humidistat that will automatically change the R.H. setpoint as a function of outdoor temperature. The code seems to be working in that it changes the setpoint as the temperature goes down (buried the DS18B20 in dry ice). When the R.H. falls below set point, the water solenoid valve opens.

The problem is that when I disconnect the AM2302 the solenoid valve opens (a very bad thing) and the program hangs up. I need to reset the Arduino to get it going again. The LCD just holds the last reading. Bottom line is that I am not getting a AM2302 error message on the LCD and the water solenoid should not be opening.

/* A DS18B20 Temperature sensor is connected to 5VDC, ground and (Data) to the Arduino’s Pin D2 and measures the outdoor temperature. This value gets converted to a range of 25 to 50 and is used as a set point for the humidity. It is compared to the humidity read from the AM2302 (DHT22) and is used to trigger the solenoid to supply water to the Humidifier. An AM2302 Humidity/Temperature sensor is connected to the Arduino’s Pin D4 and measures Temperature and Humidity. The program reads the value of this sensor every 5 seconds. Pin D7 of the Arduino is connected to a General Purpose Relay SPDT (5VDC) via a 2N2222 switching transistor to handle the 140 mA coil current. If pin D7 goes high, the relay triggers to the on state and completes the circuit between the N.O. contacts of the relay. If the relay contacts are energized (closed), they will complete the circuit and open the solenoid valve supplying water to the Humidifier on the furnace. The program is written with a test to make sure that the DHT sensor is connected and working. If the sensor cannot be read, the Water Solenoid LED will flash for 10 times, ½ second on, 1 second off to let you know there is a problem. In addition a “Fault: DHT22” message will display on the LCD. If the Humidifier relay is energized, it will latch for two minutes, before sampling the return air’s ambient humidity again. Once the return air humidity reaches the set point, the Humidistat’s relay will de-energize and the program will again start to sample the ambient humidity and test it against the set point every 5 seconds. The LCD displays the Relative Humidity set point as well as the actual humidity. This indicates whether or not the DHT-22 is working properly. The outdoor temperature is displayed on the second line of the LCD to verify that the DS18B20 is working properly. The only LED is connected to Pin D13 on the Arduino and lights when the water solenoid relay is energized supplying water to the humidifier. */ #include <DHT.h> #include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal.h> #include <Adafruit_Sensor.h> // initialize the library with the numbers of the interface pins // LCD pin RS E D4 D5 D6 D7 LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // DS18B20 Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 #define DHTPIN 4 // AM2302 is plugged into pin 4 #define DHTTYPE DHT22 // DHT 22 (AM2302) #define solenoid 3 // Pin to Activate water solenoid #define flow 13 // Pin to show solenoid valve is open (blue) DHT dht(DHTPIN, DHTTYPE); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); int tempF = 0; int setPoint; void setup() { sensors.begin(); dht.begin(); lcd.begin(16,2); lcd.clear(); pinMode(flow,OUTPUT); pinMode(solenoid, OUTPUT); Serial.begin(9600); //Begin serial communication Serial.println("Arduino Humidistat // LCD Set Point Version"); //Print a message } void loop() { // Wait 5 seconds between measurements. delay(5000); // Reading temperature or humidity takes about 250 //milliseconds! // Sensor readings may also be up to 2 seconds 'old' // (its a very slow sensor) delay(100); int h = dht.readHumidity(); // Read temperature as Celsius int t = dht.readTemperature(); int tt = dht.convertCtoF(t); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); lcd.setCursor(0,0); lcd.print("Fault: DHT22"); // If the sensor fails to read successfully execute for loop // and flash the solenoid LED for 10 times // a total of 15 seconds for(int x=0; x < 10; x++){ digitalWrite(flow,HIGH); delay(1000); digitalWrite(flow,LOW); delay(500); } return; } // Read DS18B20 and convert to humidity values between 25 and 50% // setPoint = the set point for the target humidity level { // Send the command to get temperatures int tempOut; //create a variable to hold the temperature value sensors.requestTemperatures(); tempOut = (sensors.getTempFByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire if (tempOut <= 40) { setPoint=25; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } else if (tempOut > 40 && tempOut <=50) { setPoint=30; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } else if (tempOut > 50 && tempOut <=60) { setPoint=35; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } else if (tempOut > 60 && tempOut <=70) { setPoint=40; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } else if (tempOut > 70 && tempOut <=80) { setPoint=45; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } else { setPoint=50; Serial.print ("Humidity Set Point is: "); Serial.println (setPoint); } // Test to see if the humidity is less than the set point // if so turn on solenoid // If not turn off solenoid if(h < setPoint) { digitalWrite(solenoid, HIGH); digitalWrite(flow, HIGH); Serial.println ("Solenoid valve abre"); delay(120000); } else { digitalWrite(solenoid, LOW); digitalWrite(flow,LOW); Serial.println ("Solenoid valve cerado"); } Serial.println (setPoint); Serial.print("Humidity: "); Serial.print(h); Serial.print("%"); Serial.print(" Temperature: "); Serial.print(tempOut); Serial.println(" degF "); lcd.setCursor(0,0); lcd.print("SP: "); lcd.print(setPoint); lcd.print("%"); lcd.print(" RH: "); lcd.print(h); lcd.print("%"); lcd.setCursor(0,1); lcd.print("OutTemp: "); lcd.print(tempOut); lcd.print(" degF"); } } August 31, 2017, 10:07pm 11  int h = dht.readHumidity();  // Read temperature as Celsius  int t = dht.readTemperature();  int tt = dht.convertCtoF(t);  // Check if any reads failed and exit early (to try again).  if (isnan(h) || isnan(t)) {

isnan() works with floating point numbers, not integers.

Topic Replies Views Activity
Problems with isnan and Serial Programming 7 19793 May 5, 2021
Writing program for NaN condition Programming 22 13687 May 6, 2021
Program keep on crashing on nan sensor don't work Programming 6 1020 May 5, 2021
Receiving "NaN" from Temperature & Humidity Sensor Programming 33 10009 February 11, 2023
Simple code question Programming 9 11911 May 5, 2021
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino Isnan(h)