Sorry for the cryptic title but the dang thing isn't long enough to really state my problem.
I've been try for the past two days to write a pair of sketches that will let me control 6 RGB led matrix's based on the Rainbowduino from Seeed Studio, but right now I'm just trying to get one to play nice.
I've had to do a number of things to the base code in order to be able to send 97 bytes of data (command byte followed by 96 bytes of RGB pixel/led data) to a rainbow. For starters, the Wire Library does not support more than 32 bytes transmitted in a beginTransmission/send/endTransmission session. To fix that problem, I've set up a special send function on the master arduino and tinkered with the onRecieve on the slave rainbow so that the data can be send in multiple sessions and recomposed on the other side.
The problem is I appear to be getting garbled data introduced in to the command array (RainbowCMD[97]) that would normally point to running out of SRAM but this isn't the case. I've set up a stripped down version of my code that Serial.prints the RainbowCMD array before and after the SendCMD function and the data comes out just fine. During the send the first two sessions of 32 bytes are fine, but the third block A) isn't long enough and B) is full or garbage that isn't what it's supposed to be. The final session, which is only a byte, isn't right either.
Here is the Master side code:
#include <Wire.h>
#define waitingcmd 0x00
#define morecmd 0x01
#define processing 0x02
#define checking 0x03#define transcmd 0x10
#define checkslavestate 0x11
#define slavedone 0x12unsigned char RainbowCMD[97]={
'F',
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char State= transcmd;
unsigned char slavestate= waitingcmd;
unsigned long timeout;void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
delay(2000);
}void loop()
{
int i;
for (i=0;i<97;i++){
Serial.print(RainbowCMD*,HEX);*
}*
Serial.println();*
SendCMD(4);*
delay(1000);*
for (i=0;i<97;i++){*
_ Serial.print(RainbowCMD*,HEX);_
_ }_
_ Serial.println();_
_ delay(1000);_
_}_
_//--------------------------------------------------------------------------_
_//Name:SendCMD*_
//function: Send a 5 or 97 byte Rainbow command/frame out
*//parameter: address *
//----------------------------------------------------------------------------
void SendCMD(int Add)
*{ *
* unsigned char OK=0;*
* unsigned char i;*
* char transnumber;*
* unsigned char startnumber=0;*
* unsigned char cmdsession=0;*
* while(!OK) //cycle until slave transmits it's done*
* { *
* switch (State)*
* { *
* case transcmd: //transmit up to 32 bytes*
* switch (RainbowCMD[0]){ //if the data is a command*
* case 'R':*
* transnumber =5; //only transmit the 5 bytes*
* break;*
* case 'F': //if it's a frame*
_ transnumber=sizeof(RainbowCMD)-(32cmdsession); //find how many untransmitted bytes there are_
_ transnumber= min(32,transnumber); //make sure it's less than 32*_
* Serial.println();*
* Serial.print(startnumber,DEC);*
* Serial.print(' ');*
* Serial.print(transnumber,DEC);*
* Serial.print(' ');*
* Serial.print(cmdsession,DEC);*
* Serial.print(' ');*
* delay(1000);*
* break;*
* }*
* if (transnumber>0){ //if ther are bytes to send*
* Wire.beginTransmission(Add); //start*
* for (i=startnumber;i<(startnumber+transnumber);i++){*
_ Wire.send(RainbowCMD[(i+(32cmdsession))]);
Serial.print(RainbowCMD[(i+(32cmdsession))],HEX);_
* }*
* Wire.endTransmission();*
* delay(1000);*
* }*
* State=checkslavestate;*
* break;*
* case checkslavestate:*
* Wire.requestFrom(Add,1); *
* if (Wire.available()>0)*
* slavestate=Wire.receive(); *
* else {*
* slavestate =0xFF;*
* timeout++;*
* }*
* switch (slavestate){*
* case morecmd:*
* cmdsession++;*
* startnumber= startnumber+32;*
* State= transcmd;*
* break;*
* case processing:*
* State=slavedone;*
* break;*
* case checking:*
* State=slavedone;*
* break;*
* default:*
* State=transcmd;*
* break;*
* }*
* if (timeout>5000) //time out occurs*
* {*
* timeout=0; //reset timout*
* State=transcmd; //trans failure, resend*
* }*
* delay(5);*
* break;*
* case slavedone: //trans done*
* OK=1; //trans confirmed and OK, will exit while loop*
* State=transcmd; //reset state for next call of cmd*
* break;*
* default:*
* State=transcmd;*
* break;*
* }*
* }*
}
[/quote]