How to "walk through" aka iterate, through an array in Arduino
Every programming language that im familiar with at least, has some form of an array. If youre trying to learn a new programming language, one of the things i would recommend is to get familiar with how to write and manipulate data and arrays like. If i was going to learn a spoken language like russian id want to prioritize learning the most common words and phrases first. Well, if im learning a new programming language, i am going to spend a ton of time learning how to use arrays in that language, because theyre, just so fundamental in this video youre, going to learn how to go through. That is how to iterate through an array in the arduino programming language. If you can do it in the arduino language, then you can also do it in the c and the c plus plus language, knowing how to do. This opens up a ton of useful possibilities for you as a programmer and id argue its really an essential thing to learn about any programming language that youre interested in using stay tuned. Music subscribe to our youtube channel to get more videos like this all right before. I jump in, i just want to say a huge shout out to altium for sponsoring this video. If you check out the description, you can get our link to a free trial of their software all right. So here we are in the arduino, ide and im going to write a little bit of code and then were going to talk about it.
Basically, this is what im going to do. I am going to make an array and im going to say, like you know what this array is going to be filled with sensor readings from some sensor like maybe maybe my device is getting a packet of information from some other device right and i have no Control over that other device im just getting a series of inputs from some other device and so im going to make an array that kind of like mocks out that input and then what im going to do is im going to iterate through each one of those Inputs and then well print it out to the serial monitor so lets. Do that all right? So what have i got here? Well, the first thing i did is i created an array, and this is just like a mock up array. It could be, you know whatever, but this is an array that holds floating point values. The name is called sensor readings and the size is four. That means it can hold a total of four values. It can never hold more than four like the limit. The size of this array is established when you first create it, so the size is four and then i initialize it with four separate values. Now you dont have to put in the values when you initialize it, but in this case i have so ive got four floating point numbers that i initialized it with now for this discussion.
Whats important for us is, we understand like what the index of these numbers are. So let me just write that out above this, so we can get clear. So the index value refers to the position of the element in the array. Now, if youre wondering like hey, what is this index thing, i dont even know how to make a raise. We just released a video not too long ago about this exact type of thing called arduino arrays. You can check that out to kind of learn what some of this stuff is. It seems a little foreign okay. So now we know what the index values theres, 0, 1, 2 and 3.. Now lets kind of move on to the rest of the program here. So weve got setup. Setup only runs once im using the serial begin function. I set that at 9600 thats the baud rate, and this is going to establish serial communication between the arduino and our computer and then weve got void loop. The loop function in arduino runs over and over and over again thats kind of where you put your main code and inside the loop. We have a another loop. Its called a for loop. All right for loops are an extremely common programming structure, youre going to find them in every language that im familiar with and were going to use a for loop here to iterate through our array, but before we jump into this for loop lets just say i wanted To print out every single one of these values to the serial monitor window, like what would i have to do if i didnt use a for loop? What would that look like well lets just code that up real quick? Do you need a printed circuit board design software to move your prototype to the next level? All team 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 its a super powerful tool at the same time, its really intuitive to use theyve got helpful. Video tutorials built right into the software, so you can kick start your learning process and actually get something made right now you can get a free trial to ultim designer, with our link in the description, thats right, you can test drive this super powerful software with the Free trial just check out the link in the description, so what im doing inside the loop here is im using the print line function. This is going to print a value out to the serial, monitor window and im, calling the array that we made sensor readings so heres sensor, readings and im passing the index corresponding to the value that i want to get so. The first item and array in an array is indexed at zero. Thats called zero indexing. You might think like well hey if its the first item shouldnt it be one well its, not one its zero all right. So i pass in the value, zero and that should print off the number one point: two three then next i have to use another print line, serial readings and i type in the number one. This is again the index number. This should give me the second element in this array, so it should print off 3.44 and so forth and so on. So the last element in this array is indexed by three all right and then i just have a little delay in here to kind of slow.
It down im going to go ahead and open up the serial, monitor window and lets uh lets see what we got here. Okay, so you can see you know, we start heres a print right here and were printing off those four values and since were in the loop, you know its going to happen over and over and over again, but this whole hard coding thing is: what kind of We want to get rid of. We dont have to mess with this and thats why we are going to use a for loop, okay, so the for loop is going to basically do that same thing, its going to index every single one of these numbers and then its going to print off those Items a for loop has three things inside of these parentheses. The first thing is called the initialization section. The second one is the condition, and then the third one is the increment. So here we initialize a variable that were going to use inside the for loop. This part only happens once right, so this is really weird inside here and its its hard to kind of wrap your head around. It seems so straightforward if youve worked with for loops for a long time, but the idea is, it gives you a place to say: hey, which variable are we going to use inside of this for loop and what were doing is initializing a variable called i, and We set it equal to zero, we say hey, this is an integer, the name of the integer is i and we set it equal to zero, so we have a variable, its called i and its set equal to zero.
So that gives us this value. I the letter, i theres nothing special with the letter i this could be a k. This could be count. This could be whatever you wan na call it vodka it doesnt matter. I is just a very common name, its terse and thats. Almost what i you know. I always use, but not a requirement by any stretch, all right, so i is this value that were going to use to kind of run this for loop. The next thing here in the for loop is called the condition. The condition determines how many times the for loop is going to execute. So our condition here is saying i less than four alright. So if we said i is equal to zero right. So if i is zero, zero less than four that statement evaluates to true. So as long as this statement evaluates to true, the for loop is going to continue to run so in order for us to stop this for loop, we need a way to change. I right well, how do we do that? Well, thats. The last part of the for loop – this is called the increment, and what were doing here is we are adding 1 to i. This i plus plus, is basically saying i plus 1., so we could say i is equal to i plus 1 kind of same thing here right, but a short way of saying that is just saying i plus plus super handy used all the time and its good And a very common thing to do is just add one in the increment, but you dont just have to add one.
Maybe we want to subtract one lets say theres a situation where we wanted to subtract one or maybe we want to multiply it times. Two. You know you can do whatever you want to. I you can do whatever you want in that increment, but a very common thing is to add one, which is in fact why they came up with this. You know plus plus operator, so i know thats, probably really. If this is the first time, youve seen that you might be like, i am really confused. I dont get this okay, so this is how its going to work when lets say lets pretend were the computer all right. We start this for loop were coming down, were like. Oh, hey, look, heres a for loop, the first thing i do, and i only do it once is. I say: oh all, right what whats this first thing here all right. I is equal to zero okay. So i is equal to zero. The next thing we do is we check the condition. So we ask all right: is i less than four well whats? I well hey. We just set it to zero right. So is zero less than four yep thats true. So if this condition is true, we are going to execute the code in here all right. So right now, what do we do when were inside our for loop? Well, the first thing is a serial dot print line so were going to print something off.
What do we print off? Well, we print off our array right sensor. Readings is what were printing, but what index value are we passing well were passing a variable. I hey wait. We just saw i what is i set at hmm? Well, remember: we set it equal to zero, it started at zero, so i is zero. So what should print off now? Well, we should see the zeroth element in our array. You know the first element, which is 1.23, so this should serially print 1.23 to the serial monitor window. Because i is 0., then i threw a little delay in here of a second just, so we could kind of see that happening, and now we get to the end of the for loop again remember were the computer were pretending. Some of us are pretending. Were the computer? Maybe you are a cyborg or whatever, so you get to the end and now whats the next thing. We do well thats, where this increment thing comes in now. This is going to add 1 to i so were going to take. I i was 0 whats, 0 plus 1. Oh hey, thats, 1., all right! So now we do our increment and the next thing we do is we check the condition. So now is i less than four well whats. I i was one is one less than four last time i checked thats, true right, so this condition is true were going to run back in to our for loop, so whats, the first thing we do.
Oh man, this is, you know we do a serial print again. What do we print sensor? Readings? Oh, but wait were passing i again, but what is i now last time it was zero, but now after we added one, i is one so now were gon na print sensor. Readings of one so now were gon na print. Whatever value is at index one. What is that? Well? Thats 3.44, so were going to print that out now we come down here. We delay a second. We come back up, hey were gon na iterate over i, so i was one we add one to one that gives us two. We check the condition. Yep two is less than four we come in here all right. I think you get the idea right, but what happens when we get to the the place where i is three all right so lets say i is three right now is three less than four yep it is. We print out sensor readings of three. That would be a hundred and eleven we delay now we add one to three and its. What what is it its four now we check our condition is four less than four no way jose. So that means this condition is false, which means this for loop. We are done with it adios senora, so we get we escape. This for loop were done with the for loop. We do like other code down here right and then in this case, since all we have is the for loop.
We get down to the bottom and we start the for loop over and we do this whole thing over again. So im gon na go ahead and upload this code. Im gon na open up the serial, monitor and then were just gon na kind of take a look at this. So you can see it and you know im not like totally making this stuff up, which is tempting, sometimes all right, so you can see were just printing these values over and over and over again and thats. Basically, how you can get through each item in an array now, in this case i mean you know this is kind of contrived, but what were doing here is a really common thing. Lets say we wanted to do something more uh, useful, well, whats. You know when i look at this data and i i see like okay, one point, two, three, three, four four five point: six wait a hundred and eleven doesnt. That seem odd. Maybe i know you know as a programmer in a the specific domain im working with that. Sometimes the sensor readings i get dont make any sense right, and maybe i have no control over the values that come into my program because maybe im like pulling them off the internet. But i do know that hey if a value is too big its like junk data, so maybe what i could do is i could check to see in my for loop if the value is greater than something and then set it equal to something else.
So let me do that kind of demonstrate what im talking about alright. So you see i added a little bit of code in here. What did i do so? Weve got the same for loop here right. It starts up here. It ends down here. Ive just got a bunch of serial prints in here and kind of some stuff im doing to demonstrate, but basically what we do is the first thing we do. Is we print off the sensor reading like we were before, but then we check each sensor reading. We say hey if the sensor reading at that index is greater than 10. We just want to set it equal to the maximum which is 10.. Maybe we want to set it equal to zero. Maybe we want to set it equal to like negative one so that we know like its a a bad piece of data, or maybe we just wan na, set it equal to zero, whatever we wan na set it to you know, we could do that here right, But we only do it if the value exceeds a certain number and then i just do a print. You know to say: hey. We changed it to this and then just some more print formatting and then i delay a second. So lets go ahead and upload this open up. The serial monitor all right so lets yeah. Well just talk through this real quick right, so it prints off the first one right, one, two three four thats fine, its not greater than 10.
Then the second then the third, but it gets to 111 right. So i would have been 3 is 111 greater than 10. It sure is, and so now we set sensor readings at index three. So this value right here we set it equal to 10. and then youll notice. That doesnt happen anymore, because weve weve changed this right so now that it is equal to 10 this, if statement isnt going to execute again. So maybe you have a function that filters out specific values from an array when you get new inputs or something like that, i think you get the idea. Basically, you can do anything. You want to the values in these arrays when you can iterate through it, and you can do it conditionally, based on some feature of an element in that array. Alright. Well, i hope that was helpful thanks a ton for watching. I really appreciate it again. I cannot stress enough how important understanding arrays are, if youre going to learn a programming language learn to use their arrays super helpful. Well, i hope you found that helpful. If you want to learn more about all this arduino programming stuff make sure to check out our training program at programmingelectronics.com, also thanks so much to ultim for sponsoring this video. If you want to get a free trial of an amazing pcb design, software check out the description, use our link, you can get a free trial of ultim designer also before you go, make sure to subscribe to the channel.