long runningAverage(int M) { static int LM[10]; // LastMeasurements static byte index = 0; static long sum = 0; static byte count = 0; // keep sum updated to improve speed. sum -= LM[index]; LM[index] = M; sum += LM[index]; index = index % LMSIZE; if (count < LMSIZE) count++; return sum / count; }