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.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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;
toavr.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.