Last active
February 19, 2017 04:40
-
-
Save jshaw/2e64df8b11ae066be7eee85a2157ff12 to your computer and use it in GitHub Desktop.
Arduino Smoothing algorithm
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
// 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