Below, is an Arduino function for reading smoothly an average value from a jumpy or erratic analog input sensor. The function reads repeatedly from an analog input, calculating a running average and returns the value back to the caller.

Here is the function :

// Get an average value from a jumpy or erratic input sensor.
const int getSensorAverageValue (const int sensorPin,
                                 const int numberOfSamples,
                                 const long timeGap)
{
  // This works as a sum counter.
  int currentValue = 0;

  // Get sensor samples with delay and calculate the sum.
  for (int i = 0; i < numberOfSamples; i++) {
    // Add sample to the sum counter.
    currentValue += analogRead(sensorPin);

    // Delay some time for the next sample.
    delay(timeGap);
  }

  // Get the average sensor value (ignore the fraction).
  return (currentValue / numberOfSamples);
}