Ok, I've just set the whole thing up here and it works for me.
The issue is that for it to work you need at least 3 Pucks, because it doesn't want to turn on a light that it just turned on time before last. I've just changed the logic so it should work:
// Pixl.js code to work with 2 lights only
require("Font6x8").add(Graphics);
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) {
g.clear(1).setFont6x8().setFontAlign(0,-1).drawString(txt,16,0).flip();
}
function show2(txta, txtb) {
g.clear(1).setFont6x8().setFontAlign(-1,-1).drawString(txta,0,0);
g.setFontAlign(1,-1).drawString(txtb,31,0).flip();
}
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>1)
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);
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();
So... Write the code to the Pucks (you can write just to RAM) and press each one once to make the red LED 'glow' (it shouldn't be on full brightness).
Now write the code to the Pixl - it can make it more reliable if you write to flash, and then you can restart it just by unplug/replugging power.
It should say Scan.. followed by Got 2, and finally when a button is pressed 123s 456 where 123s is the time takes to press the last button and 456 is the number of buttons pressed I think.
There's some debug messages printed which do go on the Pixl's display, but you can just delete the print/console.log lines from the code above if it bothers you.
Again, this was written for connecting to a big LED display so the text is small and in the top left, but it's easy to change the show functions to make the text bigger
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.
Ok, I've just set the whole thing up here and it works for me.
The issue is that for it to work you need at least 3 Pucks, because it doesn't want to turn on a light that it just turned on time before last. I've just changed the logic so it should work:
So... Write the code to the Pucks (you can write just to RAM) and press each one once to make the red LED 'glow' (it shouldn't be on full brightness).
Now write the code to the Pixl - it can make it more reliable if you write to flash, and then you can restart it just by unplug/replugging power.
It should say
Scan..
followed byGot 2
, and finally when a button is pressed123s 456
where 123s is the time takes to press the last button and 456 is the number of buttons pressed I think.There's some debug messages printed which do go on the Pixl's display, but you can just delete the print/console.log lines from the code above if it bothers you.
Again, this was written for connecting to a big LED display so the text is small and in the top left, but it's easy to change the
show
functions to make the text bigger