 # Understanding Lines of codes from a tutorial (are they superfluous?)

Hi guys,
I am new to arduino and coding. I got my first Uno and starter kit last week and have been following along tutorials to start making sense of the language and electronics. I’m really enjoying it and start to develop ideas for projects of my own.

At the moment, I am playing with a 8x8 LED matrix (I gather they are quite common) and have been following the tutorial from the manufacturer (attached).

On page 6 the code is written as follows:

``````//pin to control ROW
const int row1 = 2; // number of the row pin 9
const int row2 = 3; // number of the row pin 14
const int row3 = 4; // number of the row pin 8
const int row4 = 5; // number of the row pin 12
const int row5 = 17; // number of the row pin 1
const int row6 = 16; // number of the row pin 7
const int row7 = 15; // number of the row pin 2
const int row8 = 14; // number of the row pin 5
//pin to control COL
const int col1 = 6; // number of the col pin 13
const int col2 = 7; // number of the col pin 3
const int col3 = 8; // number of the col pin 4
const int col4 = 9; // number of the col pin 10
const int col5 = 10; // number of the col pin 6
const int col6 = 11; // number of the col pin 11
const int col7 = 12; // number of the col pin 15
const int col8 = 13; // number of the col pin 16

void setup (){
int i =0;
for(i=2;i<18;i++) {
pinMode(i, OUTPUT);
}
pinMode(row5, OUTPUT);
pinMode(row6, OUTPUT);
pinMode(row7, OUTPUT);
pinMode(row8, OUTPUT);
for(i=2;i<18;i++) {
digitalWrite(i, LOW);
}
digitalWrite(row5, LOW);
digitalWrite(row6, LOW);
digitalWrite(row7, LOW);
digitalWrite(row8, LOW);
}

void loop(){
int i;
// the row #1 and col #1 of the LEDs turn on
digitalWrite(row1, HIGH);
digitalWrite(row2, LOW);
digitalWrite(row3, LOW);
digitalWrite(row4, LOW);
digitalWrite(row5, LOW);
digitalWrite(row6, LOW);
digitalWrite(row7, LOW);
digitalWrite(row8, LOW);
digitalWrite(col1, LOW);
digitalWrite(col2, HIGH);
digitalWrite(col3, HIGH);
digitalWrite(col4, HIGH);
digitalWrite(col5, HIGH);
digitalWrite(col6, HIGH);
digitalWrite(col7, HIGH);
digitalWrite(col8, HIGH);
delay(1000);
// turn off all
for(i=2;i<18;i++){
digitalWrite(i, LOW);
}
delay(1000);
}
``````

I understand that the first part of the code links rows and columns to the pins that they are plugged to, and I understand that the void loop () defines how to flow current through the matric so that LED in row 1 col 1 lights up and then turns off.

What I am not sure about is the void setup() code.

I understand that the first for () function loops through all the pins to define their mode to output and the second for () function loops through all the pins to to set the output to LOW.

What I do not understand is why rows 5 to 8 would require to be treated outside of both for () functions.

In the schematics (and as visible in the code), rows 5 to 8 are plugged to analog pins A3 to A0. Is that the reason they have to be isolated?

I am all the more confused that taking those lines of code out does seem to change the final result on the LED matrix.

Has anyone got insights that they could share?

Thanks a lot in advance.  This question might be extremely trivial but would help validate my understanding of that bit of code.

Have a great day all! Aym

8x8_dot_matrix_mod.pdf (1.06 MB)

I’d define the pins as a couple of arrays, and make the code shorter and correct.
I’d use uint8_t (aka “byte”) instead of int.
I’d make the pin an OUTPUT and write it LOW in the same loop.

It looks like the code author was unsure of himself regarding the pin numbers (as opposed to names such as A0, A1 etc) for the analogue pins. He certainly did not choose to declare the variables and use them in the most effective way

It would have been better to put the pin numbers in an array of bytes and to iterate through them to set their pinModes. The entries in the array could then use the pin names such as A0, A1 etc which the compilation process would convert to pin numbers.

agree with AWOL but to the question I don't see why they are there either, they are encompassed in the for loop... so should not matter if wires are connected to right place...

what type of arduino do you have?

On a UNO A0 is indeed 14 (A1 is 15 and A2 is 16...) but on a MEGA A0 is 54, A1 is 55 and A2 is 56, ...

It is a UNO indeed with analog pins A0 to A5 (looks to work quite well with 14-19)

Happy to see that I seem to have understood the code well enough.

I will definitely do more research into arrays (I have not used them yet).

Thanks again! Have a great day and week ahead!

aym_rico:
I will definitely do more research into arrays (I have not used them yet).

could be something like this

``````const byte row[]  = {2, 3, 4, 5, 17, A2, A1, A0};
const byte col[]  = {6, 7, 8, 9, 10, 11, 12, 13};
const byte nbRows = sizeof(row) / sizeof(row);
const byte nbCols = sizeof(col) / sizeof(col);

void allPinsOff()
{
for (byte r = 0; r < nbRows; r++) digitalWrite(r, LOW);
for (byte c = 0; c < nbCols; c++) digitalWrite(c, LOW);
}

void setup ()
{
// set pins mode
for (byte r = 0; r < nbRows; r++) pinMode(r, OUTPUT);
for (byte c = 0; c < nbCols; c++) pinMode(c, OUTPUT);
// set pins status
allPinsOff();
}

void loop() {

// the row #1 and col #1 of the LEDs turn on
digitalWrite(row, HIGH);
for (byte r = 1; r < nbRows; r++) digitalWrite(r, LOW); // no really necessary as we know they are LOW already

digitalWrite(col, HIGH);
for (byte c = 1; c < nbCols; c++) digitalWrite(c, LOW); // no really necessary as we know they are LOW already

delay(1000);

allPinsOff();     // turn off all

delay(1000);
}
``````

could be a bit simpler here as you have the same number of rows and columns so you could group things but that gives you an idea on how to use arrays.

Just remember index for your arrays start at 0 and goes to n-1 if you have n elements

it’s even easier with range based for, no need for a sizeof helper any more:

``````  for (auto &r : row) pinMode(r, OUTPUT);
for (byte c = 0; c < nbCols; c++) pinMode(c, OUTPUT);
``````

very much simplified:
let choose the compiler the size of your variable (auto)
declare a reference (&)
accessible by a variable (r)
depending on your set of data (row)
and you get the content of each row in r.