 # Need Temperature math to return a null if non existant

I have a simple temperature reading on all 6 analog sensors. (Simple glass temperature diodes)

``````#include <math.h>

double Temp;
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15;            // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print(",");
Serial.print(",");
Serial.print(",");
Serial.print(",");
Serial.print(",");
delay(2000);
}
``````

This works on its own. And all is well. But perhapse I have one of sensors not plugged in. The Serial print will say ‘nan’ aka ‘not a number’. Ok that is fine for the above, but I wish to expand this code for a larger project. I need it to say ‘null’ not ‘nan’.

Is there a simple way to get the result of the double Thermister code to spit back "null’ instead of ‘nan’?

I tried a simple if statement right at the end.:

``````  Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
if (!Temp) {
Temp = "null" ;
}
return Temp;
``````

But because this is a binary number, it wont allow it to be a character.

"null" is not a value that a double can have.

michinyon: "null" is not a value that a double can have.

I was afraid that would be true. What about returning with a 0 value instead?

If that isn't an ability, Would it be safe to say that the only way would be to (outside the double) do IF/Then statements that pretty much asks the following?

``````IF (float(Thermister(analogRead(2))) == nan) {
serial.println("null")
else {
}
``````
``````float reading = float(Thermister(analogRead(2));
Serial.println("null")
else {
}
``````

There are several solutions to your problem.

One method is to return some specific number, which is interpreted to mean, the sensor didn't get an answer. This could be zero, as eqrunner suggests, but 0 might reasonably be a temperature that you could read. So pick some other number which isn't a temperature which you could actually measure. And then be careful about comparing floats.

The second method would be, to have two return values from the function, one being an actual value and the other being some kind of true/false flag indicating that a valid measured result was obtained.

michinyon: One method is to return some specific number, which is interpreted to mean, the sensor didn't get an answer. This could be zero, as eqrunner suggests, but 0 might reasonably be a temperature that you could read. So pick some other number which isn't a temperature which you could actually measure.

For example, -500.0 would do. https://en.wikipedia.org/wiki/Absolute_zero

Thanks for the help. As I was dreading, I see that I can not return from the DOUBLE Thermister with a ‘null’ reading, as it has to be a number.

So based on that information, I have found a small fix for it. I simply put in an IF statement in the beginning of the DOUBLE. Since RawADC is the raw 0 to 1024 reading from the analog sensor. If this value is 0. Then I immediately return with a value of -500.0 skipping over all the math.

I will just have to do an additional IF statment to find out if the result is -500.00, then print “null” instead.

``````#include <math.h>

double Temp;
return -500.0;
}
Temp = log(((10240000 / RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp - 273.15;            // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;

void setup() {
Serial.begin(9600);
}

void loop() {
delay(2000);
}
``````

Bad idea. Float comparisons for equality are not guaranteed.

Float comparisons for equality are not guaranteed.

Not guaranteed for all values. Checking for -500.0 should work, but, checking for less than -499 would be better.

By the way, ASSuming that ANY reading from any pin with nothing connected to it means anything is NOT a good idea.

PaulS: By the way, ASSuming that ANY reading from any pin with nothing connected to it means anything is NOT a good idea.

In my circuit, there is a 10k pull down resistor always present. Thus the 0 value. Only time it gets a reading is when the temperature sensor is in line and is pulling up the value.