Sub topic 9
Arrays and Looping Over Them
This chapter assumes you know the Arduino sketch structure, can configure digital pins with pinMode(), and can write simple for loops. You also need an Arduino board with four LEDs and suitable resistors, or you can adapt the examples to the board's built-in LED.
An array is a fixed-size collection of values with the same data type. Instead of storing four pin numbers in four separate variables, you can store them in one array:
const byte ledPins[] = {3, 4, 5, 6};The array contains four byte values. Each value has an index, which is its position in the array. Arduino arrays use zero-based indexing, so the first item is at index 0 and the last item is at index 3.
| Expression | Value |
|---|---|
| `ledPins[0]` | `3` |
| `ledPins[1]` | `4` |
| `ledPins[2]` | `5` |
| `ledPins[3]` | `6` |
The expression ledPins[2] reads the third item. You can also assign a new value to an array element, as long as the index is valid:
ledPins[2] = 7;Do not access an index outside the array. Reading ledPins[4] in this example is an error because no fifth element exists. C++ does not automatically stop you from making this mistake.
Counting and iterating over an array
A for loop is a natural way to visit every array element. The loop variable starts at zero, continues while it is less than the number of elements, and increases after each pass.
const byte ledPins[] = {3, 4, 5, 6};
const byte ledCount = sizeof(ledPins) / sizeof(ledPins[0]);
void setup() {
for (byte i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (byte i = 0; i < ledCount; i++) {
digitalWrite(ledPins[i], HIGH);
delay(200);
digitalWrite(ledPins[i], LOW);
}
}sizeof(ledPins) gives the array's total size in bytes. sizeof(ledPins[0]) gives the size of one element. Dividing the two produces the number of elements. This calculation works when ledPins is an array in the same scope where the calculation is made.
The first loop configures every pin. The second loop turns each LED on, waits, and turns it off before moving to the next pin. To add another LED, add its pin number to the initializer. The loops then use the new count automatically.
Choosing the right loop
Use a for loop when you know the start, end condition, and update step. It is usually the clearest choice for arrays.
Use a while loop when repetition depends on a condition that may change unpredictably:
while (digitalRead(2) == HIGH) {
// Keep working while the button signal is HIGH.
}A do-while loop always runs its body at least once because it checks the condition afterward:
byte index = 0;
do {
digitalWrite(ledPins[index], HIGH);
delay(100);
digitalWrite(ledPins[index], LOW);
index++;
} while (index < ledCount);For an array, make sure the condition prevents the index from going past the last element.
Common errors
- Starting the index at
1, which skips the first element. - Using
<=instead of<in the loop condition. - Changing the array but keeping a hard-coded count.
- Using an array element before configuring its pin.
- Assuming an array grows automatically. A built-in array has a fixed size after compilation.
Arrays and loops work especially well together because one variable can represent many related hardware items. This makes a sketch easier to extend and reduces copied code.
Exercise
What does ledPins[1] contain when const byte ledPins[] = {3, 4, 5, 6};?
Show answer
It contains 4, because array indexing starts at zero.