Created
February 11, 2016 01:07
-
-
Save wonkoderverstaendige/be6cede7df8852513700 to your computer and use it in GitHub Desktop.
averaging array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long average(int newest) { | |
#define NUM_SAMPLES 10 | |
static int data[10]; | |
static byte index = 0; | |
static long sum = 0; | |
static byte count = 0; | |
sum -= data[index]; | |
data[index] = newest; | |
sum += newest; | |
index++; | |
index = index % NUM_SAMPLES; | |
if (count < NUM_SAMPLES) count++; | |
return (float)sum / count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment