Smoothing Readings From an Analog Input
Smooth multiple readings of an analog input.
This sketch reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is useful for smoothing out the values from jumpy or erratic sensors, and also demonstrates the use of arrays to store data.
10k ohm potentiometer
Connect one pin of a potentiometer to 5V, the center pin to analog pin 0, and the the last pin to ground.
The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each new value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.
Altering the size of the array used, by changing
to a larger value will smooth the data collected even further.
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language. Last revision 2015/07/29 by SM
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.