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();
}
}