Go Down

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

AWOL

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

Here is what I tried AWOL

Code: [Select]

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  :smiley-mr-green:

PaulS

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.

Code: [Select]
    for(int i = 3; i < 9; i++) pinMode(i, OUTPUT);
should be:
Code: [Select]
    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.

Pedro147

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)
Code: [Select]
     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  :smiley-red:  but I do not understand how your suggestion can help with the problem that I am having. Am I missing something  Pedro.

AWOL

No problems with that code here
Quote
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)
"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.

Go Up