Using Arrays with Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Tutor] 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've got 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. 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. In this video you're gonna learn what an array actually is. You'll learn what you can put in an array, how to create arrays in your program and then how to access the items in an array and change the items of an array. Stay tuned. (upbeat music) Subscribe to our YouTube channel to get more videos like this. Before we start just a big shout-out to Altium for sponsoring this video. Huge thanks. You can check the description to get a free trial of the Altium software. All right, let's go ahead and get started. So what the heck is an array? Well, an array is just a list. It's like literally just a list of stuff. Like let's say you had to go shopping at the grocery store and you made a list. You'd be like, all right, I need an apple, I need a--, I dunno, some coffee and a rhododendron. I mean I don't know what you're shopping for. But you make a list of stuff it's 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 like more going on. And for that reason it's called a data structure. 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. All right, so what can you 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. So you can put whatever you want in an array as long as it's the same data type of thing. So let's get down to business. How do you actually create an array? Now we're in the Arduino IDE so we'll go ahead and write an array out here so you can see this. But roughly this is basically how it's gonna go. First, you're gonna say hey, this is gonna be the data type we're gonna put in our array. Then we're gonna give the array a name. Then inside brackets we can optionally put the size of this array. After that we set it equal to a bunch of stuff in our array. The stuff in the array are called the elements and knowing that terminology is somewhat useful because it allows us to talk about arrays and make sense to one another. So just to remember that each of the things in an array is called an element of the array. And each element is separated by a comma. And if I didn't mention it, they're stored inside these 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. If you don't write in Arduino often and maybe you write in other programming languages, it can be hard to remember that semicolon. One way to remember it is just to forget it and then you're gonna get an error and you'll get so annoyed by getting all the errors, you'll start remember to adding that 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. All right. So let's go ahead and create an array down here and walk through this. So again, we're in the Arduino IDE, I am going to make an array of bytes. So let's do this. Do you need a printed circuit board design software to move your prototype to the next level? Altium Designer is a great choice for designing PCBs, sharing your design with team members and even getting your design manufactured. What really kind of blows me away about this software is that even though it's a super powerful tool, at the same time it's really intuitive to use. They've got helpful video tutorials, put right into the software so you can kickstart your learning process and actually get something made. Right now you can get a free trial to Altium Designer with our link in the description. That's right. You can test drive this super powerful software with the free trial. Just check out the link in the description. All right. So I have created an array called pin numbers. So this is the name of the array. When I wanna refer to this array, how do I refer to it? Well, I say hey, pinNums. Okay. So what goes in pinNums? Like what data type? This bytes go in pinNums, right? Cause we have to specify what data type are we gonna be storing in our pin number array. Now how many elements will there, how much storage, like how much space is gonna be allocated for this array in memory? Well, it's gonna be eight because we're specifying the size right here. So the pinNums array at most can hold eight elements. And so what I've done then is I've put eight bytes inside of pinNums. I've got one, two, three, four, five, six, seven, eight. So I've put eight things in here. If I tried to put a ninth thing and I try to verify this, (foreign word) too many initializers. It says, "hey, the size you specified it as eight". "What are you doing man?" See you can't overfill an array. You'll get an error. Now what if I get rid of the eight? What if I get rid of the eight? Notice everything's still working. Now the size of our array is being specified by the number of elements we've initialized it to. So the size of this array is still eight and that's because we have eight elements. But let's say we got rid of the last two. Now the size of this array is only gonna be six cause we have six elements in here. Now check this out. What if I did 10, I've set this equal to 10. And then let's say, I put just six elements in here. What's gonna be the size of pinNums? Well, the size will still be 10 because that's what we specified here. But the other values are not initialized. We have not initialized those values. So in this case we can still add some more elements to the array until we get up to that size of 10. All right. So I'm gonna set this back to eight items. We've got eight elements in the array verify that, everything should be working. So that is how you create an array. Now if I wanted an array of floats, then I would need to make an array that was designed to hold floats. And I could put floating point numbers inside that array. Now if all this stuff I'm talking about this whole like floating point numbers, integers is just like Greek to you and you're trying to learn more, check out our training program at programmingelectronics.com. All right. Enough about that. All right. So that's how you create an array. Really not too much rocket science there. So once you've created an array, how do you like get an item out of the array? Well, you do this thing called indexing and all you have to do is write the name of your array, this square brackets and then you put the number of the index inside there. That sounds really weird and it's kind of funky. So we're just going to talk about this. It's really straightforward once you get it and it's super handy. So if we look at our array down here, each one of these items, each one of these elements rather has an index and the index is based on where they show up inside the array. So let me write something out here I think that will help us. So the index numbers that I've written up here refer to each element in the array based on where they are. The weirdest part is that the index numbers start at zero. Now you might be like, "man that's crazy". "Why don't they just start at one?" "Ain't one the first element?" Well, I'll just give you index one. Well, this is just not how it works. It's zero. So we just have to accept that it's zero and move on. So if you wanna get the first element in your array, number six, then we need to use the zero index. What do I mean we need to use the zero index? Well, let me set up a little print statement here and we'll print out these values to the serial monitor window just so you can see. (upbeat music) All right, so let's just set this to 99. So let me walk through this example here. So what I wanna do is I wanna print off index zero of the pinNums array to the serial monitor. So we started out in index zero. it's the first element, right? And this is the number six so I wanna print this off. Well, in my setup what I'm doing though, is I'm gonna go ahead and I'm gonna change pinNums of zero to 99 just to make things confusing. So how do I do that? Well, I use the name of the array pinNums, I've got these square brackets and then I put the index number inside here. So I'm saying whatever element is at the zeroth index, I am going to now assign it. I'm gonna use the assignment operator the equal sign. I'm gonna assign it to 99. So now this six, now it's a 99. Then what I'm gonna do down here is I'm just printing off the value to the serial monitor window. Now you might be wondering like what's all this stuff. Well, here I'm just starting to see your communication with serial begin. Then I'm printing out some helper text. So this is just says; "hey, pinNums at zero is gonna equal this." So this is just helper text to help us see what we're actually printing out. But this is what I want you to pay attention to. Now I'm just calling pinNums of zero. So again the name of the array, these square brackets and then a value. And then this index value is what's gonna print out. So it's like, "hey, I wanna know what's it you know index zero." Well, what is ed index zero? Well, it started out as six, right? But then we updated it. We changed it to 99 so we should expect 99. So let me go ahead and verify that, upload it, we'll open up the serial monitor window and there we go. pinNums of zero is equal to 99. If I change this to I don't know seven, upload it, 13 see we're at index seven. What if I change it to 22? Mmh, wait a second. We don't have 22 elements in this array. There's only a size of eight. What do you think is gonna happen? I don't know. Oh, it compiled just fine. It uploaded. Hey, it's uploading just fine. Let's see what's there. I'm opening the serial monitor window here. 155. What does that even mean? I don't know. what's happening here is we are referencing data that we did not assign previously. Now we're not gonna get into like pointers and all the memory stuff that's going on behind this. That's like for another discussion but basically we are actually looking at a piece of data further down in memory than we've actually stored. And so we have no idea what we've gotten here. So you gotta be careful. You don't wanna go out of the bounds of the array. You don't wanna try to access things outside the size of the array as you can see us letting us do it but that's a real problem. In this case we're just viewing something. But what if we actually assigned something outside of the memory of where the array is like that could really mess up the program. So don't wanna do that. Okay. Well, I hope you found that helpful. If you wanna learn more about all this Arduino programming stuff, make sure to check out our training program at programmingelectronics.com. Also thanks so much to Altium for sponsoring this video. If you wanna get a free trial of an amazing PCB design software, check out the description. Using our link you can get a free trial of Altium designer. Also before you go, make sure to subscribe to the channel if you'd like to get more videos like this. Leave a comment if you have any questions or thoughts about this video. And as always have a great day. Thanks so much. Bye. (keyboard tunes playing)
Info
Channel: Programming Electronics Academy
Views: 28,822
Rating: undefined out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy
Id: qLAVgrEo2qw
Channel Id: undefined
Length: 13min 51sec (831 seconds)
Published: Fri Dec 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.