Arduino Array Intro

Who hasn’t made a list of stuff, like a grocery list or to-do list?

When programming with Arduino, a list of stuff is called an “array.

Check out this lesson to learn about Arrays:

  1. What is an array?
  2. What can I put in an array?
  3. How do you create an array?
  4. How do I get an item out of an array?

Intro to arrays

Are you trying to use an array in your Arduino program? Maybe you’ve got some values in an array and you’re trying to print them out. Maybe you have some pin numbers in the array and you’re trying to get each pin to do something like turn an LED on or off or whatever.

arduino array intro

The bottom line is that arrays are really useful data structures. They’re common in just about every programming language. So learning how to use them is super important.

What is an array?

All right, so what IS an array? Well, an array is just a list. And lists can be really useful, right? Like a to-do list. An array is just a list of stuff. Now an array is a little more complicated than just say an integer. There’s more going on. And for that reason, it’s called a data structure. arduino array intro So an array is a data structure. It structures our data in a certain way and there’re certain rules we have to use when we work with that data structure with an array.

What can I put in an array?

Well, you can put anything you want in an array. But in Arduino or in C, it needs to be all of this same type of stuff. So if you’re gonna put integers in the array, you have to have a list of integers. You can’t have like an integer and then a floating point number and then a byte. Like they all need to be the same type.

How do you create an array?

Using the Arduino IDE, we’ll go ahead and write out an array.arduino array intro First, we’re going to declare which data type we’ll be using in our array. Then we’ll give the array a name. Then inside brackets, we can optionally put the size of this array. After that, we set it equal to the elements of our array (and knowing that terminology is useful because it allows us to talk about arrays and make sense to one another).  And each element is separated by a comma and stored inside curly braces.

Finally, at the end of the statement, you need to put a semicolon. In every line of Arduino code or C code, you need to end with a semicolon.

Now if you don’t specify the size when you are creating your array, then the number of elements you put inside the curly braces will be the size of the array. And that’s one important thing to realize with arrays in Arduino. You can’t later increase the size of the array. The size of the array is set once it’s created. You can’t make it bigger, you can’t make it smaller. You can only change what the elements actually are. Which is different from a lot of other programming languages where you can dynamically change the size of the array.

So in this example, we’re calling the array “pinNums”. And pinNums is using bytes as the data type. Remember we have to specify what data type are we’ll be storing in our pin number array. Now, we need to decide how much space must be allocated for this array in memory. We’re going to choose and specify eight in the brackets. This means that our array pinNums, at most, can hold eight elements. And so what I’ve done then is include eight bytes inside of pinNums. If I tried to put a ninth byte and I try to verify this, it will tell us we have too many initializers.

The IDE is like saying “hey, the size you specified it as eight”. If you attempt to overfill an array, you’ll get an error. So that is how you create an array. So that’s how you create an array.

How do I get an item out of an array?

So once you’ve created an array, how would we get an item out of the array? Well, we’re going to do what’s called “indexing”. We’ll write the name of our array, use the square brackets, and then put the number of the index inside it.arduino array intro

 

See video for an example of using our array to print values to the Arduino serial monitor.

AppLab Bricks open in background with actual brick

Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?

Arduino Ventuno single board computer - top side

New Ventuno Q Dual Brain Single Board Computer

AppLab Pip Install

How to Add Python Packages in Arduino AppLab (No pip install needed)

Arduino Power Section Schematic

Kit-on-a-Shield Schematic Review

Just how random is the ESP32 random number generator?

Just how random is the ESP32 random number generator?

Leave a Comment