Puck.js previous value storage

Posted on
  • Hi all,

    I'm working on a project where I use the puck.js to keep track of reps based on the difference of the starting value returned by the magnetometer and the elevated value when users complete a rep.

    Currently I'm using a hardcoded value for comparison, but I don't like this approach. I want to be able to either read the previous value obtained by the magnetometer or at least get some help with storing the value for later use.

    Any help is much appreciated.

    var zero = Puck.mag();
    var doorOpen = false;
     var isRep = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var h = Math.floor(s);
        if (h > 500){
          isRep = true;
          console.log("Rep counted"); 
    Puck.on('mag', onMag);
  • Tue 2020.03.17

    Hello @user110429,

    Is the above snippet from this example perhaps? (last code snippet)


    Maybe populate an array with each value read, then perform the comparrison calculation after it is known there won't be any more data?


    If walking an array to extract a value is not your cup of tea, then there are 'Try it' examples at my favorite simple test site:


    Would you mind posting some data output, and of course a made up example of how/what is desired to be viewed. That would assist us in providing some ideas.

  • You can use require("Storage").write/readJSON to write/read data for later use.

    However the magnetometer value will depend on what orientation you're in, so really it'd make more sense to do something like have a buttonpress 'reset' the zero value, so you can just press it before you start doing exercise.

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

Puck.js previous value storage

Posted by Avatar for user110429 @user110429