# example - rooling average code

I have a code I use very often when I monitor a sensor and the measurements are fluctuating.
This code stabilizes the displayed measured value.

The code is here:

``````/* --- Global variables ---------------------------------------------*/
/* --- Averaging number -------------------------------------------*/
/* avgCount - low value low averaging - high value high averaging  */
/* choose any values between 2 and 1000 or higher */
const int  avgCount = 10;
/* --- Rooling average variables ----------------------------------*/
float rpmAvg = 0.0, avg[avgCount];
int x = 0;
bool avgCalc = false;
/* --- Sensor variable --------------------------------------------*/
float rpm = 0.0;//
``````
``````void loop(){
/* --- Display the Effect of rooling average -------------------------*/
Display(RPMsensor());
/* --- Measuring update delay ----------------------------------------*/
unsigned long waitMillis = millis();while(millis() - waitMillis < 500);
}
``````
``````float RPMsensor(){
/* -------------------------------------------------------------------*/
/* --- build the array for the rooling average calculation -----------*/
avg[x] = rpm;if(x < (avgCount - 1)){x++;}else{x = 0;avgCalc = true;}
/* --- calculating the rooling average -------------------------------*/
if(avgCalc == true){
for(int i = 0;i < avgCount;i++){rpmAvg += avg[i];}
rpm = rpmAvg / avgCount;rpmAvg = 0;}
return rpm;
}
``````

I hope also others can use this small code.

The line "Display(RPMsensor());" is the link to your display sub code for the sensor value "rpm".

You mean rolling, not rooling.

Code is a substance not an object, we talk of code in the abstract. "A code" is something for turning a message into unreadable ciphertext, not the same thing.

Its a 'sketch' (Arduino specific) or more generally 'program'/'app'/'utility', (or a code snippet or piece of code if a fragment)

Your code is unfortunately over-commented, making it hard to read. Only comment something
that is not self-evident from the code, try to make the code clear.

Global variable should not be indented, they are top-level and thus have zero indent.

``````  avg[x] = rpm;if(x < (avgCount - 1)){x++;}else{x = 0;avgCalc = true;}
``````

Entering the obfuscated C contest?! This is poor style, new line for each statement please,

Your code has very odd behaviour - for 9 readings it displays each reading verbatim, then in place of the 10th
reading it displays the weighted average of the last 10 readings. That's probably not what you
think its doing?

This is not a rolling average.

For a rolling average you need to keep track of the last N readings and the sum of the last N readings
efficiently, and return an average on every sample.

This is done by subtracting the oldest stored reading from the sum variable, then replacing it by the
latest new reading, and adding the new reading to the sum variable. Return the sum variable divided by N
every time.

Here's my stab at a rolling average:

``````#define N 10

{
static long sum = 0L; // sum of last N readings
static int arr[N] ;  // last N readings as a circular buffer
static int index = 0 ;