Unit 15 – Using Arrays with Arduino

Arduino Array Title

The code of the last example is very long. But actually almost the same things are repeated all the time, no? We can do it easier – using the array.

An array is a special type of variable. It can not only store one but multiple values.

Think of it as a cargo ship. It can store containers. Every container has got it’s own number. If you want to look inside, you need to tell the number. Those numbers start with 0 not with 1 what sometimes is confusing.

Arduino Array compared with a container ship

Of course the array also has to be declared (created). There are several ways to do that.

This is how to declare an empty array without value assignment.

int nums[6];

An array named «nums» is created. The variable type is int (integer numbers). Arrays usually can only store values of the same type. The 6 in the brackets means that our cargo ship has got six containers where we can put cargo (numbers) in. It is called the length of an array.

Arduino Array compared with a container ship declaration

An other way to create an array:

int nums[] = {10, 78, 45, 67, 38, 14};

Now we declare the array and directly assign values. We don’t have to tell it the length. By the way, you find the square brackets [] on your keyboard pressing Alt-Gr + 8 and Alt-Gr + 9 on a PC or Alt + 5 and Alt + 6 on a Mac. So, what is the length of the array? Right: It again is six.

Arduino Array compared with a container ship assignment

But how can we write values into the array? Actually, it is done the same way like with normal variables. The only difference is that we have to state the number of the container.

nums[3] = 50;
Arduino Array compared with a container ship one value

And how can we read it out? Let’s say we want to know the value of the fourth position (remember, it starts with 0 so the fourth position is 3) and write it into an other variable.

a = nums[3];
Arduino Array compared with a container ship get value

In the last unit, we played different tones. We had to repeat almost the same commands again and again. Now, we can simply put the frequencies into an array:

int frequencies[] = {262, 294, 330, 349, 392, 392);

Easy! We have an array of the variable type int. It is named frequencies and the frequencies we need are already assigned. OK, let’s see what is where:

frequencies[0] returns 262
frequencies[1] returns 294
frequencies[5] returns 392

Cool, we can use the array like a normal variable. We only have to state the position.
For a melody we even need the duration of the tone. Let’s put it into a second array:

int tonDuration[]= {200, 200, 200, 200, 400, 400);

The length of both arrays must be the same. To play one by one we will use a for-loop. The counter variable will count, which tone should be played and sends it to the arrays:

int frequencies[] = {262, 294, 330, 349, 392, 392};
int tonDuration[]= {200, 200, 200, 200, 400, 400};
int toneQuantity = 6;

void setup() {

}

void loop() {
  for (int theTone=0; theTone< toneQuantity; theTone = theTone +1){
    tone(9, frequencies[theTone]);
    delay(tonDuration[theTone]);
    noTone(9);
    delay(tonDuration[theTone]);
  }
}

This code is much shorter. Now, play the whole melody of the last unit. Here is the final solution:

int frequencies[] = {262, 294, 330, 349, 392, 392, 440, 440, 440, 440, 392, 440, 440, 440, 440, 392, 349, 349, 349, 349, 330, 330, 392, 392, 392, 392, 262};
int tonDuration[]= {200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800, 200, 200, 200, 200, 800, 200, 200, 200, 200, 400, 400, 200, 200, 200, 200, 800};
int toneQuantity = 27;

void setup() {

}

void loop() {
  for (int theTone=0; theTone< toneQuantity; theTone=theTone+1){
    tone(9, frequencies[theTone]);
    delay(tonDuration[theTone]);
    noTone(9);
    delay(tonDuration[theTone]);
  }
}

Leave a Reply

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