Skip to content

Instantly share code, notes, and snippets.

@davemenninger
Created January 19, 2011 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemenninger/785487 to your computer and use it in GitHub Desktop.
Save davemenninger/785487 to your computer and use it in GitHub Desktop.
turns each row on in order
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI
#define NumLEDs 56
#define rows 8
#define columns 7
int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;
int current_img[columns][rows][3] = {0};
int next_img[columns][rows][3] = {0};
int current_frame = 0;
int num_frames = 8;
void setup() {
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
}
void SB_SendPacket() {
if (SB_CommandMode == B01) {
SB_RedCommand = 65;
SB_GreenCommand = 50;
SB_BlueCommand = 50;
}
SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCommand;
while(!(SPSR & (1<<SPIF)));
}
void WriteLEDArray() {
SB_CommandMode = B00; // Write to PWM control registers
for (int h = 0;h<NumLEDs;h++) {
SB_RedCommand = LEDChannels[h][0];
SB_GreenCommand = LEDChannels[h][1];
SB_BlueCommand = LEDChannels[h][2];
SB_SendPacket();
}
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
SB_CommandMode = B01; // Write to current control registers
for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
delayMicroseconds(15);
digitalWrite(latchpin,HIGH); // latch data into registers
delayMicroseconds(15);
digitalWrite(latchpin,LOW);
}
void ConvertImg(){
//converts the "2D" current_img array into the "1D" LEDChannels array
for ( int x = 0; x < columns; x++ ){
for ( int y = 0; y < rows; y++ ){
int h = 0;
//the following deals with the alternating direction of the rows
if ( x%2 == 0 ){
h = (rows*x) + y;
}
else{
h = (rows*x) + (rows-y-1);
}
LEDChannels[h][0] = current_img[x][y][0];
LEDChannels[h][1] = current_img[x][y][1];
LEDChannels[h][2] = current_img[x][y][2];
}
}
}
void loop() {
//do something to modify the next_img
if ( current_frame == 0 ){
//turn on row 0
for ( int x = 0; x < columns; x++ ){
//this row on
next_img[x][0][0] = 1000;
next_img[x][0][1] = 1000;
next_img[x][0][2] = 1000;
//last row off
next_img[x][columns][0] = 0;
next_img[x][columns][1] = 0;
next_img[x][columns][2] = 0;
}
}
else {
//turn on row
for ( int x = 0; x < columns; x++ ){
//this row on
next_img[x][current_frame][0] = 1000;
next_img[x][current_frame][1] = 1000;
next_img[x][current_frame][2] = 1000;
//turn previous row off
next_img[x][current_frame-1][0] = 0;
next_img[x][current_frame-1][1] = 0;
next_img[x][current_frame-1][2] = 0;
}
}
current_frame++;
if ( current_frame >= num_frames ) current_frame = 0;
//no need to alter below this point
//copy the next_img to the current_img
memcpy( current_img , next_img , sizeof(current_img) );
//memcpy( current_img , next_img , ( NumLEDs * 3 * sizeof(int) ) );
//change the 2D grid of values into a 1D chain of values
ConvertImg();
//write out the current array to the "display"
WriteLEDArray();
//pause between refresh
delay(400);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment