Addressing Blinkstrip leds with Node.js

Is there a ‘rate limit’ to sending commands to a Blinkstrip? If I send 8 commands one after another if often misses two or three of them. Eg;

for(var x=0; x<7; x++) { leds.setColor(color, { index: x }); }

On the other hand, if I use a callback and a bit of recursion it works perfectly, eg;

function c(color, x){
    if (x > 8) { return; }
    leds.setColor(color, { index: x }, function(err){
	x++;
	c(color, x);
    });
}
c('red',0);

I’ve only been playing with the Blinkstrip for about half an hour, just trying to get a handle on the necessary steps to getting the most out of it. :smile:

If I should be limiting the speed of sending, does anyone have any recommendations for a node.js library to do timed calls?

Replying to my own post just to say timer.js works nicely. Install it with “npm install --save-dev timer.js” and then use a tick event on a timer. Ticking every 5ms seems to work nicely, but 1ms still sees some leds not lighting correctly. I can live with that. :smile: Sharing the code as I go… (ticks are every 0.1 seconds (100ms) in this version).

var blinkstick = require('blinkstick');
var Timer = require('timer.js');

var leds = blinkstick.findFirst();
var x = 0;
var v = 1;
var color = 'red';

var k = new Timer({
  tick    : 0.1,
  ontick  : function(sec) {

  	leds.setColor(color, { index: x });

  	x = x + v;
  	if (x === 9) { x = 8; v = -1; color = 'black'; }
  	if (x === -1) { x = 0; v =  1; color = 'red'; }

  },
  onend   : function() { 

  	c('black',0);

  }
});

k.start(5);


function c(color, x){

	if (x > 8) { return; }

	leds.setColor(color, { index: x }, function(err){
		x++;
		c(color, x);
	});

}

You must allow about 30 ms before you send another color to a single LED or you can use LED frames to light up all connected LEDs at once, but the same 30ms rule still applies. This is required, because BlinkStick can’t communicate via USB when it’s sending data to LEDs and 30 ms is the safe amount of time for BlinkStick to complete the data transfer to LEDs.