Help with LED Cube Programming

Hello,

I have been testing a 3x3x3 LED Cube. I tried to make programming sequences easier by creating a couple Libraries.

Frame.h

/*
Michael B
8/13/2011
LED Cube Programming Test
controls 3x3x3 cube by assigning each LED a number
*/
#ifndef Frames
#define Frames

#include "WProgram.h"



class Frame{
private:
	//variables
	bool toggled;
	unsigned int count;
	int LED[27];
	int pixelSize;
	bool lastFrame;
	int C[9];
	int A[3];
	//private member functions
	void On(int led);
	void Off(int led);

public:
	//public member functions
	Frame();
	Frame(int led[], int size);
	void setLED(int led[], int size);
	void frameCycle();
	int getSize();
};

#endif

Frame.cpp

/*
Michael B
8/13/2011
LED Cube Programming Test
controls 3x3x3 cube by assigning each LED a number
*/
#include "WProgram.h"
#include "Frame.h"

const int LAYER = 9;

Frame::Frame(){
	for(int i = 0; i < 27; i++){
		LED[i] = 0;
	}
	pixelSize = 0;
	toggled = false;
	count = 0;
	lastFrame = false;
	for(int i = 0; i < 9; i++){
		C[i] = i+2;
	}
	for(int i = 0; i < 3; i++){
		A[i] = i+11;
	}
}

Frame::Frame(int led[], int size = 0){
	for(int i = 0; i < 27; i++){
		LED[i] = led[i];
	}
	pixelSize = size;
	toggled = false;
	count = 0;
	lastFrame = false;
	for(int i = 0; i < 9; i++){
		C[i] = i+2;
	}
	for(int i = 0; i < 3; i++){
		A[i] = i+11;
	}
}

void Frame::setLED(int led[], int size){
	for(int i = 0; i < 27; i++){
		LED[i] = led[i];
	}
	pixelSize = size;
}

void Frame::frameCycle(){
	//checks to see if the led is on or off
	if (toggled == false){
		//turns it on
		On(LED[count]);
		toggled = true;
	}else{
		//turns it off
		Off(LED[count]);
		toggled = false;
		//increase for next LED pixel in frame
		count++;
		if(count >= pixelSize){
			count = 0;	
		}
	}
}

void Frame::On(int led){
	int Anode = 0;
	int Cathode = 0;

	//figures out which anode and cathode to switch for
	//the LED number given
	Anode = (int) led / LAYER;
	Cathode = (int) led % LAYER;

	//LED on
	digitalWrite(A[Anode], 1);
	digitalWrite(C[Cathode], 0);
}

void Frame::Off(int led){
	int Anode = 0;
	int Cathode = 0;

	//figures out which anode and cathode to switch for
	//the LED number given
	Anode = (int) led / LAYER;
	Cathode = (int) led % LAYER;
	
	//LED off
	digitalWrite(A[Anode], 0);
	digitalWrite(C[Cathode], 1);
}

int Frame::getSize(){
	return pixelSize;
}

Sequence.h

/*
Michael B
8/13/2011
LED Cube Programming Test
controls 3x3x3 cube by assigning each LED a number
*/
#ifndef Sequences
#define Sequences

#include "WProgram.h"
#include "Frame.h"



class Sequence{
private:
	Frame frames[15];
	int frameSize;
	int delaySize;
	int frameCount;
	int delayCount;
	int toggleCount;
	int toggleMAX;
public:
	Sequence(int size);
	void set(Frame frameList[], int size, int delay);
	void setFrameSize(int size);
	void setDelaySize(int size);
	void addFrame(Frame frame);
	void sequenceCycle();
};

#endif

Sequence.cpp

/*
Michael B
8/13/2011
LED Cube Programming Test
controls 3x3x3 cube by assigning each LED a number
*/
#include "WProgram.h"
#include "Sequence.h"



Sequence::Sequence(int size){
	frameSize = size;
	delaySize = 0;
	frameCount = 0;
	delayCount = 0;
	toggleCount = 1;
	toggleMAX = 2;
}

void Sequence::set(Frame frameList[], int size, int delay){
	for(int i = 0; i < size; i++){
		frames[i] = frameList[i];
	}
	frameSize = size;
	delaySize = delay;
	frameCount = 0;
	delayCount = 0;
	toggleCount = 1;
	toggleMAX = 2;
}

void Sequence::setFrameSize(int size){
	frameSize = size;
}

void Sequence::setDelaySize(int size){
	delaySize = size;
}

void Sequence::addFrame(Frame frame){
	frames[frameSize] = frame;
	frameSize++;
}

void Sequence::sequenceCycle(){
	//call frame cycle
	frames[frameCount].frameCycle();
	//increase count, which delays the frame from switching
	delayCount++;
	if(delayCount > delaySize){
		//reset delay count
		delayCount = 0;
		//increase a two count variable
		//The logic is that i need the same frame cycle to be called twice
		//before switching to the next frame
		//One cycle turns the LEDs on
		//One cycle turns the LEDs off
		toggleCount++;
		if(toggleCount > toggleMAX){
			//increase to next frame
			frameCount++;
			toggleCount = 1;
		}
		//reset to beginning frame
		if(frameCount > frameSize - 1){
			frameCount = 0;
		}
	}
}

//LED.pde

/*
Michael B
8/13/2011
LED Cube Programming Test
controls 3x3x3 cube by assigning each LED a number
*/
#include <avr/pgmspace.h>
//Sequence of frames
#include <Sequence.h>
//Set of pixels(individual LEDs)
#include <Frame.h>
//Timer1 Library from www.Arduino.com
#include <TimerOne.h>


//Anode and Cathode pins
int C[9] = {2,3,4,5,6,7,8,9,10};
int A[3] = {11,12,13};

//set up pixel arrays
int Square1[4][16] = {
  //{0,1,2,3,4,5,6,7,8,0,0,0,0,0,0,9},
  //{9,10,11,12,13,14,15,16,17,0,0,0,0,0,0,9},
  //{18,19,20,21,22,23,24,25,26,0,0,0,0,0,0,9},
  //{6,7,8,15,16,17,24,25,26,0,0,0,0,0,0,9},
  //{3,4,5,12,13,14,21,22,23,0,0,0,0,0,0,9},
  {0,1,2,9,10,11,18,19,20,0,0,0,0,0,0,9},
  {2,5,6,7,8,11,14,15,16,17,20,23,24,25,26,15},
  {1,3,4,5,7,10,12,13,14,16,19,21,22,23,25,15},
  {0,1,2,3,6,9,10,11,12,15,18,19,20,21,24,15}
};
  
//initialize 9 frames
Frame v[4];

//initialize sequence of frames
Sequence Test(4);


void setup(){

  //set pinouts
  //turn off all LEDs
  for(int i = 0; i < 9; i++){
    pinMode(C[i], OUTPUT);
    digitalWrite(C[i], 1);
  }
  for(int i = 0; i < 3; i++){
    pinMode(A[i], OUTPUT);
    digitalWrite(A[i], 0);
  }
  

  //add LED arrays to sequence
  for(int i = 0; i < 4; i++){
    v[i].setLED(Square1[i],Square1[i][15]);
   
  }
  
  
  //set up timer 1
  Timer1.initialize(500);
  Timer1.pwm(9,512);
  Timer1.attachInterrupt(trigger);
  digitalWrite(9,1);  
  
  //setup sequence and delay
  Test.set(v,4,400);
  
}

void loop(){  
}

//interrupt call
void trigger(){ 
  //sequence cycle call
  Test.sequenceCycle();
}

The idea is that there are pixels in a Frame (as in frames per second), and Frames in a sequence. That way I could have a lot of different Sequences and it would be easy to interchange them. I apologize if the code is hard to read. I am an EE undergrad.

The code compiles and uploads fine. My problem is that when I include more then 4 Frames in a Sequence or include more than one Sequence, my Arduino, freezes i guess. I do not get any response from it.

Am I running out of memory or something? If so, how do I create a lot of sequences on an Arduino for a LED cube?

you can change all the arrays to BYTEs rather than INT(unless you are using numbers that are bigger than 255)

Great idea, kirill578!

That actually helped a lot. Not only does the Arduino run better, but I was able to delete a big chunk of code and variables.

Now I just have to think of patterns, then build a bigger cube.