var zero=Puck.mag();
function onMag(m) {
m.x -= zero.x;
m.y -= zero.y;
m.z -= zero.z;
var v=Math.sqrt(m.x*m.x+m.y*m.y+m.z*m.z).toFixed(0);
console.log(v);
}
function main() {
Puck.on('mag', onMag);
Puck.magOn();
}
Multiple calls to main() will result multiple calls to onMag() function, which once the mag is stopped (Puck.magOff()) all are stopped and started if the mag is on (Puck.magOn()). Is there something like an id of the previous callback in order to be stopped ?
Calling main() 10 times you will have v like that:
21
1167
2355
3543
4731
5919
7107
8295
9484
10672
i.e. mag values are exploding even without magnet close to the Puck.
which once the code is uploaded everything is working fine, but when saved I have two callbacks on the mag, if you save it again you will have three callbacks on the mag, etc. How can I have a single function where to receive the current mag values ?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi @Gordon,
Suppose you have
Multiple calls to main() will result multiple calls to onMag() function, which once the mag is stopped (Puck.magOff()) all are stopped and started if the mag is on (Puck.magOn()). Is there something like an id of the previous callback in order to be stopped ?
Calling main() 10 times you will have v like that:
21
1167
2355
3543
4731
5919
7107
8295
9484
10672
i.e. mag values are exploding even without magnet close to the Puck.
I came upon this issue because in general I have:
which once the code is uploaded everything is working fine, but when saved I have two callbacks on the mag, if you save it again you will have three callbacks on the mag, etc. How can I have a single function where to receive the current mag values ?
Thank you!