# Looking for a clever way to get the average value of a measurement

I am measuring the velocity v and the acceleration delta v / t. For the velocity, I collect samples for 100 ms before displaying the average of those samples. For the acceleration however I only store the last value for v in order to get delta v. The result is that the displayed acceleration value is spiky and unreliable.

``````static float v, prev_v;
static unsigned int n;
static unsigned long lastTime;

v += measure();
n++;

if((millis() - lastTime) > 100){
Serial.print(" v = ");
Serial.println(v / n);
Serial.print("a = ");
Serial.println(v / n - prev_v);
prev_v = v / n;
v = 0;
n = 0;
lastTime = millis();
}
``````

Is there a way to also get the average value for the acceleration?

Any tips are welcome!

Taking the derivative increases the noise.

You could calculate the "instantaneous" acceleration from successive values of v and average that in the same way as you do v.

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