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

arduinoled-matrix

I'm trying to drive an 8×8 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}
  };

  updateScreen(display);
}

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

  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.

Best Answer

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

  updateScreen(display);

  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.