In this section, we'll look at using arrays to store data, which allows for more powerful variable manipulation.

Transcript

So far in all of the examples that you've created or looked at variables have been used in fairly simple ways, well we're going to take a look at another way to use variables called arrays. Arrays are a data structure which allows multiple values to be grouped together in an easily accessible method, if you can remember back to chapter two, we talked about variables and how they can be thought of as containers sitting on a shelf. Well to extend that analogy if a variable is a single container then think of an array as a subdivided set of drawers on a shelf. You can still access that space on the shelf but rather than being a single container there are different miniature containers at that space, an array is structured like so.

So, in the Arduino IDE and there are four key elements to an array, you have the array data type, in this example we're using “int” much the same as we would with another variable. We have “arrayOne” that could be called anything could be called “Sydney”, it doesn't have to have the word array in it, it's just a unique name in the same way that a variable has a unique name, it has the array index just there and then it has the array contents which we have here inside of our curly brackets and each element is separated by comma so there are four unique elements or pieces of data inside there, each of which isn't it.

Now you can create different types of arrays, however an array can only hold a single type of data so if you declare an array as an “int” then it can only hold integers, like the example there. Now like variables arrays can be declared first and defined later, which allows you to create an array with blank spots in it and add data to it later on in your program, the array name follows the same conventions as a standard variable it must be unique it is case sensitive and has a scope, global or local. The index refers to the position of the array context when declaring an array you can define the number of elements or places if you wish that you have in your array and finally the array contents is the data that is inside your array.

So, let's take a look at some examples of how you can declare and define arrays. So, adding on from the example here, I've created four different types of arrays we have “arrayOne” in which we said that we have four elements to the array and inside we have these four pieces of data 11, 5, 4 and 9. Now we have “arrayTwo” in which we've left the index blank, so we're not explicitly telling it how many elements are in our array but you can see that there are 9 elements here, in fact we can add a 0 to that 0 index it or in fact with 10 to the end there so the examples a little better for 1 through to 10. So, there are 10 elements in this array and once it goes through that the compiler will automatically sort out how many elements are in there and populate that yourself so you don't always have to define number of elements in an array, a little bit more efficient.

Now here in “arrayThree” we've said that there are 10 elements in the array, and we've left off the context, now that is the same concept if we're declaring an integer just a standard variable. We could go array “someValue” and instead of letting it equal something we're just declaring and defining it later on, where we just leave out the equal signs and the value and instead just put that semicolon there to terminate the one. Well in the same way we've said we've left out the equal signs and the curly brackets in the contents and it will populate an array with ten blank spaces in which we can go and fill in later on. Now arrays don't just have to be “ints” here we've got a “char” type array or a character where it uses the ASCII table and it creates character type elements and the character is 8 bits or one byte of data and we’ve said that there are six elements in the array we have “h-e-l-l-o”, now you'll notice that's only 5 we've left space because there is actually a terminating character in that array. So there are six elements to it, so arrays don't just have to be “ints”.

Now to access an array you use its name and the index number of the element that you wish to access. Something to keep in mind though is that as with many things in programming arrays are 0 index, this means that the first element in an array is indexed as number zero and the second element as index one and so on and so forth. Whilst it can definitely be a bit confusing at times it makes a lot of sense once you start getting used to it, so let's take a look at another example. Let's say in void loop, I wish to access part of an array, as with the standard variable you don't have to use the data type again you simply use the array name so let's take “arrayOne” then inside my square bracket here is where I use the index to reference a particular element in the array so if I say “arrayOne[0]”  that means I'm accessing that 0 index of the very first element in the array, index number 0 which is 11, so “arrayOne[0]” would equal 11, likewise if it is index 1 that is the second space in the array which is 5, 2 would be 4 and 3 would be 9 and you have 0, 1, 2, 3 which is 4 elements in total.

It’d be confusing as I said but you do get used to it, likewise I can access even though I’ve got the word “hello” in my array I can actually access each individual letter, so rather than taking a string for example and saying all right string some word equals “hello” load and I've only got access to the string as a whole without some extra string manipulation and data control I can simply say alright I want to find out what the second letter is in this array and I know that it's an E or I could leave that blank and fill in another word later on and then break it apart in the same method which is really cool. So, a really handy use in arrays that you'll see later on is we can create a “for” loop. So, we create a “for” loop, standard counting loop into I and let's say I wanted to use “arrayTwo” for output pins and I want pins 1 through to 10 to be output pins. Let's say while “i < 10; i++> a standard incrementing for loop, I could then use pin mode, “arrayTwo” and you might index, put the local variable “i” and that's really cool but we'll get to that in a second and we'll take a look at a more holistic example.

So, now that you know a bit about arrays and how they work, let's put them to good use in a practical example to make an LED sequencer. Using your knowledge of LEDs, wire up eight LEDs to your Arduino board using whatever PINs you choose however I use pins 2 through to 9 so 8 LEDs all up and we're going to make them do some cool things. So, I've got my board here, fairly simple I’ve got my LEDs with a resistor going to ground from them so I'm using a high logic or active higher logic I should say and then I'm connecting my ground to my Arduino board and the pin outs directly to the LEDs so when we apply a five volt signal to them they're going to turn on which is cool! So, here's a sketch and it's fairly straightforward, we've got an array here called LED array and I'm going to use these for the pin outs to reference which LED I'm controlling. So, the array contents are 2 sequentially through to 9 which corresponds to the pins, I'm using pins 2 through to 9 then delay time which I'm just using as a counter for how rapidly we want the LEDs to sequence then as I mentioned before, inside setup I'm using this “for” loop I'm using pin modes with the array indexes of “I” and I'm declaring them all as outputs, now what this, does let's go through the basics of this because it's a really good example of using arrays. So at first for this “for” loop if you know from the previous chapter when we're talking about logic statements I would create a variable and we've initialized “I” as “0” so the first time this runs “I” is equal to “0” so what it says is “pinMode (ledArray[0])” which corresponds to the first element in our array, so “2” now LED array is “0” is equal to “2” so it would put a “2” in place of that and then it would just define “2” as an output.

Likewise when that's finished I will increment again to “1”, now “ledArray[1]” is equal to second element there which is “3”, so it will replace all of that with a “3” and so on and so forth until they’re all done which is really cool because instead of writing out “pinMode” output eight times which is really clunky and bulky, two lines of code and two spaces for our curly brackets gets the same thing done and it's really handy because if we want to increase the size of our LED rate, we can simply add some more elements to it. Change the bounding of our “for” loop and then we can declare as many as we want which is really coo. So, that is our pin modes done, now we've got two separate “for” loops here. They’re really simple, all we're doing is we're going to turn the LEDs on in order until they're all on. Then we’re going to turn them off again in the same order and you can play around with the exact animation really easily by altering the flow of our “for” loops.

The first “for” loop here again really standard counting, we're saying that “for(int i; I <=7; i++) inside which is initialized to zero, again to make it easier you could say while “I < 8” going to accomplish the same thing. Increment I, standard counting we're going to write “ledArray[i]” so we’re referencing each pin and writing “I” which is going to be in order from element 0, 1, 2, 3, 4, 5, 6, 7 which is 8 elements al up, remember zero indexing which corresponds to 2, 3, 4, 5, 6, 7, 8 which is the LED pins we're going to turn them all on and wait for delay time which is equal to 150 milliseconds each time. So, there's a time for them to step up an increment, then when it's done that we're going to turn  them back off and wipe back down in the same way, using the exact same concept but we're initializing I7, running a while “I >= 0” and “D” incrementing “I” each time.

So, let's go ahead and load this up to our board, plug that in, make sure I have the correct comport selected and hit upward and you can see it's actually already running, I had this program on there just a moment ago. You can see the LEDs turning on and off in order which is really handy. Now again you could implement a different animation. We'll just copy this, place the “for” loop here and then instead of turning it on we're going to turn it off, so what this will do is turn them all on and then all off which will do a linear work. So, you can see that it's all turning on then, then start wiping off really cool. Arrays are really simple but really powerful tools and they're really good especially for controlling outputs where you might have a whole group of LEDs, now 8 LEDs doesn't sound like much but let's say you wanted to control a 64 or an 8x8 LED matrix well you can use arrays and rather than having to declare each pin by itself and the pin mode and accessing each one by cross-referencing which pin number you're going to, you simply create it as an array and access each element that you want so row one could be element 0 through 7, then Row 2 is element 8 through 16, row 3 is 16 and so on and so forth and you have a really logical easy to organized way of having all your pin modes and your where your pin outputs clumps together.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.