Arduino Workshop - Chapter Three - IF Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if statements are the backbone of decision making from microcontrollers and systems they allow you to end up or bypass a section of code based on a condition or set of conditions this could be as simple as checking to see if a variable is a 1 or a 0 or a more sophisticated condition such as determining whether an array is sequence correctly if statements allow the exist then that mentality so it will take a look at how to use them in the Arduino IDE we're going to be turning a simple momentary button that we've used previously into a total switch using codes toggle switching allows for more complex tasks and doesn't require a huge amount of code it's a good way to get started with using if statements as highlights some of the decision making elements to consider when using if statements not to mention that having a method for creating an efficient toggle switch is a great addition to any program of toolkit along with if statements will be introducing a couple of new elements such as variable modifiers and the very useful Milly's function so let's take a look alright so let's take a look at the code behind it it's fairly straightforward and it follows some of the styling from previous code examples we have our pin definitions at the top where we're using LED pin as the onboard led pin 13 and button 10 - 10 - you can change this to whatever you like of course if you want to use an external LEDs that's fine too some global variables here is we're using the variable total state which will help keep track of whether our switch or our total of state is a 1 or a 0 on or off the last button state is so we can help remember whether the button was large pressed or released long unsigned in now this is an unfamiliar one you can see ins is quite familiar but this would be here long unsigned what's that all about well that's called a variable modifier long essentially doubles the length of the in doubles the size of the contains that have only been able to store 2 bytes it can store 4 bytes of data in there which is really really handy and unsigned touches on something that we haven't actually covered yet unsigned removes the possibility to negative values within the integer you see within an int or any variable for that matter is assigned variable let's say it's capable of storing ten values for simplicity sake you can store eight bits worth of data or sixteen bits worth of data let's take ten you can still attend bits of data all up then half of those are going to be positive and half they're going to be negative which means you can only count two for perhaps five depending on how the system works what study you can only count up to that far and then the rest is a negative variable but we're going to be working here with time and we're never going to have a negative time but it could be the possibility that we don't press the button for such a long time it goes into that extra extra length of time that we've got so it's the equivalent of doubling again the length of the variable that we can store so that's long unsigned in very useful if you know you're only ever going to be dealing with numbers greater than or equal to zero and then again a standard integer variable is debounce time which we're going to be using for DB d bouncing our switch and we'll get to that a little bit later on so we've got void setup here pretty familiar up in modes to set our LED pin and button panners output and input pull up as per the previous chapter and then in the loop this is where we get to the business end of the code and that's not as complicated as that first look so some comments you to help you sort through what's going on so the first thing we're doing in our loop is we're going to read the values of London store as a variable pretty standard we've done it in the previous chapter heap so now that we know every iteration of what our button is equal to now we've got two sets of if statements that we can separate a little here we've got this one and this one you'll see they're actually nested in yet another if statement so we'll separate those a little bit more so this if statement here is the state or the sequence of events that will happen if the button is pressed if the condition is true that it suppress and the conditions here for if the button is released and then observe this we have another if statement here which is using this function called Milly's which squeezes we're not familiar with yet so what is real is well Milly's is a software timer that is specific to arduino it's an Arduino expunction and what it does is it records the amount of time that just transpired since buuut or since the program was initiated so you can use Millie's to keep track of time and specific things and use that update other variables of software timer it's very very useful so what we're doing here is dealing with a property in switches called debounce or a phenomenon called debouncing or we're solving the bouncing issue using zoom they're bouncing if you work so with a button it's covered more in the all about switches tutorial on our website to take a look at that but simply put a button like any mechanical object has inertia which means when you press it whilst the contacts make contact with our with the other side there's a little bit of bounce in there because it travels here bounces and finally settles the bigger the switch the heavy as a switch the more debounce there is going to be in that as well so it's something to keep in mind now ordinarily if you want to use this with an LED it would be happening so quickly we never noticed but in microcontroller does it's running fast enough and because we're using it as a toggle switch it will actually register all those individual little bounces that might be happening you know less than 5 milliseconds apart in total it will register those those individual toggle presses which is not good we only want to trigger when we truly press and activate the button so we're using a method that is similar to a low pass filter if you're familiar with that and all it means is we're getting rid of the noise the high frequency noise that's happening and we only want the events when we want to do something on the events that are happening at a lower frequency we're passing the lower elements of the signal so what's happening here well we've got an if statement now the if statement has two different or a few different things in here actually we've got a set of brackets which you use for separating different elements so we can see here that we're grouping together Milly's which is the time since but-- and then we're subtracting the value last press from and if it's a little bit complicated don't worry just reread it and it will eventually get there but it's a really powerful use of if statements so Millie's - last press now last press initializes at zero because we haven't defined it so it's going to be zero so Millie's let's say that we run our program 10 seconds before we press the button that means the Milly's is equal to 10,000 milliseconds to the thousand milliseconds to a second so 10,000 let's do this high particularly 10,000 we can put that in that - zero is greater than the debounce um so this the same is if this number is greater than this number then we're going to go into the code that follows the if statement now if it's not we're simply going to bypass that whole section of code and keep going or what this allows us to do is we can use this to track how frequently a button is being pressed and if we print it we don't want to do anything with it until an amount of milliseconds has transpired so we can ignore that debouncing noise or bouncing noise I should say so we've got Millie's - last press and you can understand a bit it'll make a bit more sense once you dive into it but we'll take that that you know where we're frequently checking to see if a condition is true then if it is if the time if Millie's - the life press is greater than the debounce time so enough time has transpired to the event to occur again then what are we doing here we'll last press is equal to Millie's what this is doing is it's just recording the time that the button was pressed and it's recording that time in Millie's Millie's constantly updating so now if the while stress is equal to millions so let's say this was to all happen again in almost instantaneously Millie's - millions which are almost the same things or so last press - that is is equal to Millia so it's almost the same thing is greater than the debounce time off it's not greater indeed BAPS time is equal to 20 so it needs 20 milliseconds to occur between presses in order for us to do something so that's the debouncing there it's quite simple but really really efficient as far as ID we know goes because we don't have any inline delays which chew up our process then in here as I said we've got these two levels of their statements which is a really important part so the heavy-lifting which control the total action so what do we have here if button state is equal to zero what's that using the double equal times that means is it equal to so we're using some operators here and we cover operators more thoroughly in the next chapter but these relational operators are used to check if one value is equal to or if it is greater than or if it is greater than or equal to different conditions like that so if button state is equal to because a single equal sign defines a variable makes it equal to something whereas double equal sign checks so if one state is equal to zero if it is currently being pushed down because it's active low logic and the double ampersand here means and so both of these conditions has to be true in order for something to occur we could use an or relational offer our logical operator sorry which would be if one of them is true that's okay but if both of them has to be true because the reason for this is so if Sutton is being press low and the last button state is 1 so that's checking if it is being pressed and the last time an action was recorded as it's being released because otherwise we would just hold it down in that event would continually repeat and repeat repeat so we need a way of saying only when it's a push and the one that has been released and then we're pressing it again now what do we do here we have a variable called toggle T which initially is equal to zero so total state is equal to zero now we've got this funny-looking statement here toggle state is is equal to question mark sorry exclamation mark total state what's the go with that well what we're using here is another logical operator the exclamation mark is called do not so what we're saying is the toggle state is equal to not toggle state now we could put that exclamation mark there that'll work exactly the same way what that's doing is total state is either going to be one or a zero because it's just an on/off binary toggle switch which is saying toggle state is zero well total state now is equal to not zero which is a one if double state is one is equal to not one so it's a zero just flips or invert toggles that values fantastic so we toggle that and then we're writing that directly to our LED pin which means every time a press is registered it will turn the LED on equal to total state every time precedence flips toggle state so one press it will be on the other types of you can put a different event or you know function in here but we're just toggling an LED for now you could use it to account button presses for example and then we're recording the last button state which is important we're telling our program yet a press has been recorded the next event has to be a release in order for another press to be valid and then it will exit that loop sir that's throw that statement the if statements there when it gets to the end of the curly bracket all of your code goes inside the curly brackets want to finish that unless it's been told otherwise it will keep flowing through the program and it gets to here this button state is equals 1 well it's not equal to 1 because we've just said it's equal to 0 because it's been pressed and that's happening so quickly there's not enough time to press it and then release it in order for those two votes to come true and the last button state is equal to 0 which means it is not being pressed if it is open which is right now so we need another condition to make sure it's only open after a press so if it is equal to 1 if it is hi if you do not been pressed and the last button state equals 0 which means it is just being pressed and is now being released then all we need to do is put last button state is equal to 1 so plus 1 plus 1 so that we can reset our condition to register the press so as you can see if statements are really useful really powerful Milotic statements that we can use to control the flow of events so let's wire up our circuit and test out our total switch code and see how it goes so first of all we are going to need a push button that's all we're going to need because we'll be using the onboard led kind of course wire up an external LED as per our previous example if you like but I'm just going to be using a button I mean the 2/3 is meant to wires for this particular example it's just using circuit examples that we use the fourth they check out the previous chapters for halitosis working some wiring diagram and let's connect that as we know ops were computers we can upload this program taking your USB cable all right now let's go ahead and apply this making sure you have your board type and comport selected upload what have we got to cut an error and that's because we left in the ESL we didn't include the correct bracket so we'll delete that and it's going to work exactly as intended now very good beautiful done uploading so let me push our button at LED on board turns on oh you can see that very well factor might just water up an external LED really quickly cities and see little bit the mistaking the LED filing it up here okay you just go I'm going to take some negative negative led to ground up our our previous examples I'll just use solid ground PBS cold type things jump leads start to insert then I'll just talk it straight into inside inside add multiply any of the code and there we go you can see that LED bit will clearly to move and push our button it turns off and it's running bubbling press on press off press on profile very useful very cool bit of code so that's a little bit about how your statements work if they seem a little bit complicated don't worry and take a little while to wrap your head around but I consider this such a fantastic example of your statements because been able to create a toggle switch out of a simple push button is so invaluable everything from creating user interfaces to all sorts of projects which requires tactile input are really important they're going to rely on being able to just register the press of a button and perform an event such as a total switch so that's a bit about if statement you can see that we've got is a condition is true run the section of code if the condition is false bypass the section occurred in the curly bracket and continue on with whatever else the program was doing now there's one more thing - if statements which we haven't quite covered here and this is is else so else is a really handy statement as you think if allows us to say if the condition is true then run this code we've got another if statement here saying ask if this is true run exception code but there could be a situation where both of these are true which is when we don't want them both to run in the same loop which is where else comes in so we use else exactly the same as an if statement except there's nothing inside the brackets so if we were to go down here and create another else then now elephan this says if all of it has to be preceded by an if statement it says it's a if statements before it if none of those or if there's only one if that one's not true but say both of those are false then let's perform some out so it's sort of like a default of backup option where if none of the other conditions for any of the if statements before it in the current level are true else let's do something completely different and you can use else which is as well so you can use if else what's their size is it says well if these conditions are true the else so we can't run both of these else that has to not be true so if that if statement is true it will automatically cancel out that one so only one of them can run really really handy I put a quick reference guide all missus source codes is on the text file for the workshop that's up on the web page there you can go tackle and reread it and get a good understanding of how if statements work because they're incredibly powerful and we're going to keep and different logic statements in the next couple section
Info
Channel: Core Electronics
Views: 44,608
Rating: 4.8711987 out of 5
Keywords: how to use if statements arduino, arduino if statements
Id: raJjAlNqExQ
Channel Id: undefined
Length: 17min 5sec (1025 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.