# Temperature result in serial monitor

After mapping an LM335 temp sensor (10mv per degree C.) I thought I would see a .5 C resolution. What might I be missing? I'm new to Arduino and C++.
I only see a whole number +.00 C

``````const int numReadings = 20;

int total = 0;                  // the running total
float average = 0;                // the average

int inputPin = A0;

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
}
}

void loop() {
// advance to the next position in the array:

// if we're at the end of the array...
// ...wrap around to the beginning:
}

// calculate the average:
average = map(average, 526, 758, -20, 90);
// send it to the computer as ASCII digits
Serial.print(average);
Serial.println(" C");
delay(1);        // delay in between reads for stability
}
``````

All your calculations are dealing with integers which then get put into a float. So only integer values will be the result.
You must override the integers with a "float" in that last calculation.
Paul

I have defined average as a float. I guess I don't understand what I need to change.

Yes, but nothing in that calculation is being done as a float unless you tell the compiler to assume the other pieces of the calculation are float.

But then you need to look at the documentation for "map". Doesn't and can't work with float.
Paul

average = float (total) / float (numReadings) ;

... and ... this too for deeper understanding:
https://stackoverflow.com/questions/27674295/why-do-we-separately-cast-to-float-in-an-integer-division

When dealing with casts, either of these two syntax will compile and provide the same answer:

``````#include <Streaming.h>

int a = 5;
int b = 4;

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

void loop() {

float average = (a + b) / 2;

Serial << "I Average=" << average << "\n";

average = ((float) a + (float) b) / 2;

Serial << "F1 Average=" << average << "\n";

average = (float (a) + float (b)) / 2;

Serial << "F2 Average=" << average << "\n";

}

> I Average=4.00
F1 Average=4.50
F2 Average=4.50
``````

You are using the map() function to generate and print the final value.
The map() function only works with integer numbers.

For the mathematically inclined, here’s the whole function

``````long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

try this way

``````  // calculate the average:
average = map(average, 52600, 75800, -2000, 9000);
// send it to the computer as ASCII digits
Serial.print(average/100);
Serial.println(" C");
``````

RV - mineirin

You nailed that one. Interesting and informative on the mapping change you made. Probably understanding the map function math helps. I'll take your word on that one. Thanks

Correct form ;

``````average = (float(total) / float(numReadings))*100;
average = map(average, 52600, 75800, -2000, 9000);
// send it to the computer as ASCII digits
Serial.print(average/100);
Serial.println(" C");
``````

Better Yet. That's fun.... Very helpful!

Any reason you wouldn't define the variables as floats to begin with?

Hi
No, no reason.
you can define them at the beginning yes.
And defining in the beginning does not need to define in the calculation.

RV - mineirin

Thanks for the great explanation!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.