I would say that arrays are one of the most useful programming tools available.
If you’re 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 in arrays. In this lesson, we’re going to learn how to iterate through, an array in the Arduino programming language.
Arrays
All right, so let’s start off with the code.All right, so what do we have here? Well, the first thing I did is I created a sample array. This array holds floating-point values. The name is called “sensorReadings” and the size is four. That means it can hold up to but no more than four values. The limit or size, of this array is established when you first create it. Next, we initialize it with four separate values.
Now, you don’t have to put in the values when you initialize it but in this case, we have. So I’ve got four floating-point numbers that I initialized it with. For this lesson, it’s important for us to understand, what the index of these numbers are. So let me just write that out above this, so we can get clearer. So the index value refers to the position of the element in the array. So now we’ve established that the index values are zero, one, two, and three. Next is the setup.Setup only runs once. I’m using the Serial.begin function. I set that at 9,600. That’s the baud rate, and this will establish serial communication between the Arduino and our computer. Finally, we have the void loop.The loop function in Arduino runs over and over and over again. Inside void loop we have another loop called a for-loop. For-loops are an extremely common programming structure. You’re gonna find them in plenty of languages, and today we’re going to use a for-loop here to iterate through our array. Before we jump into this for-loop, let’s print out every single one of these values to the serial monitor window. This is what it might look like if we didn’t use a for-loop.
So what I’m doing inside the loop here is I’m using the print line function. This will print a value out to the serial monitor window, and I’m calling the array that we made, sensorReadings. So we have sensor readings, and we’re passing the index corresponding to the value that we want. The first item in the array is indexed at zero, or zero-indexing. Let’s open the serial monitor window, and see what the code gives us. Okay, so we can see it printing right here, and we’re printing off those four values, and since we’re in the loop, it’s gonna happen over and over again. But we want to get rid of this whole hard-coding thing, thus the for-loop.
Our for-loop with do essentially the same thing. It’s going to index every single one of these numbers, and then it’s 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 first, we initialize a variable that we will use inside the for-loop.This part only happens once, right? And what we’re 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. It’s called i, and it’s set equal to zero. So that gives us this value i. I is just a very common name. The next thing here in the for-loop is called the condition. The condition determines how many times the for-loop is gonna execute. So our condition here is saying, i, less than four. 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, that’s the last part of the for-loop. This is called the increment, and what we’re doing here is we are adding one to i. This i++ is, basically, saying i plus one. So we could say i is equal to i plus one. But a short way of saying that is just saying i++. When i is equal to zero.” The next thing we do is we check the condition and if it’s true, we are going to execute the code. Next, when we’re inside our for-loop we see Serial.print line.So we’re gonna print something off. We print off our array, right? SensorReadings is what we’re printing, but what index value are we passing? Well, we’re passing a variable, i. After that, we get to the end, and now, what’s the next thing we do? Well, that’s where this increment thing comes in…
For an in-depth visualization of this code in action, check out the video at the top of this lesson! Thanks for watching.
Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?