how to calculate average value from data input of lm35

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

void setup()

{

Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
data_out[thisReading] = 0;
ave_num_cal ();

}

void loop()

{

int data_out = analogRead(A0);
Vin = data_out * 0.00488;
temp = Vin*100;

Serial.print("TEMPERATURE = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(1000);

}

void ave_num_cal(){

total= total - readings[index];
readings[index] = analogRead(A0);
total= total + readings[index];
index = index + 1;

if (index >= numReadings)
index = 0;

average = total / numReadings;
Serial.println(average);
delay(1); // delay in between reads for stability
}

please help me..im still new in using arduino and still learning..

does your code compile (no)....

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.

Hope this helps.

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.

Something like

myArray(indx) = analogRead(A0);
indx ++;
if (indx > 9) {
   indx = 0;
}

Then the array will always have the 10 most recent readings and a average is always available with

tot = 0
for (byte n = 0; n < 10; n++) {
   tot += myArray[n]
}
avg = tot / 10;

...R

Then the array will always have the 10 most recent readings

After 10 readings have been taken, anyway. Before that, the average will be low.

PaulS:
After 10 readings have been taken, anyway. Before that, the average will be low.

Perfectionist !

...R

thnx guys for ur help..dis does give me knowledge in a new level for arduino.. :wink: :wink:

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

float temp;
float Vin;

void setup() {
Serial.begin(9600);
uint16_t my_analog_value = getAnalog (temp, 10);

}

void loop() {
int data_out = analogRead(A0);
Vin = data_out * 0.00488;
temp = Vin*100;

}

uint16_t getAverage (float temp , uint16_t avg)
{
uint32_t accum = 0; // averaging accumulator
uint16_t x = avg; // copy averaging count to "X"
while (x--) {
accum += temp; // add analog readings to accumulator
}
return (accum / avg); // return averaged reading

Serial.println("The average temperature value is ");
Serial.println(temp);

delay(500);

}

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:

blinkLED(3); // blink led for 3 seconds