If you've detected movement (magDiff is higher than your threshold) you can set avr.x=savr.x; (and for y and z), which will mean that for the next iteration it won't report movement unless the magnetic field has changed again.
However you're likely to then get some iterations when movement isn't reported when the device has actually moved.
The other thing you can do is lower the period of the average (eg change avr.x += dx/8; to avr.x += dx/4; (for y and z too)) which will make it less sensitive to movement but will report that it is stationary more quickly.
avr.x += dx/8;
avr.x += dx/4;
© Espruino, powered by microcosm.
Report a problem