Code for testing 8x8 led matrix Help!

I have trouble understanding why my code is not working.
I am trying to figure out the pin setup of my 8x8 led matrix as i have not been able to locate a proper datasheet. However, it seems whatever entry is first in

const int col[8] = {
11, 10, 9, 12, 13, 14, 15, 16
};

does not light up at all. Please tell where my mistake is :slight_smile:

const int row[8] = {
  3, 6, 8, 4, 1, 7, 2, 5
};

const int col[8] = {
  11, 10, 9, 12, 13, 14, 15, 16
};

void setup() {
  for (int thisPin = 0  ; thisPin < 9; thisPin++) {
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    digitalWrite(col[thisPin], HIGH);
  }
}

void loop() {
  digitalWrite(row[1], 1);
  digitalWrite(row[2], 1);
  digitalWrite(row[3], 1);
  digitalWrite(row[4], 1);
  digitalWrite(row[5], 1);
  digitalWrite(row[6], 1);
  digitalWrite(row[7], 1);
  digitalWrite(row[8], 1);

  digitalWrite(col[1], 0);
  digitalWrite(col[2], 0);
  digitalWrite(col[3], 0);
  digitalWrite(col[4], 0);
  digitalWrite(col[5], 0);
  digitalWrite(col[6], 0);
  digitalWrite(col[7], 0);
  digitalWrite(col[8], 0);
}

sketch_jan07b.ino (883 Bytes)

 for (int thisPin = 0  ; thisPin < 9;
 digitalWrite(row[8], 1);

Accessing the ninth element of an eight element array is not the best thing you could do.
Not accessing the first element is equally bad.

So, i have changed my setup to

const int row[8] = {
  6, 8, 5, 4, 1, 7, 2, 3
};

const int col[8] = {
  9, 10, 11, 12, 13, 14, 15, 16
};

void setup() {
  for (int thisPin = 1  ; thisPin < 8; thisPin++) {
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    digitalWrite(col[thisPin], HIGH);
  }
}

void loop() {
  digitalWrite(row[1], 1);
  digitalWrite(row[2], 1);
  digitalWrite(row[3], 1);
  digitalWrite(row[4], 1);
  digitalWrite(row[5], 1);
  digitalWrite(row[6], 1);
  digitalWrite(row[7], 1);
  digitalWrite(row[8], 1);

  digitalWrite(col[1], 0);
  digitalWrite(col[2], 0);
  digitalWrite(col[3], 0);
  digitalWrite(col[4], 0);
  digitalWrite(col[5], 0);
  digitalWrite(col[6], 0);
  digitalWrite(col[7], 0);
  digitalWrite(col[8], 0);
}

The problem still happens tho. The last entry of the array is not activated.

for (int thisPin = 1  ; thisPin < 8; thisPin++) {
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    digitalWrite(col[thisPin], HIGH);
  }

You still don't seem to understand that array indices start from 0.

Again here. There is no row[8]. row[8] is accessing memory outside your array.

 digitalWrite(row[1], 1);
  digitalWrite(row[2], 1);
  digitalWrite(row[3], 1);
  digitalWrite(row[4], 1);
  digitalWrite(row[5], 1);
  digitalWrite(row[6], 1);
  digitalWrite(row[7], 1);
  digitalWrite(row[8], 1);

Also, digitalWrite should use HIGH or LOW constants, not 0 or 1. It helps with both readability and portability.

I have figured it out now, thank you very much for the help :slight_smile:
I am pretty new to all this.

The array operator foo[bar] takes an array (foo) and an offset into that array (bar). The first element of an array is at offset zero.

PaulMurrayCbr:
The array operator foo[bar] takes an array (foo) and an offset into that array (bar). The first element of an array is at offset zero.

It's just pointer math. arrayName[n] is the exact same thing as saying *(arrayName + n). I think the day I understood that was the day I finally "got it" with C++.

PaulMurrayCbr:
The array operator foo[bar] takes an array (foo) and an offset into that array (bar). The first element of an array is at offset zero.

....and then you find out that bar [foo] accesses the same element!