Smoothing Sensor Input
Analog sensor input (flex, distance, sound, etc.) often has noise or jitter in the data it provides. Some of this can be improved in hardware, but often it’s easier to do in code. There is a smoothing example built in to the Arduino, and a smoothing function on the Arduino site. Here is my own version which I think is simpler and cleaner, especially if you want to smooth multiple sensors. /* Sensor smoothing by Eric Forman [www.ericforman.com] Original version 2008, updated May 2012 Smooths sensor readings by adding only a fraction of new values. The more the new value is divided, the smaller the change, thus a smoother result As with any smoothing function, the more you smooth, the slower the response time */ int sensorPin = 0; // sensor input pin int rawVal = 0; // sensor raw value float smoothedVal = 0.0; // sensor smoothed value //float smoothedVal2 = 0.0; ...