Step 4: C++ Source code

Picture of C++ Source code
In C/C++ usually one has to prototype functions before coding them.

#include <stdio.h>

int main(void);
void do_something(void);

int main(void) {



void do_something(void) {

/* comment */


The Arduino IDE does not require this step, as functions prototypes are generated automatically. Therefore function prototypes won't show up in the code shown here.

Image (1): setup() function

Image (2): spi_transfer() function using hardware SPI of the ATmega168 chip (runs faster)

Image (3): framebuffer code using a timer1 overflow interrupt.

Pieces of code that have a slightly cryptic look for beginners e.g. while(!(SPSR & (1<<SPIF))) {} use the MCU's registers directly. This example in words: "while the the SPIF-bit in register SPSR is not set do nothing".

I just want to emphasize that for standard projects it is really not necessary to deal with these things so closely related to hardware. Beginners should not be frightened by this.
stefa1686 months ago

Hi Madworm, thanks a lot for your instructable!

Right now, I'm trying to implement a control with a second arduino, so that the one connected to the matrix can just do that stuff (it won't get interferences from other parts of the code)

I was going to use SPI because with interrupts I can transmit all the 192 Bytes (I will reduce them when I will manage to do this). To do so, I have to use the SS pin of the Processor, which is pin 10. Now, here's the problem: I tried to modify correctly all the sketch so that it doesn't use pin 10 but pin 8. However the matrix filckers and then completely turns off. I don't know what I can do, as I need that pin to be free so that I can use it with the SPI_STC_vect interrupt..

How can I free that pin, so that I can use it for slave SPI?


acotton14 years ago
Hi madworm,

I have one week to make an RGB LED matrix for school. How did you wire your matrix up before you transferred it all to PCB? Due to time and money constraints, I can't have a PCB manufactured. Do you have photos, schematics, or diagrams?
madworm (author)  acotton14 years ago
One full week... snigger. SCNR.

Before it was transfered to a PCB, I uses a breadboard to test the circuit. Suffice to say that it was no fun at all. So many wires...

If you go to my blog (the link is here somewhere), you will find schematics and photos (flickr) and some code as well. The best entry point is the projects page. Other posts may have outdated content. If you intend to use any of it, make sure to get the latest versions of both from the git repositories, otherwise it may have unpredictable effects.

wmtt4 years ago
Dear madworm.

Good news, I have success in having a working RGB matrix after following your codes and instruction.

May I know how do i modify the code if I want to run it on a stand alone ATMEGA168/328 but NOT from a Arduino?

Thank you again!
madworm (author)  wmtt4 years ago
Well, once you have compiled the code (for an 168 or 328) into the .hex-files, you can just take your favourite ISP programmer and flash the chips. No need to change the source code. On linux systems the .hex files are temporerily created in '/tmp/build.xxxx', on windows I frankly don't know.

Just make sure the FUSE settings of the chips are correct. For an 168 these would be:

a) no bootloader, 16MHz quartz, 16kb usable:

lock: 0x3F
lfuse: 0xFF
hfuse: 0xDD
efuse: 0x01

b) with bootloader, 16MHz quartz, 14kb usable:

lfuse: 0xFF
hfuse: 0xDD
efuse: 0x00