Bits and Droids logo
Mail icon

Arduino programming basics: Arrays

3/30/2024 | by Bits and Droids

If we could do something, it doesn't mean we should do something. Let's paint a scene to get the brain mass going. What would be an efficient way to add everything in your room to your program through variables? A: Create a variable for each item

B: Create categories and store each item in their respective category

We could get away with both solutions when we only have three items in our room. Option A would be the most logical if it were different objects. Now, if all the items were pencils, it would become more logical to choose option B. Or what if we have hundreds of items? pencils, it would become more logical to choose option B. Or what if we have hundreds of items?

Well, this is where I would introduce an Array. An array is a collection of values stored in a single variable. The array could be seen as a cabinet for our values. Let's take a look at how an array is created in C++.

cpp
int arrayName[5];

In the code above, we've created an array with the name arrayName, which can store 5 values. Because we started our line by declaring an Int our array can only store Ints. We can change the type of values stored by changing the declaration at the start of the line.

cpp
String arrayStrings[5]; double arrayDouble[5]; int arrayDouble[5]; float arrayFloat[5];

The number 5 between the square brackets refers to the size of the array. Imagine yourself shopping in Ikea. You can't tell the salesperson you need a closet with a drawer for everything you have without telling him/her how many things you have. The same goes for arrays. We can choose to either tell our program the number of drawers we need or tell the program how many items we have. This would look like this:

cpp
int arrayIntSize[5] = {1,5,8,9,4}; int arrayIntNoSize[] = {1,5,8,9,4};

Both lines will result in an array with space for 5 ints containing the same values. It's also possible to do this:

cpp
int arrayIntSize[5] = {1,4,8};

We declared that the int array "arrayIntSize" can hold 5 values, but we only provided three. C++ will append the missing values with 0's. If we print this array, the result will be 1,4,8,0,0. When working with arrays, it's important to remember that you need to declare a size. If the size is set, it can't be altered at a later stage. When designing your program, it's important to plan out the needed size (by perhaps including some overhead). Perhaps you've already developed a real programmer's mindset and started to think about different possibilities. For instance, a workaround could be to create a new array where you copy over the old array but increase the size.

Accessing data in an array

Knowing how to retrieve data is always handy when you store data. With arrays, we can access each entry individually by referring to the element's index. When we access the data in our array, we start counting from 0. So, the index of the first element is 0, and the last element will be the number of values -1 (since we start at 0).

We're also able to set data the same way:

cpp
//if we want to change the second value, we could do intArray[1] = 55; //another scenario could be int intArraySecond[5]; intArray[1] = 2; intArray[0] = 4; //The values stored in intArraySecond are now 4,2,0,0,0 (c++ automatically assigns the 0)

Its important to note that if we don't include the type at the start we're accessing data from an array, but when we do add the type we create a new one.

cpp
//With a type declared (so new array) int intArray[3]; //Without a type declared accessing data from an existing array intArray[2];

Another dimension

Arrays can be multidimensional. A multidimensional Array consists of multiple indexes with their values. This could also be seen as an array in an array.

cpp
String stringArray[2][2] = { {"Hello", "Hi"}, {"Goodbye", "Bye"} }; //if we would like to access Goodbye, we could do Serial.print(stringArray[1][0]);

We could add as many dimensions as we like. However, keeping the dimensions constant could take up a large sum of space. In most use cases, 2 is the maximum number of dimensions you should aim for.