basic question about LED code

hello :slight_smile:
I've just started to programming Arduino with some LED example and I want Know
why this code don't work correctly on my board:

  • I have 6 LED to switch on sequentially, I use a for cycle to increment the numer of pin to switch on:
int pin=1;

void setup()
  {
    pinMode (pin,OUTPUT);
  }
  
void loop()
  {
    for (pin=1; pin<7; pin++)
      {
         digitalWrite (pin,HIGH);
         delay (100);
         digitalWrite (pin, LOW);
         delay (100);
      }
    }

It doesn't work correctly because only LED 1 switch on with max luminance , other led switch on but not a full power (very poor),
only pin that I declared at begin works fine , why??

I think it's because only pin 1 is set as OUTPUT in setup()... it only runs that line once and pin=1 at that time.

thanks for your answer, how I can resolve this problem?

You could move the pinmode into the for loop inside loop(), although that's an ugly solution because it will keep on re-doing that.

So better is to put the pinmode into a for loop of its own, exactly as you have inside loop(), but in setup() and with only the pinmode inside.... or have 6 pinmodes, each doing a different pin.