• As mentioned, gyro would work well.

    However compass could be ok as long as there are no magnets floating around (or you could ensure you mount the Puck with a magnet nearby so that you're then measuring the rotation relative to that magnet). You just need to rotate the Puck a full 360 degrees when you start up so you can calibrate the max/minimum values.

    This is some old code I had kicking around that that works out an angle then lights up accordingly. Realistically to work nicely it needs a magnet nearby though

    var Vec3 = require("Vec3");
    var vmin = new Vec3(Puck.mag());
    var vmax = vmin;
    
    function onMag(xyz) {
      vmin = vmin.min(xyz);
      vmax = vmax.max(xyz);
      var c = vmin.add(vmax).mul(0.5).sub(xyz);
      var a = Math.atan2(c.x,c.y)*180/Math.PI;
      analogWrite(LED1,a/180,{freq:100,soft:tr­ue});
      analogWrite(LED2,-a/180,{freq:100,soft:t­rue});
    }
    
    Puck.on('mag', onMag);
    Puck.magOn(10);
    
About

Avatar for Gordon @Gordon started