Apprentice coder

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

   #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.

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.

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"

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.

I don't mean to sound like a simpleton :grin: 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.

Does this help?
The first clause of any "for" loop is executed only once.

Think of it like this:

int row = 0;
while (row < 8)
{
  int col = 0;
  while (col < 8)
  {
    doSomething (matrix [row][col]; 
    col++;
  }
  row++;  
}

Now you can see that "row" is only initialised once.
That is exactly equivalent to two nested "for" loops

So am I right in looking at it like -

  1. the row counter is initialised at zero
  2. the column for loop initialises at col 0, then executes setLed progressively for all columns through to col 7
  3. the code then goes back to the first for loop where the row counter is now set to row 1
  4. the second for loop again executes setLed progressively for all columns through to col 7 but obviously now on row 1
  5. this continues until row 7 goes through the same routine and then the code moves to void down

then the code moves to void down

All correct, except for that very last bit.
It is function down, not void down.

I stand corrected AWOL 8) I should know that it is a function. Thank you so much for taking the time to help me in my "quest to code"
Pedro

Would someone have a moment to explain to me why in this code there is a brief pause before the code resumes if I declare variable test in the for loop as an int, whereas if I declare it as a byte there is no pause. Just curious thanks.

  #include "LedControl.h"
  int timer = 80;
  LedControl lc=LedControl(12,11,10,1);
 
  void setup() 
  {
    lc.shutdown(0,false);
    lc.setIntensity(0,5);
    lc.clearDisplay(0);
  
  }
  void loop ()
  {
    for (int test = B00000001; test>0; test <<=1)
    {
      lc.setColumn(0,1,test);
      delay(timer);
    }
  }

Which has more bits to shift a 1 leftwards across:
B0000000000000001 (int, with upper 8 bits implied even tho not shown)
or
B00000001 (byte)

So moving a "1" leftwards across, is shifting 16 bits to the left before the "1" comes back around, so to speak. But doesn't shifting a "1" to the right also also have to shift 16 bits to the right before comes back around (not doubting you obviously just trying to understand :blush:) or does it have something to do with the difference between declaring the test variable as int or byte? Is maybe the byte declaration shifting a whole byte at a time whereas the bit declaration is shifting one bit at a time. Thanks CR.

It shifts left 16 bits - then it falls off the end, test becomes 0,
the for:next loop ends,
and loop can start over with test = 1.

There's only something displayed while test is 00000001, 00000010, 00000100, 00001000, 00010000, 00100000, 01000000, 10000000.
While test is an int, there is no display while test is 000000010000000, out to 1000000000000000.

I don't see any shifting to the right.

Of course, sorry I don't know how I came up with a shift right. Put it down to brain flatulence 8) As usual CR thanks for your time.

In my "quest to code" so I can stop pestering people on the forum to help me so much :blush: I have been trying to follow Grumpy Mikes tutorial at

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

I did not have a seven segment display so I hooked six LED's up to digital pins 3 to 8 on an Uno and progressed through the tutorial until I successfully got to the stage of running this code.

  int timer = 100;
  int pins [6] = { 3, 4, 5, 6, 7, 8 };
  boolean led [6] = { HIGH, LOW, LOW , LOW, LOW , HIGH };
  
  void setup () 
  
  { 
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
  }
  
   void loop ()
  
  {
    for(int i=0; i<7; i++) digitalWrite(pins[i], led[i]);
  }

The next stage of the tutorial expands the code to use char, a mask and a bitwise AND operation. So obviously I would need to incorporate this code into the previously posted code

char led = B10000001;
int mask = 1;

for(int i=0; i<7; i++)
{
  if((mask & led) == 0) digitalWrite(pins[i], LOW);
 else digitalWrite(pins[i], HIGH);
  mask = mask << 1;
}

I tried to put char led = B10000001; and int mask = 1; at the top of the code prior to setup and the if else and mask code into loop which did compile but did not light LED's 1 and 6 as hoped. I realise that this is a roundabout way to light two LED's but as I said at the start I am doing this to learn how to code and feel that this is an important coding technique to understand and use.
Thank you for taking the time to read this and hopefully explain what I am doing wrong,

Pedro.

Your led array as posted has only six elements, but your bit pattern spans eight.
Post your actual code instead of the second snippet, please.

Here is what I tried AWOL

int timer = 100;
int pins [6] = { 3, 4, 5, 6, 7, 8 };
char led = B10000001;
int mask = 1;

 

 void setup () 
  
  { 
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
  }
  
  
 void loop ()
 
 {

for(int i=0; i<7; i++)
{


 { if((mask & led) == 0) 
  digitalWrite(pins[i], LOW); 
  else digitalWrite(pins[i], HIGH);

 mask = mask << 1;
  
 }
}
}

I see what you mean re the discrepancy between six array elements and a byte for the bit pattern :grin:

The Tools + Auto Format menu item needs to become your friend.

You need to learn where { and } are needed (and where they are not, but are a good idea), and where they are not.

The code after an if or else statement should be in { and }. The if statement itself does not need to be.

The code for a for or if statement should be placed on different lines from the statement.

    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);

should be:

    for(int i = 3; i < 9; i++)
    {
      pinMode(i, OUTPUT);
    }

You can add a Serial.begin() call to setup() and Serial.print() statements to loop() to see what is happening.

Thank you for your response PaulS. I see what you are saying regarding the curly braces and using the serial monitor and point taken, but if I change the code in setup to how you suggest I get errors
"expected initialiser before"for"
"expected constructor, destructor or type conversion before < token"
"expected constructor, destructor or type conversion before ++ token"

Also the initial code that I posted (the code that did work)

     int timer = 100;
  int pins [6] = { 3, 4, 5, 6, 7, 8 };
  boolean led [6] = { HIGH, LOW, LOW , LOW, LOW , HIGH };
  
  void setup () 
  
  { 
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
  }
  
   void loop ()
  
  {
    for(int i=0; i<7; i++) digitalWrite(pins[i], led[i]);
  }

also had this for loop inside the curly braces and I am not doubting your expertise :blush: but I do not understand how your suggestion can help with the problem that I am having. Am I missing something Pedro.

No problems with that code here

Binary sketch size: 934 bytes (of a 30,720 byte maximum)

That is to say, the compiler is OK with it.
I still have issues about the size of the arrays (or the loop lengths, whichever way you look at it)