Reputation: 6646
I am trying to turn on a specific LED using the NeoPixel module. How it works is really easy: Parse it a 2D array of RGB colors. Here's an example:
require("neopixel").write(NodeMCU.B2, [[255, 0, 0], [0, 255, 0], [0, 0, 255]]);
That will turn on the first three LEDs with red, green, and blue. I want to have a function, where I can do something like:
function single(number, color) {
require("neopixel").write(NodeMCU.B2, number, color);
}
single(0, [255, 0, 0]);
single(1, [0, 255, 0]);
single(2, [0, 0, 255]);
Which would do the exact same as above. Now you might ask: Why would you want that? Well:
write()
function 100+ LEDs, where most of them are blackIs something like that possible, or would I have to do some magic in order to remember the last LED configuration?
Upvotes: 1
Views: 220
Reputation: 1886
Yes, totally - it's worth looking at the neopixel docs at http://www.espruino.com/WS2811 as they suggest you use an array to store the current state.
Once you have that array - called arr
here - you can use the .set
method to set the 3 elements at the right position (3x the number, because RGB), and then can resend the whole array.
var arr = new Uint8ClampedArray(NUM_OF_LEDS*3);
function single(number, color) {
arr.set(color, number*3);
require("neopixel").write(NodeMCU.B2, arr);
}
Upvotes: 1