Code for LED Cube

Hey guys,
SO we have a 4x4x4 LED cube wired in the same way a Hypnocube (http://www.hypnocube.com/Docs/HC4_Instructions.pdf; section titled "The Bad") would be wired (vertical plane with all the cathode leads connected via a cross wire then ground drop wires and all the cathodes connected via one drop wire). Our professor provided us with code to run it but it's not working. Here's the code-

#include <avr/pgmspace.h> // allows use of PROGMEM to store patterns in flash

#define CUBESIZE 4
#define PLANESIZE CUBESIZE*CUBESIZE
#define PLANETIME 3333 // time each plane is displayed in us -> 100 Hz refresh
#define TIMECONST 20 // multiplies DisplayTime to get ms - why not =100?

// LED Pattern Table in PROGMEM - last column is display time in 100ms units
// TODO this could be a lot more compact but not with binary pattern representation
prog_uchar PROGMEM PatternTable[] = {
// blink on and off

B0001,B0000,B0000,B0000,B0001,B0000,B0000,....,B0000,10,

// this is a dummy element for end of table (duration=0) aka !!!DO NOT TOUCH!!!
B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, B0000, 0
};

/*
** Defining pins in array makes it easier to rearrange how cube is wired
** Adjust numbers here until LEDs flash in order - L to R, T to B
** Note that analog inputs 0-5 are also digital outputs 14-19!
** Pin DigitalOut0 (serial RX) and AnalogIn5 are left open for future apps
*/

int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int PlanePin[] = {16, 17, 18, 19};

// initialization
void setup()
{
int pin; // loop counter
// set up LED pins as output (active HIGH)
for (pin=0; pin<PLANESIZE; pin++) {
pinMode( LEDPin[pin], OUTPUT );
}
// set up plane pins as outputs (active LOW)
for (pin=0; pin<CUBESIZE; pin++) {
pinMode( PlanePin[pin], OUTPUT );
}
}

// display pattern in table until DisplayTime is zero (then repeat)
void loop()
{
// declare variables
byte PatternBuf[PLANESIZE]; // saves current pattern from PatternTable
int PatternIdx;
byte DisplayTime; // time*100ms to display pattern
unsigned long EndTime;
int plane; // loop counter for cube refresh
int patbufidx; // indexes which byte from pattern buffer
int ledrow; // counts LEDs in refresh loop
int ledcol; // counts LEDs in refresh loop
int ledpin; // counts LEDs in refresh loop

// Initialize PatternIdx to beginning of pattern table
PatternIdx = 0;
// loop over entries in pattern table - while DisplayTime>0
do {
// read pattern from PROGMEM and save in array
memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );
PatternIdx += PLANESIZE;
// read DisplayTime from PROGMEM and increment index
DisplayTime = pgm_read_byte_near( PatternTable + PatternIdx++ );
// compute EndTime from current time (ms) and DisplayTime
EndTime = millis() + ((unsigned long) DisplayTime) * TIMECONST;

// loop while DisplayTime>0 and current time < EndTime
while ( millis() < EndTime ) {
patbufidx = 0; // reset index counter to beginning of buffer
// loop over planes
for (plane=0; plane<CUBESIZE; plane++) {
// turn previous plane off
if (plane==0) {
digitalWrite( PlanePin[CUBESIZE-1], HIGH );
} else {
digitalWrite( PlanePin[plane-1], HIGH );
}

// load current plane pattern data into ports
ledpin = 0;
for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
}
patbufidx++;
}

// turn current plane on
digitalWrite( PlanePin[plane], LOW );
// delay PLANETIME us
delayMicroseconds( PLANETIME );
} // for plane
} // while <EndTime
} while (DisplayTime > 0); // read patterns until time=0 which signals end
}

Please help :frowning: all of us have no coding experience and its due Monday...also maybe are we not hooking it up to the Arduino board properly? The codde works with a single LED... :~

When posting code, please wrap it in code tags (the "#" button in the editor.)

Our professor provided us with code to run it but it's not working.

Ok. It's not working. You need to explain what "not working" means.

The cube does nothing, it's completely unresponsive; the LED on Pin13 flashes so I know the codes there and as I said it worked with a single LED earlier. I dont know if it's how were attaching it to the board, or if there's not enough current to drive all the LED's, or some issue like that. OR if it's just the code won't work with our cube because maybe it's wired differently from his.

So you are using the instructions on how to make the LED matrix but not the controller board?

I didn't see any kind of current limit resistors in the matrix. Are you using those when connected to the Arduino?

We have the cathode drop wires all connected so that there is one ground input into the Arduino board and then one anode drop wire from each column connected to a 220 ohm resistor (because our transistors and shift registers didn't come in the mail when they were supposed to we had to forgo the RGB aspect of it until next week). So each layer (vertical array) has 4 anode leads and one resistor per lead for a total of 16 input anode leads to the Arduino and 16 220 ohm resistors. Simply all we have are the wired LEDS, resistors and the Arduino board. We had the ground lead in the GND pin, then the anode leads in pins 0-13 and also A0 and A2 for a total of 16 inputs.

Works Perfectly for me :slight_smile: :slight_smile: :slight_smile: :slight_smile:

For perfect hardware details
Visit:

These 16 pins will be written high
int LEDPin[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

while these 4 pins will be written low one at a time.
int PlanePin[] = {16, 17, 18, 19};

If you have the cathodes from all 4 layers connected to Arduino Gnd, then anytime you write an anode high you are turning on all LEDs at once, and overloading the anode driver pin.
Does the '328P chip feel warm or hot at all when turned on?