In this section, you'll learn about using 'while' loops and how to use them to change the structure of your code.

Transcript

While loops are a fundamental part of programming, as they allow a section to run continuously while a condition is true, and will break out of the loop when it is no longer true or when break is called. Unlike if statements, they will run over and over again for as long as the condition is met, and are really handy if you don't know in advance how many times it should run. The void loop function that you write the body of your code in that you're probably used to by now is nothing more than a fancy while loop. As with an if statement, you use while and then put the condition within the brackets, with the code entered within the following curly brackets. For example, you can replicate the void loop function simply by using while true or while one, which creates an infinite loop that can only be exited by calling break.

Let's have a go at implementing a while loop within the Arduino IDE. As you can see here, the code is quite simple, very similar to our last example for if statements. We've got our two pin definitions, we're using pin 13, I'm just piggybacking that particular pin 13 to use a red LED so it's a little bit easier to see on the top-down camera. And some global variables, we're using toggle state and button state, and button state is going to initialize as 1. There's a reason for this, so we're going to be looking at a slightly different method, a more efficient method for toggling an LED or blinking an LED, and we're going to control the blink rate of the LED depending on whether the button is being pressed or not in a really efficient manner. We're going to set up again the familiar pin mode setups, and we enter our loop where the important things happen.

So button state we've already defined here as 1. Button state is equal to digital read button pin, and that just means the button state naturally because we're pulling the pin up is going to start at 1, and then when we read it, it can change from there. Similar to the if statements, we're using the double equal sign as a relational operator. While button state is equal to 0, we run the following code infinitely until the condition is no longer true. So what happens when we're holding the button down? Toggle state is equal to not toggle state, a handy trick we learned for toggling the state of a binary variable or Boolean data in the previous video. It's going to change from 1 to 0 each time it iterates through that loop. Then we're writing the value of toggle state to digital write with the effect each time it runs through, it's going to turn on or off and flip that, but it saves us having to write digital write high, digital write low, saving us some code and programming resources.

Then we delay for 50 milliseconds, and then we check again to see what the state of the button is, because if we didn't, it'd have no way. So we've checked the state of the button here, but inside the while loop, if we don't check for the button, it's going to have no way of knowing if the state of the button changes to break out of that condition. So we have to constantly check to see if the condition is true by reading button state, and then it'll go again, check if the condition is true or false and proceed accordingly. Now what happens if we're not holding the button in? Well, of course, the code within the while loop cannot run. It doesn't enter the while loop code at all. So it simply bypasses to this, which is the same thing. It's just toggle state, flips toggle state, then digital write, LED pin, toggle state. But this time we're going to delay for 200 milliseconds.

All this means is that the LED is going to blink at 200 millisecond intervals on and off every 200 milliseconds while the button is not being held, and when it is being held down, it'll blink much faster, and then when we let go, it'll exit the loop, continue to blink slowly. The advantage of using toggle state to control our LED is if we didn't, if we just did digital write delay, digital write delay, there's two sets of delays that we have to wait for in order to read the button state, which is going to introduce extra latency in breaking out of that loop or entering it or vice versa. By simply using toggle state, it allows us to check it much more frequently. Really, really handy. So let's go ahead and upload this code and we'll see how it works on our Arduino board. So it'll compile the code first, and done. Fantastic. You can see the LED is blinking nice and slowly there. If we push the button in, it'll start blinking really fast, really quickly. We let go, blink slow again. Very, very handy.

Now, of course, if we ever want to break out of a while loop, as we mentioned before, we can use break. Break is simply spelled correctly with a semicolon. What that does is if you're inside of a loop, it doesn't work for if statements, but if you're inside of a loop, a while loop or a for loop or be it whatever, you can break out of that and it will exit that loop up to where it's at. So if we called break before the delay, that delay would not come into effect. It would simply exit out of the loop and go back to wherever it was from. Fantastic. That's how you can use breaks to control different loops as well. Next up, we're going to take a look at for loops, which are another form of loop that we can use to control the flow of our code.

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.