Apprentice coder

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)

AWOL yes that compiled ok for me too. With regard to the "I still have issues about the size of the arrays (or the loop lengths, whichever way you look at it)" I added two more LED's (now eight) and changed the code to

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



void setup () 

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


void loop ()

{

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


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


      mask = mask << 1;


    }
  }
}

but still no luck. I suppose what I am asking is does the format of the code appear to be correct.The " mask = mask << 1;" at the end of the code doesn't quite look right to my very inexperienced eye 8)

Thanks for your help Pedro.

for(int i=0; i<9; i++)

Still unhappy.

How about the debug prints?

You might want to move "mask" and its initialsation into "loop ()"

I don't mean to sound unhappy, just frustrated :grin: But suffice to say AWOL, that edit you just did suggesting
"You might want to move "mask" and its initialsation into "loop ()" did the trick. I tried the dedug print but something just flashed across the screen at light speed and I couldn't remember enough about the serial monitor function to make it stay still until I could read it. So thanks very much for your help and that gives me something to work with. Also thanks to PaulS for his suggestions and help. You fellas are so cluey 8)

Pedro147:
did the trick.

The latest code you posted is still running off the end of the array. It might run and look OK, but it's not right yet.

I don't mean to sound unhappy, just frustrated

No, it is me that is still unhappy with the loop I quoted.

All is good that ends well AWOL. Thanks again for your help

PeterH:

Pedro147:
did the trick.

The latest code you posted is still running off the end of the array. It might run and look OK, but it's not right yet.

Can you offer any suggestions as to how I can fix this problem, because I prefer not to labour under any misapprehensions that the code is correct. Thanks PeterH.

If you've declared an array, then it is best to use it everywhere.
At the moment you're not using it in "setup()".

Also, if you make the array of the correct type, "byte", you can use the size of the array to control your loops, so you should never step off the end.#define N_ELEMENTS(array) (sizeof(array)/sizeof(array[0])).

(In fact, this method works for arrays of any datatype)