# Direct Drive Single Color 4x4x4 LED Cube

Hello everyone, I would like to find out some information about a few things I do not understand. I am very new to Arduino and everything LED’s, so please explain things in a manner that one who is just getting started will understand. Ok so I have a 4x4x4 LED single color cube that is direct driven by the Arduino. My setup is as follows.

16 Anode pins hooked up to 0,1,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1
4 layer pins hooked up to A2,A3,A4,A5
All are set as outputs
LED pins set High are ON and Low is off
Layer pins set as High are off and Low is on

I have 3 questions here:

#1: Under void setup there is a for statement and a pinmode statement followed by the same thing for the level pins. Can anyone explain this is a simple manner than I can understand why this is needed and what it does in relation to my cube/code? I understand its setting up the pins as outputs and that it reads pin equals 0 and if pin is less than 16 add 1. WHAT??? I have no idea what that means.

#2: Take a look at my void loop section and youll see I made a pattern that lights up a layer then moves from top to bottom, then left to right. There has to be an easier way than writing all that data like that. If you have a way can you explain how to set it up, and what each part means?

#3: The way I have my void loop setup It just plays each pattern once and moves on, how do I get each pattern to play lets say 10 times and then move to the next one?

I have found many easy to make work tutorials on the led cube…but almost nothing on explaining it. I hope that my first round here at Arduino shows me the light Thanks

Here is my code:

``````#include <avr/pgmspace.h>

int ledPin[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1};
int levelPin[4] = {A2,A3,A4,A5};

void setup(){

for (int pin=0; pin<16; pin++){

pinMode (ledPin[pin],OUTPUT);
}

for (int pin=0; pin<4; pin++){
pinMode (levelPin[pin],OUTPUT);
}
}

void loop() {

//UP AND DOWN
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);digitalWrite(A3,HIGH);digitalWrite(A4,HIGH);digitalWrite(A5,HIGH);
delay(200);
digitalWrite(A2,HIGH);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);digitalWrite(A3,LOW);digitalWrite(A4,HIGH);digitalWrite(A5,HIGH);
delay(200);
digitalWrite(A3,HIGH);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);digitalWrite(A3,HIGH);digitalWrite(A4,LOW);digitalWrite(A5,HIGH);
delay(200);
digitalWrite(A3,HIGH);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);digitalWrite(A3,HIGH);digitalWrite(A4,HIGH);digitalWrite(A5,LOW);
delay(200);
digitalWrite(A5,HIGH);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);digitalWrite(A3,HIGH);digitalWrite(A4,LOW);digitalWrite(A5,HIGH);
delay(200);
digitalWrite(A2,HIGH);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);digitalWrite(A3,LOW);digitalWrite(A4,HIGH);digitalWrite(A5,HIGH);
delay(200);
digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);digitalWrite(A3,HIGH);digitalWrite(A4,HIGH);digitalWrite(A5,HIGH);
delay(200);

//SIDE TO SIDE

digitalWrite(0,HIGH); digitalWrite(1,LOW);digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,HIGH);digitalWrite(5,LOW);digitalWrite(6,LOW);digitalWrite(7,LOW);
digitalWrite(8,HIGH);digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,HIGH);digitalWrite(13,LOW);digitalWrite(A0,LOW);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,LOW); digitalWrite(1,HIGH);digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,HIGH);digitalWrite(6,LOW);digitalWrite(7,LOW);
digitalWrite(8,LOW);digitalWrite(9,HIGH);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,HIGH);digitalWrite(A0,LOW);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,LOW); digitalWrite(1,LOW);digitalWrite(2,HIGH);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,LOW);digitalWrite(6,HIGH);digitalWrite(7,LOW);
digitalWrite(8,LOW);digitalWrite(9,LOW);digitalWrite(10,HIGH);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,LOW);digitalWrite(A0,HIGH);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,LOW); digitalWrite(1,LOW);digitalWrite(2,LOW);digitalWrite(3,HIGH);digitalWrite(4,LOW);digitalWrite(5,LOW);digitalWrite(6,LOW);digitalWrite(7,HIGH);
digitalWrite(8,LOW);digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,HIGH);digitalWrite(12,LOW);digitalWrite(13,LOW);digitalWrite(A0,LOW);digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,LOW); digitalWrite(1,LOW);digitalWrite(2,HIGH);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,LOW);digitalWrite(6,HIGH);digitalWrite(7,LOW);
digitalWrite(8,LOW);digitalWrite(9,LOW);digitalWrite(10,HIGH);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,LOW);digitalWrite(A0,HIGH);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,LOW); digitalWrite(1,HIGH);digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,HIGH);digitalWrite(6,LOW);digitalWrite(7,LOW);
digitalWrite(8,LOW);digitalWrite(9,HIGH);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,HIGH);digitalWrite(A0,LOW);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
digitalWrite(0,HIGH); digitalWrite(1,LOW);digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,HIGH);digitalWrite(5,LOW);digitalWrite(6,LOW);digitalWrite(7,LOW);
digitalWrite(8,HIGH);digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,HIGH);digitalWrite(13,LOW);digitalWrite(A0,LOW);digitalWrite(A1,LOW);
digitalWrite(A2,LOW);digitalWrite(A3,LOW);digitalWrite(A4,LOW);digitalWrite(A5,LOW);
delay(200);
}
``````

So if I understand correctly, this pattern

``````digitalWrite(0,HIGH); digitalWrite(1,HIGH);digitalWrite(2,HIGH);digitalWrite(3,HIGH);
digitalWrite(4,HIGH);digitalWrite(5,HIGH);digitalWrite(6,HIGH);digitalWrite(7,HIGH);
digitalWrite(8,HIGH);digitalWrite(9,HIGH);digitalWrite(10,HIGH);digitalWrite(11,HIGH);
digitalWrite(12,HIGH);digitalWrite(13,HIGH);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);digitalWrite(A3,HIGH);digitalWrite(A4,HIGH);digitalWrite(A5,HIGH);
``````

Turns on all LEDs that A2 drives. Do you have current limit resistors in place? Is there some kind of buffering in place?
The '328P chip the Uno has a current limit of 200mA total on the VCC or GND pin, about 20mA on any single IO pin, and has limits on current per groups of pins. So depending on your hardware setup, you may be overstressing the chip and setting yourself for an early chip death.

1. This says loop thru the 16 elements (0 to 15) of the array “ledPin” and make each pin an output.
for (int pin=0; pin<16; pin++){

pinMode (ledPin[pin],OUTPUT);
}

1. Instead of all the digitalWrites, you could write a couple of similar loop to command the outputs high or low:
``````byte layer0[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,};

for (byte pin=0; pin<16; pin++){

pinMode (ledPin[pin], layer0[pin]);
}
digitalWrite (levelPin[0], LOW);
delay(200);
digitalWrite (levelPin[0], HIGH);

repeat for the next 3 layers:
for (byte pin=0; pin<16; pin++){

pinMode (ledPin[pin], layer1[pin]);
}
digitalWrite (levelPin[1], LOW);
delay(200);
digitalWrite (levelPin[1], HIGH);
``````
1. Put each pattern in a loop:
``````for (byte loopCount = 0; loopCount<10; loopCount = loopCount +1){
// pattern code from above
}
// repeat for next pattern
``````

Use byte instead of int for values <=255, save on SRAM so you can have more patterns.
Or look into putting the patterns in PROGMEM so they are saved in flash, then you can have even more.
After that, you can look at storing this:
byte layer0 = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,};
as this
int layer0 = {0xFFFF, 0xFFF0, etc,};
where each 4 characters hold the data for one layer. 2 bytes of memory vs 16.

I have 100ohm resistors on all 16 anode legs

That code has an issue with the layer0 and layer1, plus I have no idea where all that info goes. I put the byte at the top...under the void setup...in the void loop...I changed some names and numbers to match the words I used to set it up...nothing. Plus Im not sure if this belongs with a different setup. All cathodes and Anodes are setup as outputs...and I don't have shift registers. I haven't seen anyone else running this this way...I got this setup out of a book.

Any other ideas anyone?