Skip to content

Instantly share code, notes, and snippets.

@jshaw
Last active February 19, 2017 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshaw/2e64df8b11ae066be7eee85a2157ff12 to your computer and use it in GitHub Desktop.
Save jshaw/2e64df8b11ae066be7eee85a2157ff12 to your computer and use it in GitHub Desktop.
Arduino Smoothing algorithm
// taken from https://github.com/arduino/Arduino/issues/3934
// its an alternative to: https://www.arduino.cc/en/Tutorial/Smoothing
const int filterWeight = 4; // higher numbers = heavier filtering
const int numReadings = 10;
int average = 0; // the average
void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// seed the filter
average = analogRead(inputPin);
}
void loop() {
for (int i = 0; i < numReadings; i++) {
average = average + (analogRead(inputPin) - average) >> filterWeight;
}
Serial.println(average);
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment