Arduino sensor data erratic
IMO much better than the average is a very simple digital low pass filter.

LPF_DEPTH_SHIFT as large as slower reaction to the changes (lower filter "frequency"). Remember that LPF_data has to accommodate (max value) &lt;&lt; LPF_DEPTH_SHIFT. You need to choose the proper type.

#define LPF_DEPTH_SHIFT   3

unsigned int LPF_data = UINT_MAX;

unsigned int lowpassfilter(unsigned int value)
{
    unsigned int v = LPF_data &gt;&gt; LPF_DEPTH_SHIFT;
    if(LPF_data == UINT_MAX)
        LPF_data = value &lt;&lt; LPF_DEPTH_SHIFT;
    LPF_data -= v;
    LPF_data += value;
    return LPF_data &gt;&gt; LPF_DEPTH_SHIFT;
}