Learn to code

ARDUINO & CODES

Previous Post
Understanding If / Else statement in Arduino
Next Post
Using Switch Statements in Arduino

Understanding Arrays in Arduino

in Arduino Basics

The key in programming is to make a few lines of code to do as much work as possible. So far, we already explained how to use for loops in the tutorial: Arduino Tutorial: 2.5 For loop/repeat a statement

With efficient coding, you will save not only time but also eliminate mistakes that occur while coding.

Arrays are groups of the same kind of data that are placed consecutively in memory. For example, we can have an array of integers (type int) which is two or more integer numbers occurring one after the other.

An element in an array refers to each value in the array. If we have an array of integers, then each individual integer is referred to as an element of the array. In an array of bytes, each element is a byte (of the Arduino byte type).

To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The key here is that each element in an array is placed directly after the previous element which allows us to access each element, in turn, using a loop.

Now we know what the idea is behind an array we can apply this knowledge to our previous tutorial: 2.5 Understanding for loops in Arduino. We will enhance this code by using an array for the LED pins and explain the difference. Before we adjust the code we will take a look at the generic code for the array.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 9
220 ohm resistor 220 ohm resistor × 4

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Generic code for arrays

As stated above, An array is a collection of variables that are accessed with an index number. Each variable in the array is called an element. Firstly, we need to declare the array just as we declare a variable.

int myIntegers[4] = {2, 7, 10, 11};
int myPins[] = {2, 7, 10, 11};
int mySensors[5] = {5, 8, -8, 3, 2};
char myMessage[6] = "hello";

In myIntegers we declared a variable without choosing the size of the elements. We can also declare an array without initializing the number of elements as we did in the above example with myPins. Note that when declaring an array of type char, one more element than your initialization is required, to hold the required null character.

The null character is the first element in your array. Arrays are zero indexed. The first element of the array is at index 0. We can apply this to the generic code of arrays above.

int myIntegers[0] = 2 // The first element of the array
int myIntegers[1] = 7 // The second element of the array
int myIntegers[3] = 11 // The last element of the array

It may seem odd that an array with a dimension of four has the last element accessed using array[3], but because the first element is array[0], the four elements are:

array[0],array[1],array[2],array[3]

This example makes use of 4 LEDs connected to the pins 2, 7, 10 and 11 on the board using 220-ohm resistors.

The Code

// sketch using arrays
int ledPins[] = { 2, 7, 10, 11};       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)
void setup() {
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT); // acces the elements of the array 
  }
}
void loop() {
  
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    
    digitalWrite(ledPins[thisPin], HIGH); // turn the LEDs on
  }
  delay(1000); // wait for 1 second before turning the LEDs off
  
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    
    digitalWrite(ledPins[thisPin], LOW); // turning the LEDs off.
  }
  delay(1000); // wait for 1 second before starting the loop again

Code Explanation

So let’s start by looking at out code and explain the beginning. We want to blink 4 LEDs. Therefore we need to declare these pins as output. The most simple method for doing this is written below by repeating the same line over and over

pinMode(2, OUTPUT);
pinMode(7, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);

But it is repetitive and the job of a computer is to do repetitive work not you. The problem here is that each pin needs to be identified in turn on each line. So when faced with this problem your solution is always to think, how can I make this part of the line into a variable?

We already solved this problem by creating a for loop in the previous tutorial. However, this is only relevant if your pins are in sequential order.

for(int i = 2; i < 5; i++) pinMode(i, OUTPUT);

 

If your pins are in an arbitrary order you will find another solution. The answer lies in arrays

You can see an array as making an unordered list into an ordered one. The array holds an index or box in which to put a number or character. Suppose you want to use pins 2, 7, 10  and 11 as output pins. Firstly, we need to define the array with the values five values. Look closely, at the syntaxes that we are using.

int ledPins [4] = { 2, 7, 10, 11 }

 

This array that we named ledPins will have an index from zero to four. The first box ledPins[0]  will contain the value of 2 and the last box ledPins[4] will contain the value of 10. Then it is easy to set all these pins to outputs by using a for loop from 0 till 4.

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT); // acces the elements of the array 
  }

 

Next, we apply the same method for turning the LEDs on and off. We are using a for loop that calls the elements in the declared array.

for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    
    digitalWrite(ledPins[thisPin], HIGH); // turn the LEDs on
  }
  delay(1000); // wait for 1 second before turning the LEDs off

 

Now you know how you can create arrays.  The below example shows you how to assign values to the elements in the array and print them to the serial monitor.

Example Code 2

void setup() {
  int my_array[5];     // an array with 5 integer elements
  int i;               // used as an index into the array
  
  Serial.begin(9600);
  
  my_array[0] = 23;    // assign a value of 23 to the 1st element
  my_array[1] = 1001;  // assign a value of 1001 to the 2nd element, 
  my_array[2] = 9;
  my_array[3] = 1234;
  my_array[4] = 987;
  
  // display each number from the array in the serial monitor window
  
}
void loop() {
for (i = 0; i < 5; i++) {
    Serial.println(my_array[i]);
    delay(1000);
  }
}

 

Conclusion

There are several things to remember when you use arrays in your coding:
 
1. You can use an array when you want to make an arbitrary sequence into a consecutive one.
 
2. Arrays can store patterns.
 
3. You can use arrays in for loops for making a more understandable and simpler code.

In this tutorial I have demonstrated how to use a TM1637 4-digit 7-segment display with the Arduino Uno Board. We created a count down function and two functions to display text to the display.

I hope that you enjoyed reading the tutorial and fount is useful and informative. If you did please subscribe to my Instagram or Pinterest account found at the top of this page.

In any case that you have questions, feel free to ask them in the comments.

2 Comments. Leave new

Hello,
I have a question regarding the array:
How can I pick a single value in the array, foe example, let’s say that I only want to turn on the LED connected to pin 7 (2nd value on the array) if a switch is high, how can I do that?

Thank you in advance

Nick

Reply

    Thank you for your question, If I understand you correctly you want to press something and use the second element in the array. Remember, arrays start from 0. I hope the following code make will make you understand it a bit more.

    const int buttonPin = 2;
    int ledPins[] = { 2, 7, 10, 11};
    pincount = 4;

    void setup() {
    for (int thisPin = 0; thisPin < pinCount; thisPin++) { pinMode(ledPins[thisPin], OUTPUT); // acces the elements of the array // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } if (digitalRead(buttonPin) == HIGH) { // turn LED on: digitalWrite(ledPins[1], HIGH); } else { // turn LED off: digitalWrite(ledPins[1], LOW);

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu