counting with for loop

heey there. I’m making a night rider project and when i want the leds to go back i need to use 2 in the for loop. i don’t know why i have to do it this way can somebody explain it to me? i wil mark the piece of code i mean.

int ledpin[4]={3, 5, 6, 9};

void setup() {
  
  for (int i=0; i<4; i++){
pinMode(ledpin[i], OUTPUT);

  }
}

void loop() {
  for( int i=0; i<4; i++){
    
    for(int a=0; a<=255; a++){
  analogWrite(ledpin[i], a);
  delay(5);
    }
    for(int a=255; a>=0; a--){
  analogWrite(ledpin[i], a);
  delay(5);
    }
  }

   [color=red] for( int i=2; i>0; i--){[/color]
      for(int a=0; a<=255; a++){
  analogWrite(ledpin[i], a);
  delay(5);
      }

       for(int a=255; a>=0; a--){
  analogWrite(ledpin[i], a);
  delay(5);
       }
    }
}

You can’t use ‘color’ tags to mark things in a code block.
Also, it’s a good idea to format your code properly before posting. It makes it much easier to decipher:-
(I marked the relevant line.)

int ledpin[4] = {3, 5, 6, 9};

void setup()
{
    for (int i = 0; i < 4; i++)
    {
        pinMode(ledpin[i], OUTPUT);
    }
}

void loop()
{
    for ( int i = 0; i < 4; i++)
    {
        for (int a = 0; a <= 255; a++)
        {
            analogWrite(ledpin[i], a);
            delay(5);
        }
        for (int a = 255; a >= 0; a--)
        {
            analogWrite(ledpin[i], a);
            delay(5);
        }
    }

    for ( int i = 2; i > 0; i--)    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    {
        for (int a = 0; a <= 255; a++)
        {
            analogWrite(ledpin[i], a);
            delay(5);
        }

        for (int a = 255; a >= 0; a--)
        {
            analogWrite(ledpin[i], a);
            delay(5);
        }
    }
}

I’m too tired to go over your code, but it’ll be easier now for the next viewer. :slight_smile:

i need to use 2 in the for loop.

You appear to want to operate on the pins in reverse order, excluding the first and last pins. The index for the second to last pin is 2. Why is that a mystery.

Your indenting sucks, and there is no excuse for that, since Tools + Auto Format will fix what you messed up.

Where are the comments?

Your sequence (ignoring the fading) is

0 1 2 3
H L L L
L H L L
L L H L
L L L H

L L H L
L H L L

The first for loop takes care of the first 4 (forward). The second loop takes care of the last 2 (reverse).

If you would use 3 instead of 2, the last LED will light again. Also note that the the second loop does not go to 0 but stops at 1 so the first LED does not light again either.

There are many, many, many …, (you get the picture) ways to do this but here is one perhaps you’ll get something out of it other than a working lighting sequence.

// ... NAME EACH OF OUR PINS SO WE KNOW WHAT THE HELL THEY ARE FOR  ...
const uint8_t           pinLED_1         = 3;
const uint8_t           pinLED_2         = 5;
const uint8_t           pinLED_3         = 6;
const uint8_t           pinLED_4         = 9;

// ... ALIAS LED STATE FOR READABILITY ...
const uint8_t           LED_OFF         = LOW;
const uint8_t           LED_ON          = HIGH;

// ... ARRAY OF ALL LEDS USED BY THE SEQUENCER ...
const uint8_t           pinsLEDS[]      = { pinLD1, pinLD2, pinLD3, pinLD4 };

// ... SOME CONSTANTS FOR USE IN UP-DATING TO LED"S BEING SEQUENCED ...
const size_t            ARRAY_RANGE     = sizeof(pinsLEDS) / sizeof(pinsLEDS[0]);
const size_t            ARRAY_MIN       = 0;
const size_t            ARRAY_MAX       = ARRAY_RANGE - 1;

const unsigned long     DELAY_SOME      = 100UL;

void loop()
{
    // ... SEQUENCE INCREMENT VALUE, +1 OR -1 ...
    stat int    increment       = 1;

    // ... INDEX OF LED IN ARRAY THAT IS CONSIDERED ON ...
    static size_t     index_pinsLEDS  = 0;

    // ... SET LED'S CURRENT STATE ...
    for ( int i = ARRAY_RANGE; i--; )
    {
        digitalWrite(pinsLEDS[i], ((i == index_pinsLEDS) ? LED_ON : LED_OFF));
    }


    // ... UPDATE SEQUENCE ...
    index_pinsLEDS += increment;

    // ... IF WE'VE REACHED JUST SHORT OF EITHER END OF THE ARRAY ..
    if ( (index_pinsLEDS < (ARRAY_MIN + 1)) || (index_pinsLEDS > (ARRAY_MAX - 1)) )
    {
        // ... NEGATE OUR INCREMENT VALUE SO NEXT OFFSET IS OTHER DIRECTION ...
        increment = -increment;
    }

    // ... LET A LITTLE TIME PASS BEFORE WE UPDATE THE DISPLAY AGAIN ...
    delay(DELAY_SOME);
}

void setup()
{
    // ... SET PIN MODE, AND STATE, FOR EVERY PIN IN THE ARRAY ...
    for ( size_t i = ARRAY_RANGE; i--; )
    {
        pinMode(pinsLEDS[i], OUTPUT);

        digitalWrite(pinsLEDS[i], LED_OFF);
    }
}