You are reading a single comment by @SimonT and its replies. Click here to read the full conversation.
  • Here's a fun way to visualize live gyro data for the z axis - especially fun to roll the puck along a desk! Also fascinating to swing it around fast and film it in slow motion, shows a really nice fast response.

    Gyro is quite power hungry so you can tap the puck to toggle it on and off.

    let callbackCount = 0;
    Puck.on('accel', function(a) {
      callbackCount++;
      if(a.gyro.z > 0) {
       analogWrite(LED1, a.gyro.z / 32764);
       analogWrite(LED2, 0);
      } else {
       analogWrite(LED1, 0);
       analogWrite(LED2, -a.gyro.z / 32764);
      }
    });
    
    let gyroOn = false;
    function toggleGyro() {
      if(gyroOn) {
        Puck.accelOff();
        digitalWrite([LED1, LED2], 0);
      } else {
        Puck.accelOn(104);
        // Set max range to 2000 degrees per sec, 104 Hz
        Puck.accelWr(0x11, 76);
      }
      gyroOn = !gyroOn;
    }
    
    setWatch(toggleGyro, BTN, {edge:"rising", debounce:50, repeat:true});
    digitalWrite([LED1, LED2], 0);
    
    /*
    let lastCount = 0;
    setInterval(() => {
      let change = callbackCount - lastCount;
      print((change / 5).toFixed(1) + ' callbacks / sec');
      lastCount = callbackCount;
    }, 5000);
    */
    

    @Gordon - I did notice the callback seems to be called frequently (about 280 times / sec), regardless of the rate passed in to accelOn() - bug?

About

Avatar for SimonT @SimonT started