What is an Array?

An array is a list of similar items. Now, the values 1, 3.14156, and -200 are sort of
similar, because they are all numbers. But a C++ compiler (like the one underlying the
Arduino IDE) would definitely interpret the floating point number to be unlike 1 and -200,
and might even interpret those two numbers as being dissimilar.

What’s Similar and What’s Not?

Consider the following data:

int      xinum    = 1;   // a signed integer at the native size for the compiler, 16 bits on ATmega chips
int16_t  xi16num  = 1;   // a signed integer, guaranteed to be 16  bits on all systems
long     xlnum    = 1;   // a signed long integer, 32 bits on ATmega chips
uint8_t  xu8num   = 1;   // an unsigned integer, 16 bits
int      yinum    = 2;
int16_t  yi16num  = 2;   
long     ylnum    = 2; 
uint8_t  yu8num   = 2; 
int      zinum    = 2;
int16_t  zi16num  = 2;   
long     zlnum    = 2; 
char     xc       = 'a';      // a character
String   xs       = "a";      // a string, because it is enclosed in double quotes
char     yc       = '#';  
String   ys       = "R & B";  
char     zc       = '$'; 
String   zs       = "#(*&";

So which of the above values could be combined in an array? See the table below:

Possible elements in an array Similar? Explanation
xinum, yinum, zinum Yes All are signed integers
xu8num, yu8num, zinum No Last item is signed
xc, xs, yc No Second item is a String
xs, ys, zs Yes All items are String’s

When You Need an Array

Whenever a program repeats one or more operations on several values of the same
type, then it is probably wise to use an array.

Consider a circuit with 3 LEDs, simulating a traffic light: one is red, one is yellow,
and one is green. Without using an array, you might write:

#define REDLED     4      // the red LED will be connected to Digital Pin 4
#define GREENLED   5
#define YELLOWLED  6
#define REDTIME    10000  // 10 seconds for the red light
#define GREENTIME  6000   //  6 seconds for the green light
#define YELLOWTIME 4000   //  4 seconds for the yellow light

void setup() {
  pinMode(REDLED, OUTPUT);
  pinMode(GREENLED, OUTPUT);
  pinMode(YELLOWLED, OUTPUT);
  digitalWrite(REDLED, LOW);
  digitalWrite(GREENLED, LOW);
  digitalWrite(YELLOWLED, LOW);
}

void loop() {
  digitalWrite(REDLED, HIGH);
  delay(REDTIME);
  digitalWrite(REDLED, LOW);
  digitalWrite(GREENLED, HIGH);
  delay(GREENTIME);
  digitalWrite(GREENLED, LOW);
  digitalWrite(YELLOWLED, HIGH);
  delay(YELLOWTIME);
  digitalWrite(YELLOWLED, LOW);
}

The foregoing is long and repetitive. Worse, if you had a problem involving
more lights (or, more generally, more signals that had to be controlled), then
the number of similar values might be twice as long, or even hundreds of times
as long. Without using arrays, your program would have to grow proportionately.

Array Syntax

In C and C++, an array is expressed using the following format:

data_type name[] = { value1, value2, value3 };

To be concrete, here’s how an array for the list of LED pins in the example above:

The square brackets and the curly braces in the above statement are characteristic
of an array definition statement:

What’s in This Packet

The sketch in this packet shows how to re-write the traffic light
demo using arrays, and then shows how to exploit the existence of an array to
re-write the traffic light demo in a much more compact and efficient way using loops.