Arduino Workshop - Chapter Three - WHILE Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you [Music] while loops are a fundamental part of programming as they allow a section to run continuously while ever a condition is true and will break out of the loop when it is no longer true or when break is course unlike if statements they will run over and over and over and over again for as long as the condition is met and they're really handy if you don't know in advance how many times it should run for 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 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 let's 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 we've got our two pin definitions using Pinto 10 I'm just piggybacking that particular Pinto 10 to use a red LED so it's 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 there's a reason to difference so we're going to be looking at slightly different method a more efficient method for toggling an LED or blinking an LED and we're 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 that's Amelia pin mode setup and we enter a loop where the important things happen so button state we've 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 we're 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 we're 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 we're 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 it's going to change from one to a zero each time to interact through that loop then we're writing the value of total state to digital right with the effect each time I run through it's going to turn it on or off and flip that but it says it's 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 didn't it it have no way to check the state of the button here but inside the while loop if we don't check the bun it's 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 we're not holding the button in well of course the code within the while loop cannot run it does it doesn't enter the while loop code at all so simply bypasses to this which is the same thing it's just total state flip toggle state then digital right LED pin toggle state but this light time we're 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 it'll 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 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 total 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 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 you'll 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 you're inside of the loop it doesn't work the if statement but if you're 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 it's 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 what's wrong fantastic that's how you can use breaks to control a different loops as well so next up we're going to take a look at four loops which are another form of loop that we can use to control the flow of a code
Info
Channel: Core Electronics
Views: 60,562
Rating: 4.8007665 out of 5
Keywords: arduino while loop, how to use while loop in arduino, arduino how to use while loop
Id: Wd4e4AFvqkM
Channel Id: undefined
Length: 5min 9sec (309 seconds)
Published: Tue Mar 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.