hye..i need help for my project..i tried to calculate the average value of the first 10 value read from lm35..can anyone help me guide if there is any mistake in my programming
float temp;
float Vin;
const int numReadings = 10;
float data_out[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
At first, you declared data_put as an array of floats, then in your loop you declared it as an integer.
also, the "readings" array isn't declared anywhere. Right now, the code, if it were to compile. would only show the latest read of LM35, because the averaging function is only ran once.
learnKID:
hye..i need help for my project..i tried to calculate the average value of the first 10 value read from lm35..can anyone help me guide if there is any mistake in my programming
Averaging analog input readings is typical and a good idea to remove (or at least diminish) electrical noise.
Averaging is simple. Just do this:
uint16_t getAnalog (int port, uint16_t avg)
{
uint32_t accum = 0; // averaging accumulator
uint16_t x = avg; // copy averaging count to "X"
while (x--) {
accum += analogRead (port); // add analog readings to accumulator
}
return (accum / avg); // return averaged reading
}
Now, let's say you want to read analog port A3 and take 100 readings to average it. Simply call the function above like this:
uint16_t my_analog_value = getAnalog (A3, 100);
Now "my_analog_value" will be a 100 times averaged reading of analog input port A3.
If you want to calculate an average without using a blocking WHILE or FOR you should save the successive values into an array used as a circular buffer.
learnKID:
sory to ask again..i tried to display the value data of the average value. But it only display the value of data read from LM35
Well, that's kind of what you are telling it to do with this line:
Serial.println(temp);
You are still printing temperature, not the average value.
When you declare a function - it is what it is - simply a declaration, that code will not run until you call that function.
Functions are called, by typing there name ('getAverage' in your case) and then typing the function parameters in parenthesis. For example, to blink an LED with a function that takes seconds as parameter, you would do: