You are reading a single comment by @Charly86 and its replies. Click here to read the full conversation.
  • @Gordon
    Thanks, I missed this one
    I tried something similar with start/stop movement detection with button, when pressed, Green led indicate it's on and then blink Red in case of movement. another push stop detection by blue blink
    New position is saved like this moving and leaving puckjs to another place will stop detection until it's moved again. Seems to works, need to play with threeshold.
    Here the code, I'll try your since your math detection looks better than mine ;)

    var th = 10;
    var x =0;
    var y =0;
    var z =0;
    var accFirstRead = true;
    var acc = false;
    
    Puck.magOff();
    
    function doJob(x,y,z) {
      var b = Puck.getBatteryPercentage();
      var t = E.getTemperature();
      var l = Puck.light()*100 ;
      console.log("Batt", b, "  Temp",t, "  Light",t );
      console.log("Diff X:",x, " Y:",y, "Z:",z);
    }
    
    setWatch(function() {
      if (acc) {
        Puck.magOff();
        acc = false;
        digitalPulse(LED3, 1, 100);
        console.log("Accel OFF");
      } else {
        accFirstRead = true;
        acc = true;
        //Puck.magOn(0.31);
        Puck.magOn();
        digitalPulse(LED2, 1, 100);
        console.log("Accel ON");
      }
    }, BTN, {edge:"rising", debounce:50, repeat:true});
    
    Puck.on('mag', function(xyz) {
      var diffx = xyz.x-x;
      var diffy = xyz.y-y;
      var diffz = xyz.z-z;
      x = xyz.x;
      y = xyz.y;
      z = xyz.z;
      //digitalPulse(LED3, 1, 5);
      //console.log(xyz);
      //doJob(xyz);
    
      if (accFirstRead) {
        accFirstRead = false;
        doJob(x,y,z);
      } else {
        if (Math.abs(diffx)>th || Math.abs(diffy)>th*2 || Math.abs(diffz)>th*2) {
          digitalPulse(LED1, 1, 250);
          doJob(diffx, diffy, diffz);
        } else {
          //console.log("Diff X:",diffx, " Y:",diffy, "Z:",diffz);
        }
      }
    });
    
About

Avatar for Charly86 @Charly86 started