Arduino Workshop – Chapter Three – WHILE Loops

If you dont know in advance how many times it should run for the void loop function, that you write, the body of your code, in that youre, 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 this code entered within the following curly brackets, for example, you can replicate the void loop function simply by using while true or well one which creates an infinite loop. That can only be exited by calling break lets have a go at implementing a while loop within the Arduino IDE. So, as you can see here, the code is quite simple, very similar to our last example. For if statement, weve got our two pin definitions using Pinto 10 Im just piggybacking that particular Pinto 10 to use a red LED so its a little bit easier to see on the top down camera and some global variables, using total States and button state and but Instead is going to initialize as one and theres a reason to difference so were going to be looking at slightly different method, a more efficient method for toggling, an LED or blinking an LED and were going to control. The blink Raiders led, depending on whether the button is being pressed or not in a really efficient manner, now setup again, thats Amelia pin mode setup and we enter a loop where the important things happen.

So button state weve already defined here as one the button state is equal to digital red button pin and that just means the button state and actually because were pulling the pin up is going to start at one and then when we read it, you can change From there so similar to the, if statements were using the equals double equal sign as a relational operator, so while button state is equal to zero, we run the following code: infinitely until the condition is no longer true. So what happens when were holding the button? Well, total state is equal to not total state a handy trick. We learned to toggling the state of a binary variable or boolean data in the previous video, so its going to change from one to a zero each time to interact through that loop then were writing the value of total state to digital right with the effect. Each time I run through its going to turn it on or off and flip that, but it says its happening to write digital write, higher digital write, load, saving us some code and programming resources. Then we delay to 50 milliseconds and then we check again to see what the state of the button is, because if we didnt it, it have no way to check the state of the button here, but inside the while loop. If we dont check the bun its going to have no way of knowing, if the state as a button changes to break out of that condition, so you have to constantly check see if the condition is true by reading button state and then it will go again Check if the condition is true or false and proceed accordingly now, what happens if were not holding the button in well, of course, the code within the while loop cannot run it.

Does it doesnt enter the while loop code at all so simply bypasses to this, which is the same thing? Its just total state flip toggle state. Then digital right LED pin toggle state, but this light time were going to delay for 200 milliseconds, and all it means is that the LED is going to blink 200 millisecond intervals on and off every 200 milliseconds. While the button is not being held and when it is being held down, it will blink much faster and then, when we let go itll exit the loop continue to blink slowly and the advantage of using toggle state to control our LED is if we did. If we just did digital write delays, did you write, delay theres 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 total state, it allows us to check it much more frequently, really really handy so lets go ahead and upload this code and well see how it works on our Arduino board. So it will compile the code and done and top it. So you can see the LED is blinking, nothing slowly there. If we push the button in youll, start ranking blinky really fast, really quickly. We let go blink. Slow again. Very, very handy now is cool if we ever want to break out of the wall.

If, as I mentioned before, we can use break and break you simply felt correctly with the semicolon. What that does is if youre inside of the loop, it doesnt work the if statement, but if youre inside of a loop for a while loop or a for loop of beer, whatever you can break out of that, and it will exit that loop up to where Its at which we call 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 whats wrong. Fantastic thats how you can use breaks to control a different loops as well.

Share.
Exit mobile version