Skip to content

Instantly share code, notes, and snippets.

@wonkoderverstaendige
Created February 11, 2016 01:07
Show Gist options
  • Save wonkoderverstaendige/be6cede7df8852513700 to your computer and use it in GitHub Desktop.
Save wonkoderverstaendige/be6cede7df8852513700 to your computer and use it in GitHub Desktop.
averaging array
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