Return only Accel values

Posted on
  • Hi,

    I saw the documentation about the built in accelerometer/gyroscope.
    How I can return only the x/y values of the accelermoter, starting at 0?

    Best regards

  • Got it.

  • Puck.accelOn();
    
    Puck.on('accel', function(data) {
      console.log(data.acc.x);
    });
    
  • But I have another question.

    Any idea how I can get x/y starting at 0 with a non sensitive movement tracking?
    The raw values changing very fast.

    I would like to use the Puck.js as a Joystick.

    Best regards

  • To get the value from 0, you need to subtract a 'base' value.

    For instance you could do:

    Puck.accelOn();
    var accZero = Puck.accel().acc; // get the base value when we start up
    Puck.on('accel', function(data) {
      console.log(data.acc.x - accZero.x);
    });
    

    If you want to stop the values moving so fast, you can do an average:

    Puck.accelOn();
    var accZero = Puck.accel().acc; // get the base value when we start up
    var avrX = 0;
    Puck.on('accel', function(data) {
      avrX = (avrX*0.9) + 0.1*(data.acc.x - accZero.x);
      console.log(avrX);
    });
    

    You can change values to get a slower movement but they must always add up to 1, so 0.9/0.1 or 0.8/0.2(faster) or 0.95/0.05(slower)

  • Awesome, thank you Gordon!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Return only Accel values

Posted by Avatar for psc1988 @psc1988

Actions