Pages: [1] 2 3 ... 6   Go Down
Author Topic: Apprentice coder  (Read 4641 times)
0 Members and 1 Guest are viewing this topic.
Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 12, 2012, 01:42:51 am by AWOL » Logged

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

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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"
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 13, 2012, 06:19:15 am by Pedro147 » Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Think of it like this:
Code:
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
Logged

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

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25732
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
then the code moves to void down
All correct, except for that very last bit.
It is function down, not void down.
Logged

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

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I stand corrected AWOL  smiley-cool  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
Logged


Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
  #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);
    }
  }
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26378
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-red) 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.
Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26378
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course, sorry I don't know how I came up with a shift right. Put it down to brain flatulence  smiley-cool As usual CR thanks for your time.
« Last Edit: October 14, 2012, 09:25:47 pm by Pedro147 » Logged


Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 514
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In my "quest to code" so I can stop pestering people on the forum to help me so much   smiley-red 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.
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
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.


« Last Edit: October 26, 2012, 09:34:13 pm by Pedro147 » Logged


Pages: [1] 2 3 ... 6   Go Up
Jump to: