You can use this code to smooth the results from various sensors, but it is most useful with the accelerometer.

The first parameter is an array of the three values from the current sensor reading

The second parameter is the previous three values, or null if no values yet.

If ALPHA is zero or 1, no filtering takes place.  In the program where I used this, here is the declaration:

static final float ALPHA = 0.25f; // if ALPHA = 1 OR 0, no filter applies.
protected float[] lowPass( float[] input, float[] output )
  if ( output == null )
    return input;
  for ( int ix=0; ix<input.length; ix++ )
    output[ix] = output[ix] + ALPHA * (input[ix] - output[ix]);
  return output;