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 readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
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:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
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
Added ...
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
// calculate the average:
average = (total / numReadings)*100;
average = map(average, 52600, 75800, -2000, 9000);
// send it to the computer as ASCII digits
Serial.print(average/100);
Serial.println(" C");
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
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");