You have all the Pucks running the code acting as 'slaves' - tapping them once turns them on, and they turn off after some inactivity
The MDBT42 breakout code runs and controls them and displays the score.
Without the MDBT42 breakout code the Pucks won't do anything, but you'd need the breakout and a display wired up (or maybe a Pixl.js or a Bangle.js).
However if you just want to try it, I've taken the code and removed all mention of the display hardware, so if you write this code onto one of the Puck.js and disconnect, it should be able to control the other two I think:
var MAXTIME = 60;
var lights = [];
var currentLight = undefined;
var lastLight = undefined;
var score = 0;
var timer = 0;
var timerInterval;
function addrToId(addr) {
var d = addr.substr(12,5).split(":").map(n=>parseInt(n,16));
return (d[0]<<8)|d[1];
}
function show(txt) {
}
function show2(txta, txtb) {
}
function startDeviceScan() {
show("Scan...");
NRF.findDevices(function(devices) {
show(`Got ${devices.length}`);
print(" "+devices.map(d=>d.id+" => "+addrToId(d.id)).join("\n "));
lights = devices.map( d => addrToId(d.id) );
if (lights.length>2)
setTimeout(startGame, 1000);
}, {timeout : 5000, filters : [{ namePrefix:"BTK" }] });
}
function showNewLight() {
var light;
do {
light = lights[Math.floor(Math.random()*lights.length*0.999)];
} while (light==currentLight || light==lastLight);
lastLight = currentLight;
currentLight = light;
console.log("Light ", light);
NRF.setAdvertising({},{
showName:false,
manufacturer:0x0590,
manufacturerData:[0,0,light>>8,light&0xFF],
whenConnected:true,
interval: 100
});
}
function stopLights() {
console.log("Stop Lights");
NRF.setAdvertising({},{
showName:false,
manufacturer:0x0590,
manufacturerData:[0,0,0,0],
whenConnected:true,
interval: 100
});
setTimeout(function() {
NRF.setAdvertising({},{showName:true, interval:375});
}, 5000);
}
setWatch(showNewLight, BTN1, {repeat:true});
function startGame() {
show("GO!");
timer = MAXTIME;
score = 0;
showNewLight();
NRF.setScan(function(d) {
var m = d.manufacturerData;
console.log(addrToId(d.id), m);
if (addrToId(d.id) == currentLight) {
var time = (m[2]<<8) | m[3];
console.log("Light pressed", time+" ms");
score++;
show2(timer+"s", score);
showNewLight();
}
}, { filters: [{ manufacturerData:{0x0590:{}} }] });
if (timerInterval) clearInterval();
timerInterval = setInterval(function() {
if (timer>0) {
timer--;
show2(timer+"s", score);
if (timer==0) stopLights(); // turn lights off
} else {
show("="+score+"=");
setTimeout(function() {
show("");
}, 500);
}
}, 1000);
}
startDeviceScan();
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.
So I'm pretty sure what happens is:
Without the MDBT42 breakout code the Pucks won't do anything, but you'd need the breakout and a display wired up (or maybe a Pixl.js or a Bangle.js).
However if you just want to try it, I've taken the code and removed all mention of the display hardware, so if you write this code onto one of the Puck.js and disconnect, it should be able to control the other two I think: