Go Down

Topic: Apprentice coder (Read 5 times) previous topic - next topic

Pedro147

Being relatively new to Arduino I am trying to come to grips with the coding side of things. With regard to the following code

Code: [Select]
   #include "LedControl.h"
int timer = 50;

LedControl lc=LedControl(12,11,10,1);

void setup()
{
  lc.shutdown(0,false);
  lc.setIntensity(0,5);
  lc.clearDisplay(0);
}

void down()
{
  for(int row = 0; row < 8 ; row ++)
  {

    for(int col = 0; col < 8; col ++)

    {   
      lc.setLed(0,row,col,true);
      delay(timer);
    }
  }
}


void up ()
{
  for(int row = 7; row >=0; row --)

  {

    for(int col = 7; col >=0; col --)

    {
      lc.setLed(0,row,col,false);
      delay(timer);

    }
  }
}

void loop ()
{
    down ();
  //  lc.clearDisplay(0);
  up ();
}


Which as I am sure you can see is to control an 8 x 8 LED matrix with a Max 7219 progressively lighting the LED's from position (0, 0) to LED at position (7, 7) the top row of the matrix being LED (0, 0) TO LED (0,7) and then once all 64 LED's are on, turning them off one by one back to the starting position.

With regard to the first two for loops, in the void  down () function, presumably in the first for loop, int row = 0; initialises the row counter variable as zero, then row < 8 ; is the test condition which is true, so the code moves to the second for loop and goes through the same procedure for the columns and if this test is also true then the code lc.setLed(0,row,col,true); is executed lighting the LED at position (0, 0) then implementing delay(timer);

What I am having trouble understanding is why the code for the columns, increments from column 0 to column 7, before the first for loop then does it's first increment from the initialised row 0 to row 1, prior to the second for loop again incrementing through columns 0 to 7.

I think I understand the basic sequence of events in one for loop but two nested like this is confusing me. Can anyone please take a few minutes to try and explain the sequence of decisions that this code executes.

Thanks for reading and I hope my rambling description makes sense,

Pedro.
     

AWOL

#1
Oct 12, 2012, 08:39 am Last Edit: Oct 12, 2012, 08:42 am by AWOL Reason: 1
Quote
What I am having trouble understanding is why the code for the columns, increments from column 0 to column 7, before the first for loop then does it's first increment from the initialised row 0 to row 1, prior to the second for loop again incrementing through columns 0 to 7.

I'm not sure I understand the question.
You can only do one thing at a time, so scanning is this way seems, to me, perfectly natural.

When you read a page of text, you scan the words first across the line, then down the page.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pedro147

I realise that this is normal behaviour but what I am trying to ask is why the code "uses" row 0 then goes to the columns "for loop" lighting all LED's in the row 0 and then incrementing the row counter to row 1 and lighting all the LED's in row 1. Why does the code remain in the column "for loop" incrementing from col 0 to col 7 before going back to the row "for loop"

AWOL

Because when you're reading a page of text, you read the columns (words) across the page, before going on to the next row of words.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pedro147

#4
Oct 12, 2012, 10:01 am Last Edit: Oct 13, 2012, 01:19 pm by Pedro147 Reason: 1
I don't mean to sound like a simpleton  :smiley-mr-green: but if you read a page you go through it from start to finnish without going back and repeatably reading one particular passage. This is what is what I am not getting. Do not some sections of code execute what is "in the curly brackets"and then go back to the for loop that initiated them until that loop cannot logically execute what is "in the curly brackets" and then either go onto the next section of code in void loop or back to the start of void loop. Thanks for trying to help me with this I appreciate your time.

Go Up