Embedded Micro

Programming the 3x3x3 RGB LED Cube

by on Aug.20, 2010, under Programming, Tutorials

So you have a 3x3x3 RGB LED Cube and you are board of the built in patterns. Now you want to try your hand at making your own patterns but you’re not quite sure where to start. You’ve come to the right place. In this tutorial I will explain how the demo code works and how it can be easily adapted to your new patterns.

First if you have not already download the Source Code.

This code will be the base for your code. Much of the underlying code is done for you so you can just worry about implementing your patterns. Go ahead and open up main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "hardware.h"
#include "driver.h"
#include "patterns.h"
 
extern volatile uint8_t cube[3][27];
 
int main (void)
{
    init_ports();
    USART_Init(21); //baud 115200
    init_timer1(); //set up the timer and start the main interrupt
    display_colors(3000);
    while (1)
    {
        //serial_control();
        plasma(750);
        corner_expand(35);
        rain(500);
        worm(150);
        flash_rand(100);
    }
    return 0;
}

This is the core section of the code. Everything before the while loop is setting up the cube so you should not need to mess with this. The code inside the while loop are the patterns that will be displayed. You can change the order or change their arguments (the numbers in parentheses). The number sets how long the pattern will run for.

Note that they are not a unit of time but rather how many times a pattern will execute its loop. Some patterns have fast loops and others have slow loops so the fact that plasma has 750 and corner_expand has 35 does not necessarily mean that plasma will run 21 times longer.

Now that you are familiar with the highest level open up patterns.c. This file contains the actual patterns and is where you should add yours.

Let us take a look at one of the simplest patterns, flash_rand()

61
62
63
64
65
66
67
68
69
70
void flash_rand (uint16_t len)
{
    uint16_t ctr=0;
    for (ctr=0;ctr<len;++ctr)
    {
        fill_buffer(rand() % MAX_COLOR,rand() % MAX_COLOR,rand() % MAX_COLOR);
        display_buffer();
        delay_ms(300);
    }
}

This function simply fills the cube will a solid random color every 300ms.

There are a set of functions you can use to manipulate the cube. They are listed in driver.h. In flash_rand() we use fill_buffer() which will fill the entire cube with a single color.

The next line display_buffer() copies the cube’s frame buffer to the display buffer. This is used so your entire frame will be displayed once it has been fully rendered. MAX_COLOR is defined as the brightest value a LED can have. Anything higher than MAX_COLOR will be treated the same as if it were MAX_COLOR. This number is subject to change so it is always best to use a fraction of MAX_COLOR.

The last line is used to wait 300ms before showing the next frame. There are delay functions in driver.h which should be used instead of the normal AVR delays like _delay_ms(). This is because the cube display is interrupt driven so your delay will be interrupted many times making the delay time much longer than what you originally wanted.

Something to note is that while you can write to the cube’s frame buffer directly you should use the functions in driver.h instead. The main reason for this is that in future version of the cube the wiring of the LED matrix may change which will lead to a change in the driver and the frame buffer structure. Using the display functions guarantees that the patterns you worked hard on will be usable on future versions of the cube.

I will be adding more to this tutorial.


10 Comments for this entry

  • dturpin

    Hi,
    just built a cube bought from sparkfun. The code that is preprogrammed, does that have the serial communication commented out like in your example above? Cause I cant get that to work. Is it possible to have both the serial and the normal patterns on at the same time? So if you don’t send anything over the serial it will defualt to the normal patterns?

  • admin

    The code that comes with the cube has the serial control commented out. It is possible to get both running at the same time and it was planned for the next update. Basically what I was going to do was have the normal patterns start and have the serial port cause an interrupt which would change the mode to serial control. So sending data to it at any time would cause it to change modes.

  • dturpin

    Ok, sounds like I have a nice project for the weekend then =) Thanks

  • Scott Maddux

    I am new to this stuff, so please bear with…
    I made the cube and love it!!
    I have the Arduino and have uploaded sketchs in the .pde format.
    These source codes are not ‘.pde’-so what software does this work with and how do I get it??

    Can I use the arduino IDE??
    Any help will be appreciated.

    • admin

      This code was not written for the Arduino, it is written for a standalone AVR. SparkFun added the Arduino bootloader so you don’t need a programmer to reprogram it. You will need to use AVR studio and winAVR to use the current code.

  • andy

    sorry that im so dumb about this stuff, i want to get back into making this kinda stuff (last time was 20 years ago!) if i buy the kit and build it then what do i need to communicate with it from my laptop/desktop? ive heard about arduino on sparkfun and some FTDI Cable and now you guys are talking about some AVR thingy.i can understand the software aspect and building the thing its just how to connect the 2 together..
    what exactly do i need to buy and would sparkfun have it?
    thanx in advance for any help with my noob question

    • admin

      To program the cube you need the FTDI cable and the header to solder onto the cube. The FTDI cable is basically a USB to serial adapter. The cube has an Arduino boot loader on it so you can program it over a serial connection instead of having to buy a programmer.

      You could use the same cable to control the cube using the PulseAudio plugin.

      To sum it up you just need the cube kit, FTDI cable, and the right angle header, and yes, SparkFun has everything.

      • Remko

        Hey,
        I can’t figure out how to get the hex-file from the computer to the Cube. I’m using avrdude but it doesn’t work for me somehow. Then I used serjtag-avrdude with FTDI-Bitbang method. It also didn’t work out for me (it says wrong Verification of µC. My Question now: How and with what Software do you program the Cube?

        • courtnek

          Did you ever get an answer for this? I am able to build new hex files with the makefile + source, but what arguments need to be used for avrdude to properly program the chip with an FTDI cable?

2 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...