user5166: I was just playing around a bit and ran into a strange problem. LED 3, 5, 6, 9 and 10 are on, as intended. For some reason LED 0 and 1 are on as well. Can someone explain why this is happening?
int ledPins[] = {3, 5, 6, 9, 10};
int pinCount = sizeof(ledPins);

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

void loop(){
   for (int i = 0; i < pinCount; i++){
     digitalWrite(ledPins[i], 1);
   }
}


user5168: You haven't initialised the other pins at all; or included your circuit, but clearly you have LEDs on more pins than those defined (5 defined, 7 on).
The other LEDs are, therefore, in an undefined state - and may well be attached to pins set 'on' by default in your circuit. You probably need to either move 0/1 off the serial interface or explicitly set them 'off'.

user42: The behaviour of your program is undefined because you're reading past the end of your array.
In C/C++, the sizeof operator always gives you the size of something in bytes. It doesn't directly tell you the number of items in an array. Since int is usually 2 bytes for Arduino programming, the pinCount variable is actually being initialised to 10 (5 items x 2 bytes each).
The result is that each for loop is actually doing 10 iterations. The first 5 iterations are OK, reading correctly from the ledPins array. However, the rest of the iterations are reading whatever data happens to be in memory after that. This means it could be trying to initialise and switch on any pin, potentially including 0 and 1.
I can't guarantee that this is the source of the problem, but it's certainly very important to fix.
The solution is quite easy though. Divide the total size of the array by the size of a single element:
int pinCount = sizeof(ledPins) / sizeof(int);


5166: <p>I was just playing around a bit and ran into a strange problem. LED 3, 5, 6, 9 and 10 are on, as intended. For some reason LED 0 and 1 are on as well. Can someone explain why this is happening?</p>

<pre><code>int ledPins[] = {3, 5, 6, 9, 10};
int pinCount = sizeof(ledPins);

void setup(){
  for (int i = 0; i &lt; pinCount; i++){
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop(){
   for (int i = 0; i &lt; pinCount; i++){
     digitalWrite(ledPins[i], 1);
   }
}
</code></pre>
 5168: <p>You haven't initialised the other pins at all; or included your circuit, but clearly you have LEDs on more pins than those defined (5 defined, 7 on).</p>

<p>The other LEDs are, therefore, in an undefined state - and may well be attached to pins set 'on' by default in your circuit. You probably need to either move 0/1 off the serial interface or explicitly set them 'off'.</p>
 42: <p>The behaviour of your program is undefined because you're reading past the end of your array.</p>

<p>In C/C++, the <code>sizeof</code> operator always gives you the size of something in <strong>bytes</strong>. It doesn't directly tell you the number of items in an array. Since <code>int</code> is usually 2 bytes for Arduino programming, the <code>pinCount</code> variable is actually being initialised to <strong>10</strong> (5 items x 2 bytes each).</p>

<p>The result is that each <code>for</code> loop is actually doing 10 iterations. The first 5 iterations are OK, reading correctly from the <code>ledPins</code> array. However, the rest of the iterations are reading whatever data happens to be in memory after that. This means it could be trying to initialise and switch on any pin, potentially including 0 and 1.</p>

<p>I can't guarantee that this is the source of the problem, but it's certainly very important to fix.</p>

<p>The solution is quite easy though. Divide the total size of the array by the size of a single element:</p>

<pre><code>int pinCount = sizeof(ledPins) / sizeof(int);
</code></pre>
