The project I am working on project that is a arduino temperature logger (to sd card) and uploadeder to a website (google chart)
I can read the sensor just fine. And using 'double Thermister' loop, am able to convert the 0-1024 value to a Fahrenheit value.
#include <math.h>
double Thermister(int RawADC) {
double Temp;
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() {
Serial.println(float(Thermister(analogRead(0)))); // display Fahrenheit
}
The problem is. If the value is 0 (no sensor is attached) the 'double Thermister' will return an empty 'nul' (nothing exists) value. When this data is uploaded to the website, the chart crashes because it doesn't know what to do with the 'nul'. DO note. chart will accept the text "null" as a value and will work just fine.
So my first solution to this was: In the 'double Thermister' loop, I first ask if the raw value of the analog sensor in question is 0. If so, I thought I could have it return the text value "null". This does not work:
double Thermister(int RawADC) {
double Temp;
if (RawADC == 0) {
return "null";
}
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;
return Temp;
}
Because this is a 'Double' It needs to have a number value. It will not accept text.
So my next work around for this is to give it a number value that the sensor would not normally read. In my case the value -500.00 works. So I plugged it in.
double Thermister(int RawADC) {
double Temp;
if (RawADC == 0) {
return -500.00;
}
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;
return Temp;
}
I then have to return to the original Serial.print, and force it to an if/then statement before print out the result.
if (float(Thermister(analogRead(0))) == -500.0) Serial.print("null");
else Serial.print(float(Thermister(analogRead(0))));
And all works fine. That is the simple version of my code. The issue I have is I have 7 sensors (so far). And this action occurs on various occasions. When Serial.print wants to view the values. When SD.Write wants to write the values to the SD card (myfile.println), and when Ethernet. wants to view the values and client.print them.
This is an overwhelming amount of extra code and I a trying to figure out how I can condence the code. Or better said, Modify the 'double Thermister' loop to be able to return a decimal value or "null"
My thinking would be:
Serial.println(Sensor_result(2))
^ this would go to the Sensor_result loop with the value of 2 (representing which sensor we want to read.
char Sensor_result() {
- Would now plug that sensor you want to read into a float(Thermister(analog(#)) space.
- The 'double Thermistor' loop would run, returning either -500.00 or a Fahrenheit value ( ex: 75.24)
- Now it would look at the return value of 'double Thermistor' and check to see if it is -500.00
- If it is -500.00 it would change the result to text "null". If not it would leave it alone as 75.24
- This would return with either "null" or 75.24
}
Serial.println could now print out either "null" or 75.24 for analog input sensor 2
> this could also return a result for client.print, or myfile.print
I am asking for suggestions on how I could build a loop that would accomplish this and be able to 'return' with a alphanumaric value to insert in the Sensor_result spot for the sensor it was requesting.