Fighting with computers

Computers are not always friendly.

Sunday, April 28, 2013

LEDs getting smarter

Sometime ago, I used for a project a set of RGB LEDs that were networked and responded to a few commands sent through a shared bus. It was a lot of work but it was fun. A while ago, at a RepRap meeting, user fungus showed me how he was using new controllers for doing exactly that, controlling RGB leds through a shared bus (daisy-chain would be the exact term though). They are available either as a small PCB with the chip so you can solder your own LED, or with a built-in LED or in a long stripe of LEDs.

Other manufacturers just included the chip together with their RGB leds, so you can get a three pin RGB led that includes internal memory to be set at any desired color from a single-pin output of your favorite micro-controller. And my favorite during the last few years has been Arduino so I just downloaded the FastSPI library and try to make sense of it. Unfortunately it never is so easy, so this time I was forced to upgrade my Arduino to 1.0.4 (I was avoiding that and keeping the 023 version that worked nicely with my 3D printers firmware, I hope nothing will break now) as library was not compiling and I did not want to go through the effort of backporting the code to an old version of Arduino environment.

I did not find a simple example, but eventually got everything running fine with an Arduino UNO using just one output pin. Now I have to figure out what patterns may look cool and to code them.

Here you have my sample code I used for the video above (I am using digital pin 5 on Arduino for the data line):

// Sample code for WS2811 LED bars
// by misan

#include "FastSPI_LED2.h"
#define NUM_LEDS 60

struct CRGB { byte g; byte r; byte b; };
struct CRGB leds[NUM_LEDS];

WS2811Controller800Mhz<5> LED;

void setup() { LED.init(); }
int counter=0;
int MAX=16;     //keep power usage low

void loop() {
    leds[counter].g = random(MAX);
    leds[counter].b = random(MAX);
    leds[counter].r = random(MAX);
    LED.showRGB((byte*)leds, NUM_LEDS);
    counter = ( counter + 1 ) % NUM_LEDS;