RGB LED controller

I wrote some functions to control 8 RGB LEDs connected in an 8x3 matrix on 11 I/O lines. The code uses timer interrupts to run in the background updating the LEDs without the user having to worry about it.

I made a little chaser to test it and my main loop is as simple as:

void loop()                     // run over and over again
   RedByte = (RedByte * 2);
   if (RedByte > 255) RedByte = 1;
   GreenByte = (GreenByte * 2);
   if (GreenByte > 255) GreenByte = 1;
   BlueByte = (BlueByte * 2);
   if (BlueByte > 255) BlueByte = 1;

All the user has to do is set the three bytes, and the background functions take care of the rest. It updates every millisecond, so it's very quick and responsive when the bytes change.

I tried to make a video of it but my phone camera isn't that great. The best I could get was: http://www.youtube.com/v/WdeTxpbYBPM in the light and http://www.youtube.com/v/ItEYdk-uQi8 in the dark

I consider this to be a building block, it can be used with data from connected input device or from the desktop to display things in creative ways (a temperature probe, reacting to music, computer CPU load, etc).

Right now though the background code is in the form a few functions, I need to learn how to make a proper library :).

Looks like a really interesting project... Could you by any chance post the interrupt code? Always interesting to se how people solve the problem whit multiplexing lots of leds...

Oh and what method do you use to set the state of the pins, do you use digitalWrite or do you set the pins using direct access to the registers... The later one is much much faster and is good when you have to do it fast and often.


I don't usually like posting code of a work in progress since it can get pretty messy, I like to polish it first. But, my ISR is:

void pulseLeds()
  static int i = 0;
  i = (i + 1) % 3;
  switch(i)        // disply one colour each interrupt call
    case 0:
     digitalWrite(BLUE, HIGH);     // Turn off blue LEDs
     setAnodes(RedByte);           // Set the anodes for red
     digitalWrite(RED, LOW);       // Turn on the red LEDs
    case 1:
     digitalWrite(RED, HIGH);
     digitalWrite(GREEN, LOW);
    case 2:
     digitalWrite(GREEN, HIGH);
     digitalWrite(BLUE, LOW);

Obviously, setAnodes() is a simple function to break down the byte and load it onto the Anode, which have their pin numbers stored in an array.

I do use digitalWrite() for now, since timing isn't very critical, the only issue is not stealing too many cycles from the main program. It's responsive enough to do a colour persistance-of-vision display, which is something I had in mind for it.

Would there be any interest in a shield kit for this? I could get fancy and leave space for sensors on the analog inputs as well.