• Ahh, interesting. Try this:

    Puck.accelOn();
    Puck.accelWr(0x11, 0b00011100); // scale to 2000dps
    
    var d = 0;
    Puck.on("accel", a=> d += a.gyro.z/64000);
    
    setInterval(function() {
      print(d.toFixed(2));
    }, 500);
    

    If you rotate the Puck around like it's a knob then it will do a pretty convincing job of counting rotations (I can't promise it's actually calibrated in any way though). However it will wander off over time because the gyro does have noise.

    How is the spindle mounted? Because if it's horizontal then you've also got gravity, so you could detect when the Puck hasn't moved for a while and then use gravity to get an 'absolute' reading for rotation...

    This one seems to work reasonably well - it uses a slightly higher data rate of 26Hz which I think works better:

    Puck.accelOn(26); // 26Hz
    Puck.accelWr(0x11, Puck.accelRd(0x11)|0b00001100); // scale to 2000dps
    
    var d = 0, lastAccel;
    var timeStationary = 0;
    Puck.on("accel", r=> {
      lastAccel = r;
      d -= r.gyro.z/128000;
      var a = r.acc;
      a.mag = a.x*a.x + a.y*a.y + a.z*a.z;
      a.ang = Math.atan2(a.y,a.x)/(2*Math.PI);
      if (a.mag < 66000000 || a.mag > 71000000) {
        timeStationary = 0;
      } else {
        if (timeStationary<100) timeStationary++;
        else {
          // if stable for a while, re-aligh turn count
          var nearest = Math.round(d)+a.ang;
          d = d*0.8 + nearest*0.2;
        }
      }
      
    });
    
    setInterval(function() {
      print("rotation",d.toFixed(2), 
            "abs",lastAccel.acc.ang.toFixed(2), 
            "stationary", timeStationary);
    }, 500);
    

    However with that kind of thing it's drawing around 1mA, so you could expect a battery life of around 10 days. You might also want to consider combining it with the movement detection on http://www.espruino.com/Puck.js#accelero­meter-gyro to get it to enter a low power sleep mode when nothing is happening.

About

Avatar for Gordon @Gordon started