Time for action – smoothing the accelerometer response
To smooth the accelerometer response, perform the following steps:
Open
app.js
from the previous example.Add the variables given next to
app.js
. These are your tuning variables. They allow you to configure the smallest increment that can be considered a move, and how quickly the formula reacts to a large move. In short, these figures allow you to walk the line between having a slider that is just like the raw one that reacts quickly but is jittery, and one that is more like a supertanker, which reacts to change slowly but with no jitters and makes steady progress:var noiseAttenuation = 3; var accelerometerMinStep = 0.02; var filterConstant = 0.2;
Now add two more variables. These values are used to store the previous results of the filter and do not need to be modified. They will be managed by the filter code when it is running:
var alpha = 0; var lastValue = 0;
Now add the low pass filter code. This comes in two functions. The
clamp...