LED Matrix / Max7221 code problems

So, I’m having a lot of trouble handling arrays in my code. Basically, I’m using a set of arrays in my code to handle the grid of LEDs I have setup (controlled by the arduino and 2 Max7721 chips) To make a long explanation short, the code seems to ‘forget’ the data I have set up in the array. In addition, the program is really having trouble with arrays defined in the void setup() section.

To give an example, I set up two, 6x6 boolean arrays that maintain the status’ of all my LEDs. When I initialize the arrays, all the LED’s light up and stay lit (this is good). When I change the value of a single address in an array, however (ie: arrayOne[2][3] = !arrayOne[2][3]) that individual LED changes status (also good), but all the other LEDs on the entire cube turn off (not so good).

If anyone has any advice, please let me know. I’m presenting this work in a Robotics Festival tomorrow.
Thanks

Here is the code I’m working with (everything compiles correctly)

int dataIn = 4;
int load = 3;
int clock = 2;

int maxInUse = 2; //# of max7221’s

// define max7221 registers
byte max7221_reg_noop = 0x00;
byte max7221_reg_digit0 = 0x01;
byte max7221_reg_digit1 = 0x02;
byte max7221_reg_digit2 = 0x03;
byte max7221_reg_digit3 = 0x04;
byte max7221_reg_digit4 = 0x05;
byte max7221_reg_digit5 = 0x06;
byte max7221_reg_digit6 = 0x07;
byte max7221_reg_digit7 = 0x08;
byte max7221_reg_decodeMode = 0x09;
byte max7221_reg_intensity = 0x0a;
byte max7221_reg_scanLimit = 0x0b;
byte max7221_reg_shutdown = 0x0c;
byte max7221_reg_displayTest = 0x0f;
boolean ba1[6][6]; //
boolean ba2[6][6]; //boolean array of LEDs

int max_reg[2][6]; // holds the value of each row

void putByte(byte data) {
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite( clock, LOW); // tick
if (data & mask){ // choose bit
digitalWrite(dataIn, HIGH);// send 1
}else{
digitalWrite(dataIn, LOW); // send 0
}
digitalWrite(clock, HIGH); // tock
–i; // move to lesser bit
}
}

void maxAll (byte reg, byte col) { // initialize all MAX7219’s in the system
int c = 0;
digitalWrite(load, LOW); // begin for ( c =1; c<= maxInUse; c++) {
putByte(reg); // specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
}
digitalWrite(load, LOW);
digitalWrite(load,HIGH);
}

void maxOne(byte maxNr, byte reg, byte col) { //maxOne is for adressing different MAX7221’s,
//whilele having a couple of them cascaded

int c = 0;
digitalWrite(load, LOW); // begin
for ( c = maxInUse; c > maxNr; c–) {
putByte(0); // means no operation
putByte(0); // means no operation
}

putByte(reg); // specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data

for ( c = maxNr-1; c >= 1; c–) {
putByte(0); // means no operation
putByte(0); // means no operation
}

digitalWrite(load, LOW);
digitalWrite(load,HIGH);
}

void maxClear(){
for (int e=1; e<=8; e++) {
maxAll(e,0);
}
}

void cubeWrite(boolean ba1[6][6], boolean ba2[6][6]){

//These arrays take the boolean value and multiply it by the address of the LED in the MAX chip (the result is either the number itself, or 0)

int va1[6][6] = {
{ba1[0][0]*4, ba1[0][1]*8, ba1[0][2]*16, ba1[0][3]*32, ba1[0][4]*64, ba1[0][5]*128},
{ba1[1][0]*4, ba1[1][1]*8, ba1[1][2]*16, ba1[1][3]*32, ba1[1][4]*64, ba1[1][5]*128},
{ba1[2][0]*4, ba1[2][1]*8, ba1[2][2]*16, ba1[2][3]*32, ba1[2][4]*64, ba1[2][5]*128},
{ba1[3][0]*4, ba1[3][1]*8, ba1[3][2]*16, ba1[3][3]*32, ba1[3][4]*64, ba1[3][5]*128},
{ba1[4][0]*4, ba1[4][1]*8, ba1[4][2]*16, ba1[4][3]*32, ba1[4][4]*64, ba1[4][5]*128},
{ba1[5][0]*4, ba1[5][1]*8, ba1[5][2]*16, ba1[5][3]*32, ba1[5][4]*64, ba1[5][5]*128}
};

int va2[6][6] = {
{ba2[0][0]*4, ba2[0][1]*8, ba2[0][2]*16, ba2[0][3]*32, ba2[0][4]*64, ba2[0][5]*128},
{ba2[1][0]*4, ba2[1][1]*8, ba2[1][2]*16, ba2[1][3]*32, ba2[1][4]*64, ba2[1][5]*128},
{ba2[2][0]*4, ba2[2][1]*8, ba2[2][2]*16, ba2[2][3]*32, ba2[2][4]*64, ba2[2][5]*128},
{ba2[3][0]*4, ba2[3][1]*8, ba2[3][2]*16, ba2[3][3]*32, ba2[3][4]*64, ba2[3][5]*128},
{ba2[4][0]*4, ba2[4][1]*8, ba2[4][2]*16, ba2[4][3]*32, ba2[4][4]*64, ba2[4][5]*128},
{ba2[5][0]*4, ba2[5][1]*8, ba2[5][2]*16, ba2[5][3]*32, ba2[5][4]*64, ba2[5][5]*128}
};

//The lines below add up the values of each row of the Max chip

max_reg[0][0] = va1[0][0] + va1[0][1] + va1[0][2] + va1[0][3] + va1[0][4] + va1[0][5];
max_reg[0][1] = va1[1][0] + va1[1][1] + va1[1][2] + va1[1][3] + va1[1][4] + va1[1][5];
max_reg[0][2] = va1[2][0] + va1[2][1] + va1[2][2] + va1[2][3] + va1[2][4] + va1[2][5];
max_reg[0][3] = va1[3][0] + va1[3][1] + va1[3][2] + va1[3][3] + va1[3][4] + va1[3][5];
max_reg[0][4] = va1[4][0] + va1[4][1] + va1[4][2] + va1[4][3] + va1[4][4] + va1[4][5];
max_reg[0][5] = va1[5][0] + va1[5][1] + va1[5][2] + va1[5][3] + va1[5][4] + va1[5][5];

max_reg[1][0] = va2[0][0] + va2[0][1] + va2[0][2] + va2[0][3] + va2[0][4] + va2[0][5];
max_reg[1][1] = va2[1][0] + va2[1][1] + va2[1][2] + va2[1][3] + va2[1][4] + va2[1][5];
max_reg[1][2] = va2[2][0] + va2[2][1] + va2[2][2] + va2[2][3] + va2[2][4] + va2[2][5];
max_reg[1][3] = va2[3][0] + va2[3][1] + va2[3][2] + va2[3][3] + va2[3][4] + va2[3][5];
max_reg[1][4] = va2[4][0] + va2[4][1] + va2[4][2] + va2[4][3] + va2[4][4] + va2[4][5];
max_reg[1][5] = va2[5][0] + va2[5][1] + va2[5][2] + va2[5][3] + va2[5][4] + va2[5][5];

maxOne(1, 1, max_reg[0][0]);
maxOne(1, 2, max_reg[0][1]);
maxOne(1, 3, max_reg[0][2]);
maxOne(1, 4, max_reg[0][3]); //These functions actually write to the MAX
maxOne(1, 5, max_reg[0][4]);
maxOne(1, 6, max_reg[0][5]);

maxOne(2, 1, max_reg[1][0]);
maxOne(2, 2, max_reg[1][1]);
maxOne(2, 3, max_reg[1][2]);
maxOne(2, 4, max_reg[1][3]);
maxOne(2, 5, max_reg[1][4]);
maxOne(2, 6, max_reg[1][5]);
}

void setup()
{
Serial.begin(9600);

pinMode(groundPin, OUTPUT);
pinMode(powerPin, OUTPUT);
digitalWrite(groundPin, LOW);
digitalWrite(powerPin, HIGH);

pinMode(dataIn, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);

digitalWrite(13, HIGH);
maxAll(max7221_reg_scanLimit, 0x07); maxAll(max7221_reg_decodeMode, 0x00); // using an led matrix (not digits)
maxAll(max7221_reg_shutdown, 0x01); // not in shutdown mode
maxAll(max7221_reg_displayTest, 0x00); // no display test
maxClear(); // empty registers, turn all LEDs off
maxAll(max7221_reg_intensity, 0x08 & 0x0f); // the first 0x0f is the value you can set
// range: 0x00 to 0x0f

}

void loop(){

//the arrays below are the initial state, or ‘All Lights On’. For some reason, if I define this in or before void setup(), the program requires that I initialize the array again in the main loop.

boolean ba1[6][6] = {
{true,true,true,true,true,true}, // - - - - - -
{true,true,true,true,true,true}, // - 1 - - 5 -
{true,true,true,true,true,true}, // - - - - - -
{false,false,false,true,true,true}, // - - - - - -
{false,false,false,true,true,true}, // - N/A - - 4 -
{false,false,false,true,true,true} // - - - - - -
};

boolean ba2[6][6] = {
{true,true,true,true,true,true}, // - - - - - -
{true,true,true,true,true,true}, // - 2 - - 6 -
{true,true,true,true,true,true}, // - - - - - -
{false,false,false,true,true,true}, // - - - - - -
{false,false,false,true,true,true}, // - N/A - - 3 -
{false,false,false,true,true,true} // - - - - - -
};

).
if (side == 1){
ba1[0][0] = !ba1[0][0]; //This functions as it should. The LED at this value turns from on to off or from off to on, but all the rest of the LED’s turn off and stay off.

cubeWrite(ba1, ba2);
}
}

Had a look through your code and nothing is immediately obvious, can I suggest you add some debug output to the cubeWrite function, to print out the integer values for each row.

I tried a debug and it worked great! haha. Problem solved. I just didn't expect it to be that easy.