A few people have asked about having an accelerometer so they can detect movement - but it's not the only way.
For a lot of things you can still use the magnetometer. The magnetic field changes a lot, not just with angle, but with what other items the Puck.js is near. It's almost impossible to move Puck.js without changing the magnetometer's reading at least a little.
For instance try this code:
var avr = Puck.mag();
var magDiff;
Puck.on('mag', function(xyz) {
// work out difference in field
var dx = xyz.x-avr.x;
var dy = xyz.y-avr.y;
var dz = xyz.z-avr.z;
magDiff = Math.sqrt(dx*dx+dy*dy+dz*dz);
// update average
avr.x += dx/2;
avr.y += dy/2;
avr.z += dz/2;
LED.write(magDiff > 50);
});
Puck.magOn();
It looks for changes in the magnetic field about once a second, and will light the red LED if it has changed more than a little. After a few seconds of no movement the LED will turn off.
It's easy, low power, and seems to work pretty well.
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.
A few people have asked about having an accelerometer so they can detect movement - but it's not the only way.
For a lot of things you can still use the magnetometer. The magnetic field changes a lot, not just with angle, but with what other items the Puck.js is near. It's almost impossible to move Puck.js without changing the magnetometer's reading at least a little.
For instance try this code:
It looks for changes in the magnetic field about once a second, and will light the red LED if it has changed more than a little. After a few seconds of no movement the LED will turn off.
It's easy, low power, and seems to work pretty well.