Hello I am trying to get sensor data and average it every 10~20 values.
I have been looking at the smoothing example.
Examples shows below.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int 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];
Serial.println(total);
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[readIndex];
Serial.print(total);
Serial.print("\t");
Serial.println(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;
// send it to the computer as ASCII digits
Serial.println(average);
Serial.println(" ");
delay(1000); // delay in between reads for stability
}
This create the 10 arrays and stored the each sensor output data to the array and add values.
However, after storing 10th sensor value it does not reset the array.
total= total - readings[readIndex];
So for this line, total stays to the total of past 9 data and subtract past data stored in the readings array.
Then, add a new value to it and average it.
I want to get average value of every 10 data, not from the old one.
There may be simple solution but I am quite new for this and I believe your help will facilitate my process.
Thank you