I’ve written the following code to light up a row of LEDs one at a time.
int ledPins[] = {7,8,9,10,11,12,13};
void setup() {
for (int i = 0; i < sizeof(ledPins); i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < sizeof(ledPins); i++) {
digitalWrite(i, HIGH);
delay(1000);
digitalWrite(i, LOW);
delay(1000);
}
}
The above works fine. However after completing the for loop there is a long delay (about 10 seconds) before it repeats.
Why would there be such a long delay? Is this expected or is it a problem with my code?
the function
sizeof(array)return the size of the array in the memory, in bytes. and becausesizeof(int)is probably not 1, you get a larger value than expected.sizeofcan be use to determine the number of elements in an array, by taking the size of the entire array and dividing it by the size of a single element.so this line:
should be rewritten as:
see:
http://en.wikipedia.org/wiki/Sizeof