# Arduino LED matrix and 2D array, data seems to be corrupting

I'm trying to drive an 8x8 LED matrix with an arduino nano, but I can't get the LEDs to display what I tell it to.

I'm using this LED Matrix

Here's my code:

int row1 = 2;
int row2 = 3;
int row3 = 4;
int row4 = 5;
int row5 = 6;
int row6 = 7;
int row7 = 8;
int row8 = 9;

int col1 = 10;
int col2 = 11;
int col3 = 12;
int col4 = 18;
int col5 = 14;
int col6 = 15;
int col7 = 16;
int col8 = 17;

void setup() {
int i;
for(i = 2; i <= 18; i++) {
pinMode(i,OUTPUT);
}
for(i = 2; i <= 9; i++) {
digitalWrite(i, HIGH);
}
}

void loop() {

int display [8][8] = {
{1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,1}
};

}

Serial.begin(9600);
int i;
for (i = row1; i <= row8; i++) {
Serial.println(i-1);
digitalWrite(i, LOW);
digitalWrite(col8, screen[i-2][0]);
Serial.print(screen[i-2][0]); Serial.print("|");
digitalWrite(col7, screen[i-2][1]);
Serial.print(screen[i-2][1]); Serial.print("|");
digitalWrite(col6, screen[i-2][2]);
Serial.print(screen[i-2][2]); Serial.print("|");
digitalWrite(col5, screen[i-2][3]);
Serial.print(screen[i-2][3]); Serial.print("|");
digitalWrite(col4, screen[i-2][4]);
Serial.print(screen[i-2][4]); Serial.print("|");
digitalWrite(col3, screen[i-2][5]);
Serial.print(screen[i-2][5]); Serial.print("|");
digitalWrite(col2, screen[i-2][6]);
Serial.print(screen[i-2][6]); Serial.print("|");
digitalWrite(col1, screen[i-2][7]);
Serial.println(screen[i-2][7]);
delay(1);
blank();
digitalWrite(i, HIGH);
}

}

void blank() {
digitalWrite(col1, LOW);
digitalWrite(col2, LOW);
digitalWrite(col3, LOW);
digitalWrite(col4, LOW);
digitalWrite(col5, LOW);
digitalWrite(col6, LOW);
digitalWrite(col7, LOW);
digitalWrite(col8, LOW);
}


And here is the output:

1
1|0|0|0|0|0|0|0
2
0|1|0|0|0|0|0|0
3
0|0|1|0|0|0|0|0
4
0|0|0|1|0|0|0|0
5
0|0|0|0|1|0|0¾j
6
0|0|0|0|0|1|0|0
7
0|0|0|0|0|0|1|0
8
0|0|0|0|0|0|0|1


So I can tell that the error (probably) isn't my wiring, because those array values are clearly wrong, I have no idea where the ¾j is coming from.

• All your array indexing looks wrong, the array display will have indexes going from 0-7 and for example you have row8 declared as 9. – PeterJ Mar 8 '14 at 3:12
• That should be fine, as the program doesn't use the index to get the position. – Owen Tourlamain Mar 8 '14 at 3:16
• There was a bug with the indexing, the [i-1]s should have been [i-2]s. that has eliminated the last line. Here's the new output: 1 1|0|0|0|0|0|0|0 2 0|1|0|0|0|0|0|0 3 0|0|1|0|0|0|0|0 4 0|0|0|1|0|0|0|0 5 0|0|0|0|1|0|0¾j 6 0|0|0|0|0|1|0|0 7 0|0|0|0|0|0|1|0 8 0|0|0|0|0|0|0|1 – Owen Tourlamain Mar 8 '14 at 3:21
• There was also a wiring mistake, which is fixed now. However now I can get some patterns to display fine, but others wont. If I use the array full of 1's, I get the whole matrix lit up, but if I use the one that alternates 1's and 0's columns 1 and 7 wont light. EDIT: And row 8 is showing 7's data, but they aren't switched when using other paterns – Owen Tourlamain Mar 8 '14 at 3:35
• One thing that might be worth a try is a small delay after digitalWrite(i, LOW) - I assume that's a clock or latch of some sort. But it's probably worth editing the question to include some info on the LCD you're using and the type of Arduino, how it's connected etc. – PeterJ Mar 8 '14 at 3:40

The problem is you're reading outside the bounds of screen. Screen has valid addresses of 0-7, and you're trying to read from screen[8][n].

8
887|22021|-15872|-30720|124|2|9|10


^ How can you have line "8"? The line number is Serial.println(i-1);, and the value is Serial.print(screen[i-1][0]);, so the error will be present in the array access as well. To fix it, you just need to change to screen[i-2].

Ok, the other corruption (6 0|0|0|0|0|0|Á) issue is because you're heavily oversaturating the serial port. I stuck a 1 second delay in after each loop, and it fixed that issue.

Change:

void loop() {

static int display [8][8] = {
{1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,1}
};

delay(1000);          //  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}


or

void updateScreen(int screen[8][8]) {

Serial.begin(115200);
int i;
<snip>


Output:

0
1|0|0|0|0|0|0|0
1
0|1|0|0|0|0|0|0
2
0|0|1|0|0|0|0|0
3
0|0|0|1|0|0|0|0
4
0|0|0|0|1|0|0|0
5
0|0|0|0|0|1|0|0
6
0|0|0|0|0|0|1|0
7
0|0|0|0|0|0|0|1


I also tried simply increasing the baud-rate (to 115200 baud), and that also fixed the issue without the delay, so either option would work.

• Static was no help unfortunately. I was originally declaring it outside all functions, but the compiler didn't like it for some reason – Owen Tourlamain Mar 8 '14 at 4:16
• @user168057 - I was wrong, hang on, tinkering with stuff now. I just need to remember how array pointers work. – Connor Wolf Mar 8 '14 at 4:17
• Yeah, I noticed that earlier, i-1 should have been i-2. I mentioned that in the other comment thread, i'll update the post now. – Owen Tourlamain Mar 8 '14 at 4:28
• @user168057 - I tested it with that fix and it works. – Connor Wolf Mar 8 '14 at 4:28