Skip to content

Instantly share code, notes, and snippets.

@brunokruse
Created May 15, 2012 17:40
Show Gist options
  • Save brunokruse/2703594 to your computer and use it in GitHub Desktop.
Save brunokruse/2703594 to your computer and use it in GitHub Desktop.
Smoothing Algorithm
const int numReadings = 10;
int readings[numReadings]; // the readings from centroid.x
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup()
{
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void update() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment