Code change led brightness__unintentional

Hi
Newbe to the forum and programming and microprocessors.
Going through the example code to learn I have come across a problem that got me best:

  1. Running the following code on UNO board the LED's light up brightly and behave like knight rider effect...
    Sketch_novb_knight_rider_bright
    int timer = 100; // The higher the number, the slower the timing.
    void setup() {
    // use a for loop to initialize each pin as an output:
    for (int thisPin = 2; thisPin < 8; thisPin++) {
    pinMode(thisPin, OUTPUT);
    }
    }
    void loop() {
    // loop from the lowest pin to the highest:
    for (int thisPin = 2; thisPin < 8; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
    }
    // loop from the highest pin to the lowest:
    for (int thisPin = 7; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
    }
    }

  2. Running the following code 'my own first attempt' results in; the first LED lights up brightly but all the following LED's light up dimly. At least I got the same Knight rider affect
    Sketch_nov27a_knight_rider_dim
    int pins[]={2,3,4,5,6,7};
    int timer =100;
    int x=0;
    void setup(){
    pinMode (pins[x],OUTPUT);
    }
    void loop(){
    for (x=0;x<5;x++){
    digitalWrite (pins[x],HIGH);
    delay(timer);
    digitalWrite (pins[x],LOW);
    }
    for (x=5;x>0;x--){
    digitalWrite(pins[x],HIGH);
    delay(timer);
    digitalWrite(pins[x],LOW);}
    }

I am going to attempt up-loading a vid of it to you-tube.....first attempt at it, so if I succeed I will update this post with a link to it......soon as I figure out how to do that to.
PS: the second set of code keeps changing to bullet points when i pre-view the post.. Do not know why.
Kind regards

Hi and welcome.
Post your code in code tags (use the # button above the edit field).
Always do that when posting code.

In setup, you are just setting 1 pin as OUTPUT.
You should add something there i guess.

Hi
Thanks for the # button help MAS3. I will do that next time..or...Going to give it a try:

Sketch_nov27a_knight_rider_dim
int pins[]={2,3,4,5,6,7};
int timer =100;
int x=0;
void setup(){
pinMode (pins[x],OUTPUT);
}
void loop(){  
  for (x=0;x<5;x++){
    digitalWrite (pins[x],HIGH);
    delay(timer);
    digitalWrite (pins[x],LOW);
  }
  for (x=5;x>0;x--){
    digitalWrite(pins[x],HIGH);
    delay(timer);
    digitalWrite(pins[x],LOW);}
  }

Worked great. Thanks mate.
Got a video of it up onto Youtube; LED issue Arduino UNO - YouTube

The one uses a 'FOR' loop and the other I tried to use an 'Aray' of pins, Both programs seems to run good but the LED brightness change between the two.

Did you read my 2nd reply ?

int x=0;
void setup(){
pinMode (pins[x],OUTPUT);
}

Something 's missing here.

Hi MAS3
sorry for the delay in my reply......joys of being an shift worker.
In any case, I have read your reply and did look into it. I still do not under stand how or why changing any thing in the code at that spot could change the Led brightness.

Did you have a look at the vid I posted on YouTube?

Thanks for your response.

The original code sets all the pins as output
Yours only sets one of them!

Hi
Still struggling with this.

Sketch_nov27a_knight_rider_dim
int pins[]={2,3,4,5,6,7};   //  declare array with 6 elements….element 0 = pin 2, element 1 = pin 3, etc. 
int timer =100;
int x=0;                           // declare variable to substitute in for loop
void setup(){
pinMode (pins[x],OUTPUT); // set pinmode as output for [x]- array of elements with variable x
}
void loop(){  
  for (x=0;x<5;x++){               // for x with a value/startpoint of 0 (do the code below) then add 1 to 
                                                //  current value of x
    digitalWrite (pins[x],HIGH);      // with x = element 0 it will blink pin 2 and start the loop over but 
    delay(timer);                               //  with x incremented to value of 1
    digitalWrite (pins[x],LOW);      // with x = element 1 it will blink pin 3 and start the loop over
  }                                                      // this will go an till end point of for loop is reached,  x<5 
  for (x=5;x>0;x--){                         // do not know how to type “ x smaller and equal to 5” so used x<5
    digitalWrite(pins[x],HIGH);
    delay(timer);                              // the second loop does the same just in reverse of first loop
    digitalWrite(pins[x],LOW);}
  }

The code runs fine. The LED's blink up in sequence left and rite. The abnormality is that pin 2 lights up more brightly than the rest. Since the same 'for loop' is used for all the pins, Why would one pin light up more brightly than the rest?

I have a youtube video link up if my ramblings make no sense.
Kind regards
Tjaart

Hi Tjaart.

Sketch_nov27a_knight_rider_dim
int pins[]={2,3,4,5,6,7};   //  declare array with 6 elements….element 0 = pin 2, element 1 = pin 3, etc. 
int timer =100;
int x=0;                           // declare variable to substitute in for loop
void setup(){
pinMode (pins[x],OUTPUT); // set pinmode as output for [x]- array of elements with variable x
}

I'll try again.
Your problem is in the above shown part of your sketch.
In the first line you declare the pins, 6 of those.
In the second line you declare an integer to hold the value of 100.
Third line integer x is set to contain the value 0.
In setup you say: set pinmode as output for [x]- array of elements with variable x.
That is true, you are setting pins[0] (which is actually pin 2) to output.
All other pins remain as they are, and that is obviously not OUTPUT.
Maybe increasing x would help ?

Hi MAS3
Appreciate the help.
"for (x=0;x<5;x++)"
Would the value of x not increment by 1 each time the 'FOR loop" run.
I thought it would start as 0 and go on to 1 then 2 then 3 and so on.

Something just realized: :slight_smile:

i am setting the pin-out mode before the loop, hence the loop will increment within the loop and only in the loop.
With pin-mode setup before the loop the value of pin-out x may not increment.

I am going to attempt and shift the pin-out statement from void setup to be included in the loop.

SADLY I will only get to do/test this later today if not tomorrow.

You need a for loop inside the setup function as well as in your loop function in order to set ALL the pins to be outputs and not just one of them.
In an arduino the setup function runs only once, the loop function keeps on running.

:slight_smile: :slight_smile: Jipeee got it

int pins[]={2,3,4,5,6,7};
int timer =100;
int x=0;
void setup(){}
void loop(){
  
  for (x=0;x<5;x++){
    pinMode (pins[x],OUTPUT);
    digitalWrite (pins[x],HIGH);
    delay(timer);
    digitalWrite (pins[x],LOW);
  }
  for (x=5;x>0;x--){
    pinMode (pins[x],OUTPUT);
    digitalWrite(pins[x],HIGH);
    delay(timer);
    digitalWrite(pins[x],LOW);}
  }

The link to youtube video: Led & uno - YouTube

Kind regards
Tjaart

Well it might work but the proper way is to do all the initialisation of the pin mode in the setup function.
Your way you needlessly keep doing a pinMode over and over, you only need to do them once for each pin.

Hi Tjaart.

It is important that you read (and understand) replies to your questions very well.
For me, if i need some help, i'll read the answers quickly first to see whats in the post.
After that i'll probably read each line once more to see what's really in those lines.

I told you in my last reply the exact location of your problem in the sketch.
Grumpy_Mike just told you why that is the correct place to do this.
You do not want your Arduino to do things over and over again if not needed.
You'll find that out when you're going to do some more complicated sketches, but it's best when that is learned as early as possible.

So almost there, now put things in the optimum spot in your sketch and show off that sketch over here.
Others will learn from your adventures/discoveries too.