I have 8 (0-7) Neopixel leds in a strip.
At a given time I want to walk a 3 led mark along them from 0 to 7.
This is the code I have now.
if (msg.delay_time == undefined)
msg.delay_time = 400;
var delay_time = msg.delay_time;
if (msg.sunset == undefined)
msg.sunset = "196,10,0";
var sunset = msg.sunset;
sendLater(0, msg, "rgb,0," + sunset);
sendLater(delay_time * 1, msg, "rgb,1," + sunset);
sendLater(delay_time * 2, msg, "rgb,1," + sunset);
sendLater(delay_time * 3, msg, "rgb,2," + sunset);
if (msg.existing[0] == undefined) {
msg.existing[0] = "0,0,0";
}
sendLater(delay_time * 4, msg, "rgb,0," + msg.existing[0]);
sendLater(delay_time * 5, msg, "rgb,3," + sunset);
if (msg.existing[1] == undefined) {
msg.existing[1] = "0,0,0";
}
sendLater(delay_time * 6, msg, "rgb,1," + msg.existing[1]);
sendLater(delay_time * 7, msg, "rgb,4," + sunset);
if (msg.existing[2] == undefined) {
msg.existing[2] = "0,0,0";
}
sendLater(delay_time * 8, msg, "rgb,2," + msg.existing[2]);
sendLater(delay_time * 9, msg, "rgb,5," + sunset);
if (msg.existing[3] == undefined) {
msg.existing[3] = "0,0,0";
}
sendLater(delay_time * 10, msg, "rgb,3," + msg.existing[3]);
sendLater(delay_time * 11, msg, "rgb,6," + sunset);
if (msg.existing[4] == undefined) {
msg.existing[4] = "0,0,0";
}
sendLater(delay_time * 12, msg, "rgb,4," + msg.existing[4]);
sendLater(delay_time * 13, msg, "rgb,7," + sunset);
if (msg.existing[5] == undefined) {
msg.existing[5] = "0,0,0";
}
sendLater(delay_time * 14, msg, "rgb,5," + msg.existing[5]);
if (msg.existing[6] == undefined) {
msg.existing[6] = "0,0,0";
}
sendLater(delay_time * 15, msg, "rgb,6," + msg.existing[6]);
sendLater(delay_time * 16, msg, "rgb,7," + msg.existing[7]);
if (msg.existing[7] == undefined) {
msg.existing[7] = "0,0,0";
}
/**
* @param {number} delayMS
* @param {NodeMessage} msg
* @param {string} payload
*/
function sendLater(delayMS, msg, payload) {
// node.warn(`In sendLater(${delayMS},${msg},${payload}) - setting up a timer to send payload '${payload}' in ${(delayMS/1000).toFixed(2)} seconds time`)
const callback = function (msg, payload) {
// node.warn("Sending delayed payload " + payload);
msg.payload = payload;
node.send(msg);
}
setTimeout(callback, delayMS, RED.util.cloneMessage(msg), payload)
}
I don't know how I missed it for so long but there is a bug in the code.
The 3 red LED's walk along the strip but when it gets to number 6, there is a problem.
My best effort to explain what happens:
LED 0 is set to RED
LED 1 is set to RED
LED 2 is set to RED and 0 is turned off.
LED 3 is set to RED and 1 is turned off.
LED 4 is set to RED and 2 is turned off.
(repeat)
LED 6 is set to RED and 4 is tuned off.
LED 7 is set to RED and 5 is turned off - and also 6.
I think (hope) if I look at it more I may see the mistake, but.....
Sod's law says if I don't ask, I won't be able to see the mistake.
So I'm asking.
Thanks in advance.