With this MicroLib your Arduino board will be able to read data from sensors with analog output. (?!) - Of course your Arduino can already do that out of the box, leveraging the analogRead(), but you are using simple sensors like a photo-resistor, a microphone or a gas-sensor you will quickly discover that the raw values you read from those devices are fluctuating and sometimes there are rogue results.
One way to reduce the fluctuations is to read a couple of values from the sensor and then calculate the average (correct would be the "arithmetic mean") over all values which still is very sensitive to outlier values.
A very good and easy way to truncate outliers is the so called moving average where the arithmetic mean over the last n values is calculated. Its like sliding a window over the current and the past n values so that one single outlier has very little influence on the result.
To use this library in your program, simply include the file MicroLib_ADC.h .[Library API] [LDR example] Photoresistor@WikipediA Arithmetic mean@WikipediA Moving average@WikipediA
v_Value = ADC_GetAverageSample(LDR_PIN,LDR_SAMPLES); // get the average over LDR_SAMPLES values from the ADC
Serial.println(ADC_MovingAverage(&vs_RawData,v_Value)); // calculate the moving average over the last values and print
This simple example program assumes that there is a photo-resistor-bridge connected to an analog input of the arduino. The program reads a number of raw-values from the analog input and calulates the arithmetic mean by using the function ADC_GetAverageSample. Then it submits the averaged sensor value to the function ADC_MovingAverage to calculate the moving average over the last n values. Both results are being printed to the serial monitor, an example output can be seen to the right.
It was a sunny day here in Texas, as can be seen in the output so most of the averaged sensor values were around 800 except for the two rogue values, marked in yellow (i waved my hand over the sensor). You can see how much the moving average has compensated the influence of the outliers: A single drop in the raw values of 240 is reflected by a drop of just 24 in the moving average.Back to the top of the document.