Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Main program for RGB LED CUBE 8x8x8
#include <SPI.h>
#define latch_pin 4
#define blank_pin 5
#define data_pin 51
#define clock_pin 52
#define layer1 26
#define layer2 27
#define layer3 28
#define layer4 29
#define layer5 30
#define layer6 31
#define layer7 32
#define layer8 33
int layerArray[8] = {layer1, layer2, layer3, layer4, layer5, layer6, layer7, layer8};
int lastAnode;
byte red[4][64];
byte blue[4][64];
byte green[4][64];
#define BAM_RESOLUTION 4
const byte Size_X = 8;
const byte Size_Y = 8;
const byte Size_Z = 8;
int level=0;
int anodeLevel=0;
int BAM_Bit, BAM_Counter=0;
void setup(){
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
noInterrupts();
TCCR1A = B00000000;
TCCR1B = B00001011;
TIMSK1 = B00000010;
OCR1A=30;
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(layer1, OUTPUT);
pinMode(layer2, OUTPUT);
pinMode(layer3, OUTPUT);
pinMode(layer4, OUTPUT);
pinMode(layer5, OUTPUT);
pinMode(layer6, OUTPUT);
pinMode(layer7, OUTPUT);
pinMode(layer8, OUTPUT);
SPI.begin();
interrupts();
}
void loop()
{
clearfast();
LED(4, 5, 6, 3, 6, 13);
delay(5000);
clearfast();
LED(0,0,0,15,0,0);
delay(5000);
LED(7,0,0,0,15,0);
delay(5000);
LED(7,7,0,0,0,15);
delay(5000);
LED(0,7,0,15,15,0);
delay(5000);
LED(0,7,7,15,0,15);
delay(5000);
LED(7,0,7,0,15,15);
delay(5000);
LED(0,0,7,15,15,15);
delay(5000);
LED(7,7,7,15,9,0);
delay(5000);
}
void LED(int Z, int Y, int X, int R, int G, int B)
{
X = constrain(X, 0, Size_X - 1);
Y = constrain(Y, 0, Size_Y - 1);
Z = constrain(Z, 0, Size_Z - 1);
R = constrain(R, 0, (1 << BAM_RESOLUTION) - 1);
G = constrain(G, 0, (1 << BAM_RESOLUTION) - 1);
B = constrain(B, 0, (1 << BAM_RESOLUTION) - 1);
int WhichByte = int(((Z << 6) + (Y << 3) + X) >> 3);
int WhichBit = ((Z << 6) + (Y << 3) + X) - (WhichByte << 3) ;
for (byte BAM = 0; BAM < BAM_RESOLUTION; BAM++)
{
//*** RED ***
bitWrite(red[BAM][WhichByte], WhichBit, bitRead(R, BAM));
//*** GREEN ***
bitWrite(green[BAM][WhichByte], WhichBit, bitRead(G, BAM));
//*** BLUE ***
bitWrite(blue[BAM][WhichByte], WhichBit, bitRead(B, BAM));
}
}
ISR(TIMER1_COMPA_vect)
{
PORTE |= 1<<blank_pin;
if(BAM_Counter==8)
BAM_Bit++;
else
if(BAM_Counter==24)
BAM_Bit++;
else
if(BAM_Counter==56)
BAM_Bit++;
BAM_Counter++;
switch (BAM_Bit)
{
case 0:
//Red
mySPI(red[0][level + 0]);
mySPI(red[0][level + 1]);
mySPI(red[0][level + 2]);
mySPI(red[0][level + 3]);
mySPI(red[0][level + 4]);
mySPI(red[0][level + 5]);
mySPI(red[0][level + 6]);
mySPI(red[0][level + 7]);
//Green
mySPI(green[0][level + 0]);
mySPI(green[0][level + 1]);
mySPI(green[0][level + 2]);
mySPI(green[0][level + 3]);
mySPI(green[0][level + 4]);
mySPI(green[0][level + 5]);
mySPI(green[0][level + 6]);
mySPI(green[0][level + 7]);
//Blue
mySPI(blue[0][level + 0]);
mySPI(blue[0][level + 1]);
mySPI(blue[0][level + 2]);
mySPI(blue[0][level + 3]);
mySPI(blue[0][level + 4]);
mySPI(blue[0][level + 5]);
mySPI(blue[0][level + 6]);
mySPI(blue[0][level + 7]);
break;
case 1:
//Red
mySPI(red[1][level + 0]);
mySPI(red[1][level + 1]);
mySPI(red[1][level + 2]);
mySPI(red[1][level + 3]);
mySPI(red[1][level + 4]);
mySPI(red[1][level + 5]);
mySPI(red[1][level + 6]);
mySPI(red[1][level + 7]);
//Green
mySPI(green[1][level + 0]);
mySPI(green[1][level + 1]);
mySPI(green[1][level + 2]);
mySPI(green[1][level + 3]);
mySPI(green[1][level + 4]);
mySPI(green[1][level + 5]);
mySPI(green[1][level + 6]);
mySPI(green[1][level + 7]);
//Blue
mySPI(blue[1][level + 0]);
mySPI(blue[1][level + 1]);
mySPI(blue[1][level + 2]);
mySPI(blue[1][level + 3]);
mySPI(blue[1][level + 4]);
mySPI(blue[1][level + 5]);
mySPI(blue[1][level + 6]);
mySPI(blue[1][level + 7]);
break;
case 2:
//Red
mySPI(red[2][level + 0]);
mySPI(red[2][level + 1]);
mySPI(red[2][level + 2]);
mySPI(red[2][level + 3]);
mySPI(red[2][level + 4]);
mySPI(red[2][level + 5]);
mySPI(red[2][level + 6]);
mySPI(red[2][level + 7]);
//Green
mySPI(green[2][level + 0]);
mySPI(green[2][level + 1]);
mySPI(green[2][level + 2]);
mySPI(green[2][level + 3]);
mySPI(green[2][level + 4]);
mySPI(green[2][level + 5]);
mySPI(green[2][level + 6]);
mySPI(green[2][level + 7]);
//Blue
mySPI(blue[2][level + 0]);
mySPI(blue[2][level + 1]);
mySPI(blue[2][level + 2]);
mySPI(blue[2][level + 3]);
mySPI(blue[2][level + 4]);
mySPI(blue[2][level + 5]);
mySPI(blue[2][level + 6]);
mySPI(blue[2][level + 7]);
break;
case 3:
//Red
mySPI(red[3][level + 0]);
mySPI(red[3][level + 1]);
mySPI(red[3][level + 2]);
mySPI(red[3][level + 3]);
mySPI(red[3][level + 4]);
mySPI(red[3][level + 5]);
mySPI(red[3][level + 6]);
mySPI(red[3][level + 7]);
//Green
mySPI(green[3][level + 0]);
mySPI(green[3][level + 1]);
mySPI(green[3][level + 2]);
mySPI(green[3][level + 3]);
mySPI(green[3][level + 4]);
mySPI(green[3][level + 5]);
mySPI(green[3][level + 6]);
mySPI(green[3][level + 7]);
//Blue
mySPI(blue[3][level + 0]);
mySPI(blue[3][level + 1]);
mySPI(blue[3][level + 2]);
mySPI(blue[3][level + 3]);
mySPI(blue[3][level + 4]);
mySPI(blue[3][level + 5]);
mySPI(blue[3][level + 6]);
mySPI(blue[3][level + 7]);
if(BAM_Counter==120)
{
BAM_Counter=0;
BAM_Bit=0;
}
break;
}
lastAnode = (anodeLevel-1);
if (anodeLevel == 0) { lastAnode = 7; }
digitalWrite(layerArray[lastAnode], LOW);
digitalWrite(layerArray[anodeLevel], HIGH);
PORTE |= 1<<latch_pin;
PORTE &= ~(1<<latch_pin);
delayMicroseconds(3);
PORTE &= ~(1<<blank_pin);//Blank pin LOW to turn on the LEDs with the new data
//delayMicroseconds(3);
anodeLevel++;
level = level+8;
if(anodeLevel==8)
anodeLevel=0;
if(level==64)
level=0;
pinMode(blank_pin, OUTPUT);
}
void clearfast ()
{
for (unsigned char j=0; j<64; j++)
{
red[0][j] = 0;
red[1][j] = 0;
red[2][j] = 0;
red[3][j] = 0;
green[0][j] = 0;
green[1][j] = 0;
green[2][j] = 0;
green[3][j] = 0;
blue[0][j] = 0;
blue[1][j] = 0;
blue[2][j] = 0;
blue[3][j] = 0;
}
}
inline static uint8_t mySPI(uint8_t mydata){
SPDR = mydata;
asm volatile("nop");
asm volatile("nop");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment