How to Use Arrays

A variation on the For Loop example that demonstrates how to use an array.

This variation on the For Loop Iteration example shows how to use an array. An array is a variable with multiple parts. If you think of a variable as a cup that holds values, you might think of an array as an ice cube tray. It's like a series of linked cups, all of which can hold the same maximum value.

The For Loop Iteration example shows you how to light up a series of LEDs attached to pins 2 through 7 of the Arduino board, with certain limitations (the pins have to be numbered contiguously, and the LEDs have to be turned on in sequence).

This example shows you how you can turn on a sequence of pins whose numbers are neither contiguous nor necessarily sequential. To do this is, you can put the pin numbers in an array and then use for loops to iterate over the array.

This example makes use of 6 LEDs connected to the pins 2 - 7 on the board using 220 ohm resistors, just like in the For Loop. However, here the order of the LEDs is determined by their order in the array, not by their physical order.

This technique of putting the pins in an array is very handy. You don't have to have the pins sequential to one another, or even in the same order. You can rearrange them in any order you want.

Hardware Required

  • Arduino Board

  • 6 LEDs

  • 6 220 ohm resistors

  • hook-up wires

  • breadboard

Circuit

Connect six LEDs, with 220 ohm resistors in series, to digital pins 2-7 on your board.

circuit

Schematic

schematic

Code

1/*
2
3 Arrays
4
5 Demonstrates the use of an array to hold pin numbers in order to iterate over
6
7 the pins in a sequence. Lights multiple LEDs in sequence, then in reverse.
8
9 Unlike the For Loop tutorial, where the pins have to be contiguous, here the
10
11 pins can be in any random order.
12
13 The circuit:
14
15 - LEDs from pins 2 through 7 to ground
16
17 created 2006
18
19 by David A. Mellis
20
21 modified 30 Aug 2011
22
23 by Tom Igoe
24
25 This example code is in the public domain.
26
27 https://www.arduino.cc/en/Tutorial/Array
28
29*/
30
31int timer = 100; // The higher the number, the slower the timing.
32int ledPins[] = {
33
34 2, 7, 4, 6, 5, 3
35}; // an array of pin numbers to which LEDs are attached
36int pinCount = 6; // the number of pins (i.e. the length of the array)
37
38void setup() {
39
40 // the array elements are numbered from 0 to (pinCount - 1).
41
42 // use a for loop to initialize each pin as an output:
43
44 for (int thisPin = 0; thisPin < pinCount; thisPin++) {
45
46 pinMode(ledPins[thisPin], OUTPUT);
47
48 }
49}
50
51void loop() {
52
53 // loop from the lowest pin to the highest:
54
55 for (int thisPin = 0; thisPin < pinCount; thisPin++) {
56
57 // turn the pin on:
58
59 digitalWrite(ledPins[thisPin], HIGH);
60
61 delay(timer);
62
63 // turn the pin off:
64
65 digitalWrite(ledPins[thisPin], LOW);
66
67 }
68
69 // loop from the highest pin to the lowest:
70
71 for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
72
73 // turn the pin on:
74
75 digitalWrite(ledPins[thisPin], HIGH);
76
77 delay(timer);
78
79 // turn the pin off:
80
81 digitalWrite(ledPins[thisPin], LOW);
82
83 }
84}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/28 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.