user28087: I made some arduino code for leds that blink down a row and then back again, but when the program is run, there is an approximately 2 second delay in between the first for loop and the second. This is my code in the loop function:
int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < sizeof(leds); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i < sizeof(leds); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i; i > 0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}

It really makes the program look weird when the LEDs are blinking on a breadboard. Keep in mind that this only happens after the first for loop, the second is continuous back to the first. Any idea why this might be and how to fix it?

user28033: The problem is on "for loops" condition,
Try this:
int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < (sizeof(leds)/sizeof(leds[0])); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i < (sizeof(leds)/sizeof(leds[0]); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i=((sizeof(leds)/sizeof(leds[0])-1); i>=0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}


user3917: The claim “The cause it that your 3rd led (2nd element in the array) is reapeted twice” is wrong.  
Apparently that sentence is supposed to mean that the long delay is due to i being one too large, coming out of the first loop and entering the second one.  Indeed, i is too large, but that's because each element of the array is a 2-byte int, so that instead of for (int i = 0; i < sizeof(leds); i ++) being equivalent to for (int i = 0; i < 3; i ++), it is equivalent to for (int i = 0; i < 6; i ++).  
Thus, one second goes by with an extra 4 occurrences of delay(250); in the first loop, and another one second with 4 extra occurrences of delay(250); in the other loop.
To correct the problem, write sizeof leds/sizeof leds[0] instead of sizeof(leds), and write for (--i; i > 0; i--) instead of for (i; i > 0; i --).

28087: <p>I made some arduino code for leds that blink down a row and then back again, but when the program is run, there is an approximately 2 second delay in between the first for loop and the second. This is my code in the loop function:</p>

<pre><code>int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i &lt; sizeof(leds); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i &lt; sizeof(leds); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i; i &gt; 0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}
</code></pre>

<p>It really makes the program look weird when the LEDs are blinking on a breadboard. Keep in mind that this only happens after the first for loop, the second is continuous back to the first. Any idea why this might be and how to fix it?</p>
 28033: <p>The problem is on "for loops" condition,<br>
Try this:</p>

<pre><code>int leds[] = {13, 12, 11};
int i;

void setup() {
  Serial.begin(9600);

  for (int i = 0; i &lt; (sizeof(leds)/sizeof(leds[0])); i ++) {
     pinMode(leds[i], OUTPUT);
  }
}

void loop() {
    for (i = 0; i &lt; (sizeof(leds)/sizeof(leds[0]); i ++) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
    for (i=((sizeof(leds)/sizeof(leds[0])-1); i&gt;=0; i --) {
       digitalWrite(leds[i], HIGH);
       delay(250);
       digitalWrite(leds[i], LOW);
    }
}
</code></pre>
 3917: <p>The claim “The cause it that your 3rd led (2nd element in the array) is reapeted twice” is wrong.  </p>

<p>Apparently that sentence is supposed to mean that the long delay is due to <code>i</code> being one too large, coming out of the first loop and entering the second one.  Indeed, <code>i</code> <em>is</em> too large, but that's because each element of the array is a 2-byte <code>int</code>, so that instead of <code>for (int i = 0; i &lt; sizeof(leds); i ++)</code> being equivalent to <code>for (int i = 0; i &lt; 3; i ++)</code>, it is equivalent to <code>for (int i = 0; i &lt; 6; i ++)</code>.  </p>

<p>Thus, one second goes by with an extra 4 occurrences of <code>delay(250);</code> in the first loop, and another one second with 4 extra occurrences of <code>delay(250);</code> in the other loop.</p>

<p>To correct the problem, write <code>sizeof leds/sizeof leds[0]</code> instead of <code>sizeof(leds)</code>, and write for <code>(--i; i &gt; 0; i--)</code> instead of <code>for (i; i &gt; 0; i --)</code>.</p>
