• Hi - the issue is you've got two places where you can execute JavaScript - in the Web Browser, and in Puck.js itself. You're running code that's meant for Puck.js in the web browser.

    The usage of the Puck name for Puck.write and Puck.eval is a bit confusing, but basically Puck.write/eval go in the Web Browser. Puck.mag/etc (or anything on http://www.espruino.com/Reference#Puck) runs in Puck.js.

    If you want to get this working, use the two-way communication example from https://www.espruino.com/Web%20Bluetooth#two-way-communications

    Then replace:

    connection.write("setInterval(function(){Bluetooth.println(Puck.light());},100);NRF.on('disconnect', function() {reset()});\n",
                  function() { console.log("Ready..."); });
              }, 1500);
    

    with:

    connection.write(`
            var zero = Puck.mag();
            var rep = 0;
            
            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);
                if (!Boolean(s < 1000)) {
                    digitalPulse(LED2, 1, 1000);
                    rep++;
                    Bluetooth.println(rep);
                }
            }
            Puck.on('mag', onMag);
            Puck.magOn();`,
                  function() { console.log("Ready..."); });
              }, 1500);
    

    ... and you probably want to change the onLine handler to do something else with the data.

    What's then happening is you're using .write to send the program (as a string) to Puck.js for execution, rather that running it on the PC

About

Avatar for Gordon @Gordon started