user110429
Member since Mar 2020 • Last active Jul 2022Most recent activity
-
- 8 comments
- 2,009 views
-
Hi,
I've tried the suggested fix with little success, attached is my code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://www.puck-js.com/puck.js"> var connection; document.getElementById("btnId").addEventListener("click", function () { if (connection) { connection.close(); connection = undefined; } Puck.connect(function (c) { if (!c) { alert("Couldn't connect!"); return; } connection = c; function connectDevice() { connection.write("reset();\n", function () { // Wait for it to reset itself setTimeout(function () { 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); }) } }); }); </script> </head> <body> <button id="btnId" onclick="Puck.connect()">On!</button> <button onclick="Puck.write('LED1.reset();\n');">Off!</button> </body> </html>
I've tried this line:
<button id="btnId" onclick="Puck.connect()">On!</button>
With and without the onclick attribute. Whenever it connects to the Puck it follow ups with an error message: ERROR: TypeError: callback is not a function
-
I'm trying to test my code for Puck.js on a webpage, however I keep getting errors when trying to call the function, such as uncaught reference error when trying to execute the function. Also, errors are been returned when called puck.mag(). I've looked at the puck.html examples and I can't figure out why this is happening.
This is my code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://www.puck-js.com/puck.js"></script> <script src="https://www.puck-js.com/puck.js"> 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++; console.log(rep); } } Puck.on('mag', onMag); Puck.magOn(); </script> </head> <body> <button onclick="Puck.write('LED1.set();\n');">On!</button> <button onclick="onMag()">puckTest</button> <button onclick="Puck.write('LED1.reset();\n');">Off!</button> </body> </html>
-
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); console.log(h); console.log(isRep); if(!isRep){ if (h > 500){ isRep = true; console.log("Rep counted"); } } } Puck.on('mag', onMag); Puck.magOn();
-
Hey, thanks for these links. So I've used the code found on this link for counting button presses.
http://www.espruino.com/Pico+Buttons
It works! but when I tried to run it on another code editor like visual studio code (or via GitHub) it wouldn't work on my web page, any ideas why that's happening? -
Hi @Robin sorry, I did not see your messages initially. I've tried all the examples and they worked, so thank you for your help. Also @Gordon the code worked, thank you for your help.