Hi all,
arduino newbie here. I tried programming a cool plasma ball display on the sparkfun RGB LED Matrix with the SPI backpack
Hi all,
arduino newbie here. I tried programming a cool plasma ball display on the sparkfun RGB LED Matrix with the SPI backpack
(http://www.sparkfun.com/commerce/product_info.php?products_id=760)
It is supposed to look something like this: Arduino Plasma.mov - YouTube
But i cannot get my code to work. here is my code
//
// Plasma generation for the Arduino and a RGB Serial Backpack Matrix from SparkFun Electronics
//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
void LED_Setup()
{
//SPI Bus setup
SPCR = B01010001; //SPI Registers
SPSR = SPSR & B11111110;
//Set the pin modes for the RGB matrix
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
//Make sure the RGB matrix is deactivated
digitalWrite(SLAVESELECT,HIGH);
}
//
// tables for plasma
//
// 8 by 8 -> 64 bytes
// 16 by 16 -> 256 bytes
// color table 256 bytes
//
#define PLASMA_W 8
#define PLASMA_H 8
#define TABLE_W 16
#define TABLE_H 16
unsigned char gPlasma[PLASMA_W*PLASMA_H];
unsigned char gTable1[TABLE_W*TABLE_H];
unsigned char gTable2[TABLE_W*TABLE_H];
unsigned char gColorTable[256];
float gCircle1, gCircle2, gCircle3, gCircle4, gCircle5, gCircle6, gCircle7, gCircle8;
int gRoll;
void Plasma_CalcTable1 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
gTable1[index] = (unsigned char) ((sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4) *5 );
}
}
}
void Plasma_CalcTable2 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
float temp = sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4;
gTable2[index] = (sin(temp/9.5)+1)*90;
}
}
}
void Plasma_SetColor (int index, unsigned char red, unsigned char green, unsigned char blue)
{
unsigned char new_color = (red & 0xE0) + ((green & 0xE0) >> 3) + ((blue & 0xC0) >> 6);
gColorTable [index] = new_color;
}
double gRed, gGreen, gBlue;
#define color(u,a) (cos((u)+(a))+1)*127
void BuildColorTable()
{
double u;
int i;
for (i=0; i<256; i++)
{
u=2*PI/256*i;
Plasma_SetColor(i,color(u,gRed),color(u,gGreen),color(u,gBlue));
}
gRed+=0.05;
gGreen-=0.05;
gBlue+=0.1;
}
void Plasma_Setup ()
{
gCircle1 = 0;
gCircle2 = 0;
gCircle3 = 0;
gCircle4 = 0;
gCircle5 = 0;
gCircle6 = 0;
gCircle7 = 0;
gCircle8 = 0;
gRoll = 0;
for (int i=0; i<256 ;i++)
{
gPlasma[i] = 0;
Plasma_CalcTable1();
Plasma_CalcTable2();
gRed=1.0/6.0*PI;
gGreen=3.0/6.0*PI;
gBlue=5.0/6.0*PI;
BuildColorTable();
}
}
void setup()
{
LED_Setup();
Plasma_Setup();
}
void ComputePlasma (int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int roll)
{
int i, j;
for (i=0; i< PLASMA_H; i++)
{
for (j=0; j< PLASMA_W; j++)
{
int index = (i*PLASMA_W)+j;
unsigned int new_height = gTable1[TABLE_W*(i+y1)+j+x1];
new_height = new_height + roll;
new_height = new_height + gTable2[TABLE_W*(i+y2)+j+x2];
new_height = new_height + gTable2[TABLE_W*(i+y3)+j+x3]; // + gTable2[TABLE_W*(i+y4)+j+x4];
new_height = new_height & 0xFF;
new_height = gColorTable[new_height];
gPlasma[index] = new_height;
}
}
}
char SendOneChar (volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
//return SPDR; // return the received byte
}
void SendToLEDs ()
{
int i;
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(i=0; i<256;i++)
{
SendOneChar((char)gPlasma[i]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
}
void loop()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
float ratio = 2;
gCircle1 = gCircle1 + (ratio * (0.085/6));
gCircle2 = gCircle2 -(ratio * (0.1/6));
gCircle3 = gCircle3 + (ratio * (0.3/6));
gCircle4 = gCircle4 - (ratio * (0.2/6));
gCircle5 = gCircle5 + (ratio * (0.4/6));
gCircle6 = gCircle6 - (ratio * (0.15/6));
gCircle7 = gCircle7 + (ratio * (0.35/6));
gCircle8 = gCircle8 - (ratio * (0.05/6));
x2=(PLASMA_W/2)+(PLASMA_W/2)*sin(gCircle1);
y2=(PLASMA_H/2)+(PLASMA_H/2)*cos(gCircle2);
x1=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle3);
y1=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle4);
x3=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle5);
y3=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle6);
x4=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle7);
y4=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle8);
ComputePlasma(x1,y1,x2,y2,x3,y3,x4,y4,gRoll);
gRoll = gRoll + 1;
SendToLEDs();
delay (20);
}
any help, and urgently please (need this done in about a week), thanks.
i also have 2 more bits of code, one that randomly colors a pixel, and one that prints 8 letters on the screen. i want to combine all 3 of these pieces of code into one long looping animation (For example, it displays the 8 letters 3-5 times, then plays the random pixel animation for 15 seconds, then plays the plasma animation for 15 seconds, then repeats). can someone help me with this?
// Blinking LEDs
// SPI register bit positions
#define RX_IN 0
#define TX_OUT 1
#define CS 2
#define MOSI 3
#define MISO 4
#define SCK 5
// Ardrino Duemilanove pin numbers for SPI
#define CHIPSELECT 10 // CS
#define DATAOUT 11 // MOSI
#define DATAIN 12 // MISO (not used)
#define SPICLOCK 13 // SCK
// SparkFun RGB LED backpack officially supported colors
typedef unsigned char ledcolorType;
#define colorBlack 0x00
#define colorRed 0xe0
#define colorGreen 0x1c
//#define colorBlue 0x03
#define colorOrange 0xfc
//#define colorMagneta 0xe3
//#define colorTeal 0x1f
#define colorWhite 0xff
#define colorLen 13
static const ledcolorType colors[colorLen] =
//{
//colorBlack, colorRed, colorGreen, //colorBlue,
//colorOrange, //colorMagneta, colorTeal,
//colorWhite
//};
{
32,40,64,72,0,96,128,160,192,224,228,92,200
};
// SparkFun RGB LED backpack frame geometry
#define rowLen 8
#define colLen 8
typedef struct
{
ledcolorType pixels[rowLen][colLen];
} frameType;
static void SetFrameToColor(frameType& frame, const ledcolorType color)
{
for (unsigned int col = 0; col < colLen; col++)
for (unsigned int row = 0; row < rowLen; row++)
frame.pixels[row][col] = color;
}
static void SetFrameToBlack(frameType& frame) {SetFrameToColor(frame, colorBlack);}
static unsigned char PickRandomColor(void) {return colors[random(colorLen)];}
static void ChangeOneRandomPixelToColor(frameType& frame, const ledcolorType color)
{
frame.pixels[random(rowLen)][random(colLen)] = color;
}
static void SetBackpackSelectStatus(const unsigned int status)
{
digitalWrite(CHIPSELECT, status); delayMicroseconds(500);
}
static void BackpackEnable(void) {SetBackpackSelectStatus(LOW);}
static void BackpackDisable(void) {SetBackpackSelectStatus(HIGH);}
static unsigned char WriteSPIByte(unsigned char data)
{
SPDR = data; while (!(SPSR & (1 << SPIF))); return SPDR;
}
static void SetBackpackDaisyChainLength(const unsigned int length)
{
BackpackEnable();
WriteSPIByte('%'); WriteSPIByte(length);
BackpackDisable();
}
static void WriteFrame(const frameType& frame)
{
BackpackEnable();
for (unsigned int row = 0; row < rowLen; row++)
for (unsigned int col = 0; col < colLen; col++)
WriteSPIByte(frame.pixels[row][col]);
BackpackDisable();
}
// The last real frame displayed
static frameType realframe;
// One time setup
void setup()
{
// Initialize the Arduino SPI pin directions
pinMode(CHIPSELECT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK, OUTPUT);
// Magical initialization of the SPI registers
volatile byte clr;
DDRB = (1 << CS) | (1 << MOSI) | (1 << SCK);
DDRD = (1 << TX_OUT);
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
SPSR = SPSR & 0xfe;
clr = SPSR; clr = SPDR;
// Set the backpack to its default state
BackpackDisable();
SetBackpackDaisyChainLength(1); // Be careful with this one
// Initial display is all black pixels
SetFrameToBlack(realframe); WriteFrame(realframe);
}
// Main loop
void loop()
{
delay(1);
ChangeOneRandomPixelToColor(realframe, PickRandomColor());
WriteFrame(realframe);
}
#define CHIPSELECT 10//Chip Select Line
#define SPICLOCK 13//Master Clock
#define DATAOUT 11//DataOut to DataIn on Backpack
#define DATAIN 12//DataIn from DataOut on Backpack
//simple array for data frames - you can use 0-3 for the color settings on the RG display or 0-7 for the RGB Matrixs//
int databuffer[8][8][8] ={
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,32,32,32,0},
{0,0,0,0,0,0,0,3}
},
{
{0,0,0,0,0,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,32,32,32,32,0,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,0,0,0,0,3,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,3,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,0,3,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,3,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,0,32,32,32,32,0,0},
{0,0,3,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,32,228,32,0},
{0,32,0,32,0,232,32,0},
{0,32,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,3,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,228,32,0},
{0,0,32,0,0,232,32,0},
{0,0,0,32,0,232,32,0},
{0,0,0,32,228,232,32,0},
{0,0,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{3,0,0,0,0,0,0,0}
}
} ;
int z=0;
// SPI data load / transfer function - thanks Heather Dewey-Hagborg //
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
}
void clearDisplay() {
for (int i=0;i<8;i++) for (int j=0;j<8;j++)
{ spi_transfer( 0 ); }
delay(50);
}
void setup()
{
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device
SPCR = B01010001; //SPI Registers
SPSR = SPSR & B11111110; //make sure the speed is 125KHz
clr=SPSR;
clr=SPDR;
delay(10);
clearDisplay();
}
byte ddd[]={0x00,0x78,0x64,0x62,0x62,0x64,0x78,0x00};
void loop()
{
int z=0;
//loop though frames #8 //
for (int z=0;z<8;z++) {
delay(600);
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelec
delayMicroseconds(500);
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
spi_transfer( databuffer[z][i][j] );
}
}
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect
delayMicroseconds(500);
}
delay(100);
}
But i cannot get my code to work.
That's a darn shame. But, I'm not even going to guess why. What does or does not work?
Have you tried something simple like lighting up the 2nd LED in the 4th column red?
That is, are you able to light a specific LED with a specific color?
yes. the 2nd and third bits of code i posted up there work perfectly. i just cant get the first to work, nor can i combine them to get one animation out of them
wel, all, i got the plasma problem fixed. thanks for trying, though, i guess, to the guy that tried.
now i just need to combine these 4 pieces of code into 1 so that the arduino will loop all 4 animations:
//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
void SetDisplayChainLength (unsigned char length)
{
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
SendOneChar('%');
SendOneChar(length);
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
}
void LED_Setup()
{
//SPI Bus setup
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
//Set the pin modes for the RGB matrix
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
//Make sure the RGB matrix is deactivated
digitalWrite(SLAVESELECT,HIGH);
// delay (5);
// SetDisplayChainLength (1);
// delay (5);
}
//
// tables for plasma
//
// 8 by 8 -> 64 bytes
// 16 by 16 -> 256 bytes
// color table 256 bytes
//
#define PLASMA_W 8
#define PLASMA_H 8
#define TABLE_W 16
#define TABLE_H 16
unsigned char gPlasma[PLASMA_W*PLASMA_H];
unsigned char gTable1[TABLE_W*TABLE_H];
unsigned char gTable2[TABLE_W*TABLE_H];
unsigned char gColorTable[256];
float gCircle1, gCircle2, gCircle3, gCircle4, gCircle5, gCircle6, gCircle7, gCircle8;
int gRoll;
void Plasma_CalcTable1 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
gTable1[index] = (unsigned char) ((sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4) *5 );
}
}
}
void Plasma_CalcTable2 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
float temp = sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4;
gTable2[index] = (sin(temp/9.5)+1)*90;
}
}
}
void Plasma_SetColor (int index, unsigned char red, unsigned char green, unsigned char blue)
{
unsigned char new_color = (red & 0xE0) + ((green & 0xE0) >> 3) + ((blue & 0xC0) >> 6);
gColorTable [index] = new_color;
}
double gRed, gGreen, gBlue;
#define color(u,a) (cos((u)+(a))+1)*127
void BuildColorTable()
{
double u;
int i;
for (i=0; i<256; i++)
{
u=2*PI/256*i;
Plasma_SetColor(i,color(u,gRed),color(u,gGreen),color(u,gBlue));
}
gRed+=0.05;
gGreen-=0.05;
gBlue+=0.1;
}
void Plasma_Setup ()
{
gCircle1 = 0;
gCircle2 = 0;
gCircle3 = 0;
gCircle4 = 0;
gCircle5 = 0;
gCircle6 = 0;
gCircle7 = 0;
gCircle8 = 0;
gRoll = 0;
for (int i=0; i<PLASMA_W*PLASMA_H; i++)
gPlasma[i] = 0;
Plasma_CalcTable1 ();
Plasma_CalcTable2 ();
gRed=1.0/6.0*PI;
gGreen=3.0/6.0*PI;
gBlue=5.0/6.0*PI;
BuildColorTable ();
}
void setup()
{
LED_Setup ();
Plasma_Setup ();
}
void ComputePlasma (int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int roll)
{
int i, j;
for (i=0; i< PLASMA_H; i++)
{
for (j=0; j< PLASMA_W; j++)
{
int index = (i*PLASMA_W)+j;
unsigned int new_height = gTable1[TABLE_W*(i+y1)+j+x1];
new_height = new_height + roll;
new_height = new_height + gTable2[TABLE_W*(i+y2)+j+x2];
new_height = new_height + gTable2[TABLE_W*(i+y3)+j+x3]; // + gTable2[TABLE_W*(i+y4)+j+x4];
new_height = new_height & 0xFF;
new_height = gColorTable[new_height];
gPlasma[index] = new_height;
}
}
}
char SendOneChar (volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}
void SendToLEDs ()
{
int i;
//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(i=0; i<PLASMA_W*PLASMA_H; i++)
{
SendOneChar((char)gPlasma[i]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
}
void SetColor (int index)
{
index = index % 256;
unsigned char new_color = gColorTable[index];
for(int i=0; i<PLASMA_H*PLASMA_W; i++)
{
gPlasma[i] = new_color;
}
}
void loop()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
float ratio = 2;
gCircle1 = gCircle1 + (ratio * 0.085/6);
gCircle2 = gCircle2 - (ratio * 0.1/6);
gCircle3 = gCircle3 + (ratio * 0.3/6);
gCircle4 = gCircle4 - (ratio * 0.2/6);
gCircle5 = gCircle5 + (ratio * 0.4/6);
gCircle6 = gCircle6 - (ratio * 0.15/6);
gCircle7 = gCircle7 + (ratio * 0.35/6);
gCircle8 = gCircle8 - (ratio * 0.05/6);
x2=(PLASMA_W/2)+(PLASMA_W/2)*sin(gCircle1);
y2=(PLASMA_H/2)+(PLASMA_H/2)*cos(gCircle2);
x1=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle3);
y1=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle4);
x3=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle5);
y3=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle6);
x4=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle7);
y4=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle8);
ComputePlasma(x1,y1,x2,y2,x3,y3,x4,y4,gRoll);
gRoll = gRoll + 1;
// SetColor (gRoll);
SendToLEDs();
delay (20);
}
// Blinking LEDs
// SPI register bit positions
#define RX_IN 0
#define TX_OUT 1
#define CS 2
#define MOSI 3
#define MISO 4
#define SCK 5
// Ardrino Duemilanove pin numbers for SPI
#define CHIPSELECT 10 // CS
#define DATAOUT 11 // MOSI
#define DATAIN 12 // MISO (not used)
#define SPICLOCK 13 // SCK
// SparkFun RGB LED backpack officially supported colors
typedef unsigned char ledcolorType;
#define colorBlack 0x00
#define colorRed 0xe0
#define colorGreen 0x1c
//#define colorBlue 0x03
#define colorOrange 0xfc
//#define colorMagneta 0xe3
//#define colorTeal 0x1f
#define colorWhite 0xff
#define colorLen 13
static const ledcolorType colors[colorLen] =
//{
//colorBlack, colorRed, colorGreen, //colorBlue,
//colorOrange, //colorMagneta, colorTeal,
//colorWhite
//};
{
32,40,64,72,0,96,128,160,192,224,228,92,200
};
// SparkFun RGB LED backpack frame geometry
#define rowLen 8
#define colLen 8
typedef struct
{
ledcolorType pixels[rowLen][colLen];
} frameType;
static void SetFrameToColor(frameType& frame, const ledcolorType color)
{
for (unsigned int col = 0; col < colLen; col++)
for (unsigned int row = 0; row < rowLen; row++)
frame.pixels[row][col] = color;
}
static void SetFrameToBlack(frameType& frame) {SetFrameToColor(frame, colorBlack);}
static unsigned char PickRandomColor(void) {return colors[random(colorLen)];}
static void ChangeOneRandomPixelToColor(frameType& frame, const ledcolorType color)
{
frame.pixels[random(rowLen)][random(colLen)] = color;
}
static void SetBackpackSelectStatus(const unsigned int status)
{
digitalWrite(CHIPSELECT, status); delayMicroseconds(500);
}
static void BackpackEnable(void) {SetBackpackSelectStatus(LOW);}
static void BackpackDisable(void) {SetBackpackSelectStatus(HIGH);}
static unsigned char WriteSPIByte(unsigned char data)
{
SPDR = data; while (!(SPSR & (1 << SPIF))); return SPDR;
}
static void SetBackpackDaisyChainLength(const unsigned int length)
{
BackpackEnable();
WriteSPIByte('%'); WriteSPIByte(length);
BackpackDisable();
}
static void WriteFrame(const frameType& frame)
{
BackpackEnable();
for (unsigned int row = 0; row < rowLen; row++)
for (unsigned int col = 0; col < colLen; col++)
WriteSPIByte(frame.pixels[row][col]);
BackpackDisable();
}
// The last real frame displayed
static frameType realframe;
// One time setup
void setup()
{
// Initialize the Arduino SPI pin directions
pinMode(CHIPSELECT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(SPICLOCK, OUTPUT);
// Magical initialization of the SPI registers
volatile byte clr;
DDRB = (1 << CS) | (1 << MOSI) | (1 << SCK);
DDRD = (1 << TX_OUT);
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
SPSR = SPSR & 0xfe;
clr = SPSR; clr = SPDR;
// Set the backpack to its default state
BackpackDisable();
SetBackpackDaisyChainLength(1); // Be careful with this one
// Initial display is all black pixels
SetFrameToBlack(realframe); WriteFrame(realframe);
}
// Main loop
void loop()
{
delay(1);
ChangeOneRandomPixelToColor(realframe, PickRandomColor());
WriteFrame(realframe);
}
here are the other 2 sketches i need to combine with the previous 2
#define CHIPSELECT 10//Chip Select Line
#define SPICLOCK 13//Master Clock
#define DATAOUT 11//DataOut to DataIn on Backpack
#define DATAIN 12//DataIn from DataOut on Backpack
//simple array for data frames - you can use 0-3 for the color settings on the RG display or 0-7 for the RGB Matrixs//
int databuffer[8][8][8] ={
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,32,32,32,0},
{0,0,0,0,0,0,0,3}
},
{
{0,0,0,0,0,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,32,32,32,32,0,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,0,0,0,0,3,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,3,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,0,3,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,3,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,0,32,32,32,32,0,0},
{0,0,3,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,32,228,32,0},
{0,32,0,32,0,232,32,0},
{0,32,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,3,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,228,32,0},
{0,0,32,0,0,232,32,0},
{0,0,0,32,0,232,32,0},
{0,0,0,32,228,232,32,0},
{0,0,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{3,0,0,0,0,0,0,0}
}
} ;
int z=0;
// SPI data load / transfer function - thanks Heather Dewey-Hagborg //
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
}
void clearDisplay() {
for (int i=0;i<8;i++) for (int j=0;j<8;j++)
{ spi_transfer( 0 ); }
delay(50);
}
void setup()
{
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device
SPCR = B01010001; //SPI Registers
SPSR = SPSR & B11111110; //make sure the speed is 125KHz
clr=SPSR;
clr=SPDR;
delay(10);
clearDisplay();
}
void loop()
{
int z=0;
//loop though frames #8 //
for (int z=0;z<8;z++) {
delay(600);
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelec
delayMicroseconds(500);
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
spi_transfer( databuffer[z][i][j] );
}
}
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect
delayMicroseconds(500);
}
delay(100);
}
// Simple program to test using the Arduino with the RGB Matrix
// & Backpack from Sparkfun. Code is a combination of Heather Dewey-Hagborg,
// Arduino Forum user: Little-Scale, and // Daniel Hirschmann. Enjoy!
//
// The Backpack requires 125Khz SPI, which is the slowest rate
// at which the Arduino's hardware SPI bus can communicate at.
//
// We need to send SPI to the backpack in the following steps:
// 1) Activate ChipSelect;
// 2) Wait 500microseconds;
// 3) Transfer 64bytes @ 125KHz (1 byte for each RGB LED in the matrix);
// 4) De-activate ChipSelect;
// 5) Wait 500microseconds
// Repeat however often you like!
#define CHIPSELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define RED 0xE0 // For code readability.
#define GREEN 0x1C // Only 8 colors available
#define BLUE 0x07 // but variation possible with
#define ORANGE 0xFC // brightness (see Sparkfun doc).
#define MAGENTA 0xE3
#define TEAL 0x1F
#define WHITE 0xFF
#define BLACK 0x00
byte color_tst_tbl[] = {32,64,96,1,7,228,52,40}; // Table of color codes, one color duplicated to get 8 entries for test purpose.
byte pattern[] = { // Test pattern to be displayed on the matrix, entries in binary for learning purpose.
B11111111, // So you can see which led is on (1) or off (0=BLACK) on your pattern.
B00000000, // Just change it to test your own patterns.
B11111111, // We can also use hex notation: B11111111 is 0xFF in hexa
B00000000,
B11111111,
B00000000,
B11111111,
B00000000
};
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
}
void setup()
{
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device
SPCR = B01010001; //SPI Registers
SPSR = SPSR & B11111110; //make sure the speed is 125KHz
/*
SPCR bits:
7: SPIEE - enables SPI interrupt when high
6: SPE - enable SPI bus when high
5: DORD - LSB first when high, MSB first when low
4: MSTR - arduino is in master mode when high, slave when low
3: CPOL - data clock idle when high if 1, idle when low if 0
2: CPHA - data on falling edge of clock when high, rising edge when low
1: SPR1 - set speed of SPI bus
0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
*/
clr=SPSR;
clr=SPDR;
delay(10);
}
void loop()
{
delay(150);
int idx = 0;
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
delayMicroseconds(500);
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
if (bitRead(pattern[i],j) == 0) //bitRead funcion is used to get each 8 bit of the 8 bytes pattern table (64 bits in total).
{ spi_transfer(BLACK);} // bit is 0 -> Transfer Black color.
else
{ spi_transfer(color_tst_tbl[j]);} // bit is 1 -> Transfer a color choosen in the color test table.
idx++;
}
} // 64 bytes should have been transfered to the RGB matrix.
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
// What you've send is displayed on the matrix.
delayMicroseconds(500);
delay(150);
idx = 0;
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
delayMicroseconds(500);
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
if (bitRead(pattern[i],j) != 0) //bitRead funcion is used to get each 8 bit of the 8 bytes pattern table (64 bits in total).
{ spi_transfer(BLACK);} // bit is 0 -> Transfer Black color.
else
{ spi_transfer(color_tst_tbl[j]);} // bit is 1 -> Transfer a color choosen in the color test table.
idx++;
}
} // 64 bytes should have been transfered to the RGB matrix.
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
// What you've send is displayed on the matrix.
delayMicroseconds(500);
}
If you look at the 4 sketches, you'll see that there is a lot of common code, outside of setup() and loop(). Copy all the common code into a new sketch once. Copy any unique code, too.
Each sketch has a setup function. Copy all common code once, and all unique code, to the new sketch's setup function.
Copy any common function, except loop() from the 4 sketches once to the new sketch, and copy all unique functions to the new sketch.
Each sketch has a loop function that manipulates the LEDs in a unique way. Copy each loop() function to the new sketch, renaming it in the process (loop1(), loop2(), etc., or more meaningful names).
Now, create a new loop function in the new sketch. Make it call the renamed loop functions from the 4 sketches in whatever fashion seems reasonable - one after the other, in random order, in response to a button push, etc.
It's not rocket science. That's easier.
hi all.
i got 4 of the 5 animations combined with this code: it works, try it and compile it.
#define RX_IN 0
#define TX_OUT 1
#define CS 2
#define MOSI 3
#define MISO 4
#define SCK 5
#define CHIPSELECT 10//Chip Select Line
#define SPICLOCK 13//Master Clock
#define DATAOUT 11//DataOut to DataIn on Backpack
#define DATAIN 12//DataIn from DataOut on Backpack
typedef unsigned char ledcolorType;
#define colorBlack 0x00
#define colorLen 13
static const ledcolorType colors[colorLen] =
{
32,40,64,72,0,96,128,160,192,224,228,92,200
};
// SparkFun RGB LED backpack frame geometry
#define rowLen 8
#define colLen 8
typedef struct
{
ledcolorType pixels[rowLen][colLen];
} frameType;
static void SetFrameToColor(frameType& frame, const ledcolorType color)
{
for (unsigned int col = 0; col < colLen; col++)
for (unsigned int row = 0; row < rowLen; row++)
frame.pixels[row][col] = color;
}
static void SetFrameToBlack(frameType& frame) {SetFrameToColor(frame, colorBlack);}
static unsigned char PickRandomColor(void) {return colors[random(colorLen)];}
static void ChangeOneRandomPixelToColor(frameType& frame, const ledcolorType color)
{
frame.pixels[random(rowLen)][random(colLen)] = color;
}
static void SetBackpackSelectStatus(const unsigned int status)
{
digitalWrite(CHIPSELECT, status); delayMicroseconds(500);
}
static void BackpackEnable(void) {SetBackpackSelectStatus(LOW);}
static void BackpackDisable(void) {SetBackpackSelectStatus(HIGH);}
static unsigned char WriteSPIByte(unsigned char data)
{
SPDR = data; while (!(SPSR & (1 << SPIF))); return SPDR;
}
static void SetBackpackDaisyChainLength(const unsigned int length)
{
BackpackEnable();
WriteSPIByte('%'); WriteSPIByte(length);
BackpackDisable();
}
static void WriteFrame(const frameType& frame)
{
BackpackEnable();
for (unsigned int row = 0; row < rowLen; row++)
for (unsigned int col = 0; col < colLen; col++)
WriteSPIByte(frame.pixels[row][col]);
BackpackDisable();
}
// The last real frame displayed
static frameType realframe;
//simple array for data frames - you can use 0-3 for the color settings on the RG display or 0-7 for the RGB Matrixs//
int databuffer0[2][8][8] ={
{
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40}
},
{
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0},
{32,64,96,1,7,228,52,40},
{0,0,0,0,0,0,0,0}
}
} ;
int databuffer1[8][8][8] ={
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,32,32,32,0},
{0,0,0,0,0,0,0,3}
},
{
{0,0,0,0,0,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,32,32,32,32,0,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,0,0,0,0,3,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,3,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,32,32,32,32,32,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,232,32,0,0,0},
{0,0,0,0,3,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,0,32,32,32,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,0,32,32,228,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,0,0,232,32,0},
{0,0,0,3,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,32,32,32,228,232,32,0},
{0,0,32,32,32,32,0,0},
{0,0,3,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,32,32,0},
{0,32,0,0,0,232,32,0},
{0,32,0,0,32,228,32,0},
{0,32,0,32,0,232,32,0},
{0,32,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{0,3,0,0,0,0,0,0}
},
{
{0,0,0,0,0,0,0,0},
{0,32,0,0,0,228,32,0},
{0,0,32,0,0,232,32,0},
{0,0,0,32,0,232,32,0},
{0,0,0,32,228,232,32,0},
{0,0,32,0,0,232,32,0},
{0,32,0,0,0,232,32,0},
{3,0,0,0,0,0,0,0}
}
} ;
int databuffer2[1][8][8] ={
{
{0,0,0,32,32,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,32,32,0,0,0},
{0,0,0,32,32,0,0,0}
}
} ;
int z=0;
// SPI data load / transfer function - thanks Heather Dewey-Hagborg //
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
}
void clearDisplay() {
for (int i=0;i<8;i++) for (int j=0;j<8;j++)
{ spi_transfer( 0 ); }
delay(50);
}
void setup()
{
byte clr;
pinMode(DATAIN,INPUT);
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device
/*
SPCR bits:
7: SPIEE - enables SPI interrupt when high
6: SPE - enable SPI bus when high
5: DORD - LSB first when high, MSB first when low
4: MSTR - arduino is in master mode when high, slave when low
3: CPOL - data clock idle when high if 1, idle when low if 0
2: CPHA - data on falling edge of clock when high, rising edge when low
1: SPR1 - set speed of SPI bus
0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
*/
DDRB = (1 << CS) | (1 << MOSI) | (1 << SCK);
DDRD = (1 << TX_OUT);
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
SPCR = B01010001; //SPI Registers
SPSR = SPSR & B11111110; //make sure the speed is 125KHz
clr=SPSR;
clr=SPDR;
SetBackpackDaisyChainLength(1);
delay(10);
SetFrameToBlack(realframe); WriteFrame(realframe);
clearDisplay();
}
void daft()
{
int z=0;
//loop though frames #8 //
for (int z=0;z<8;z++) {
delay(600);
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect
delayMicroseconds(500);
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
spi_transfer( databuffer1[z][i][j] );
}
}
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect
delayMicroseconds(500);
}
delay(100);
}
void rows()
{
int z=0;
//loop though frames #2 //
for (int z=0;z<2;z++) {
delay(100);
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect
delayMicroseconds(500);
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
spi_transfer( databuffer0[z][i][j] );
}
}
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect
delayMicroseconds(500);
}
delay(100);
}
void exclamation()
{
int z=0;
//loop though frames #2 //
for (int z=0;z<1;z++) {
delay(1000);
digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect
delayMicroseconds(500);
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
spi_transfer( databuffer2[z][i][j] );
}
}
digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect
delayMicroseconds(500);
}
delay(1000);
}
void randomise()
{
delay(1);
ChangeOneRandomPixelToColor(realframe, PickRandomColor());
WriteFrame(realframe);
}
void loop()
{
int a=0;
for (int a=0;a<3;a++) {
daft();
}
a=0;
for (int a=0; a<32; a++){
rows();
}
a=0;
for (int a=0; a<5; a++){
exclamation();
}
a=0;
for (int a=0; a<5000; a++){
randomise();
}
}
however, i tried adding my plasma code to it and it doesn't work. i have no idea why, and the code is too big to post on here. can someone try combining the plasma code with the above code in my post to get 5 animations? please and thank you
plasma code again, for reference:
//Define the SPI Pin Numbers
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define CHIPSELECT 10//ss
void SetDisplayChainLength (unsigned char length)
{
//Activate the RGB Matrix
digitalWrite(CHIPSELECT, LOW);
//Send the color buffer to the RGB Matrix
SendOneChar('%');
SendOneChar(length);
//Deactivate the RGB matrix.
digitalWrite(CHIPSELECT, HIGH);
}
void LED_Setup()
{
//SPI Bus setup
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
//Set the pin modes for the RGB matrix
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
//Make sure the RGB matrix is deactivated
digitalWrite(CHIPSELECT,HIGH);
// delay (5);
// SetDisplayChainLength (1);
// delay (5);
}
//
// tables for plasma
//
// 8 by 8 -> 64 bytes
// 16 by 16 -> 256 bytes
// color table 256 bytes
//
#define PLASMA_W 8
#define PLASMA_H 8
#define TABLE_W 16
#define TABLE_H 16
unsigned char gPlasma[PLASMA_W*PLASMA_H];
unsigned char gTable1[TABLE_W*TABLE_H];
unsigned char gTable2[TABLE_W*TABLE_H];
unsigned char gColorTable[256];
float gCircle1, gCircle2, gCircle3, gCircle4, gCircle5, gCircle6, gCircle7, gCircle8;
int gRoll;
void Plasma_CalcTable1 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
gTable1[index] = (unsigned char) ((sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4) *5 );
}
}
}
void Plasma_CalcTable2 ()
{
for (int i=0; i< TABLE_H; i++)
{
for (int j=0; j< TABLE_W; j++)
{
int index = (i*TABLE_W)+j;
float temp = sqrt(16.0+(PLASMA_H-i)*(PLASMA_H-i)+(PLASMA_W-j)*(PLASMA_W-j))-4;
gTable2[index] = (sin(temp/9.5)+1)*90;
}
}
}
void Plasma_SetColor (int index, unsigned char red, unsigned char green, unsigned char blue)
{
unsigned char new_color = (red & 0xE0) + ((green & 0xE0) >> 3) + ((blue & 0xC0) >> 6);
gColorTable [index] = new_color;
}
double gRed, gGreen, gBlue;
#define color(u,a) (cos((u)+(a))+1)*127
void BuildColorTable()
{
double u;
int i;
for (i=0; i<256; i++)
{
u=2*PI/256*i;
Plasma_SetColor(i,color(u,gRed),color(u,gGreen),color(u,gBlue));
}
gRed+=0.05;
gGreen-=0.05;
gBlue+=0.1;
}
void Plasma_Setup ()
{
gCircle1 = 0;
gCircle2 = 0;
gCircle3 = 0;
gCircle4 = 0;
gCircle5 = 0;
gCircle6 = 0;
gCircle7 = 0;
gCircle8 = 0;
gRoll = 0;
for (int i=0; i<PLASMA_W*PLASMA_H; i++)
gPlasma[i] = 0;
Plasma_CalcTable1 ();
Plasma_CalcTable2 ();
gRed=1.0/6.0*PI;
gGreen=3.0/6.0*PI;
gBlue=5.0/6.0*PI;
BuildColorTable ();
}
void setup()
{
LED_Setup ();
Plasma_Setup ();
}
void ComputePlasma (int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int roll)
{
int i, j;
for (i=0; i< PLASMA_H; i++)
{
for (j=0; j< PLASMA_W; j++)
{
int index = (i*PLASMA_W)+j;
unsigned int new_height = gTable1[TABLE_W*(i+y1)+j+x1];
new_height = new_height + roll;
new_height = new_height + gTable2[TABLE_W*(i+y2)+j+x2];
new_height = new_height + gTable2[TABLE_W*(i+y3)+j+x3]; // + gTable2[TABLE_W*(i+y4)+j+x4];
new_height = new_height & 0xFF;
new_height = gColorTable[new_height];
gPlasma[index] = new_height;
}
}
}
char SendOneChar (volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}
void SendToLEDs ()
{
int i;
//Activate the RGB Matrix
digitalWrite(CHIPSELECT, LOW);
//Send the color buffer to the RGB Matrix
for(i=0; i<PLASMA_W*PLASMA_H; i++)
{
SendOneChar((char)gPlasma[i]);
}
//Deactivate the RGB matrix.
digitalWrite(CHIPSELECT, HIGH);
}
void SetColor (int index)
{
index = index % 256;
unsigned char new_color = gColorTable[index];
for(int i=0; i<PLASMA_H*PLASMA_W; i++)
{
gPlasma[i] = new_color;
}
}
void loop()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
float ratio = 2;
gCircle1 = gCircle1 + (ratio * 0.085/6);
gCircle2 = gCircle2 - (ratio * 0.1/6);
gCircle3 = gCircle3 + (ratio * 0.3/6);
gCircle4 = gCircle4 - (ratio * 0.2/6);
gCircle5 = gCircle5 + (ratio * 0.4/6);
gCircle6 = gCircle6 - (ratio * 0.15/6);
gCircle7 = gCircle7 + (ratio * 0.35/6);
gCircle8 = gCircle8 - (ratio * 0.05/6);
x2=(PLASMA_W/2)+(PLASMA_W/2)*sin(gCircle1);
y2=(PLASMA_H/2)+(PLASMA_H/2)*cos(gCircle2);
x1=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle3);
y1=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle4);
x3=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle5);
y3=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle6);
x4=(PLASMA_W/2)+(PLASMA_W/2)*cos(gCircle7);
y4=(PLASMA_H/2)+(PLASMA_H/2)*sin(gCircle8);
ComputePlasma(x1,y1,x2,y2,x3,y3,x4,y4,gRoll);
gRoll = gRoll + 1;
// SetColor (gRoll);
SendToLEDs();
delay (20);
}
however, i tried adding my plasma code to it and it doesn't work. i have no idea why
That does not tell us a whole lot. What failed to work? What were the symptoms? How big was the completed sketch?
If it's too big to post here, you could be running out of memory.
the completed sketch is around 8k bytes. i added the plasma code in the way i added in the other codes together into 1 sketch, getting rid of common code and copying in unique code, then creating a sep loop function from the original plasma loop that is called in the main loop. i think it might be a memory problem. whenever i run it, it just freezes and nothing shows up on the display.
the plasma code works on its own, try it and compile it to see.
The plasma code has 3 256 byte arrays and a 64 byte array. That's 832 bytes just for the 4 arrays. Look at the hardware page, and see how much of each of the 3 kinds of memory you have on your Arduino. Then, do some reading about how the various kinds of memory are used. I think you'll find that you do not have enough of one kind of memory.
You might be able to move some data from one kind of memory to another.