If, else if, and else in Arduino Chapter 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so today what I'm going to do is explain a few of the examples in Chris Odom's book on the if-else then statements and what I want to do is just kind of copy and paste in the examples and run over them and explain them quickly so you get an idea of exactly what they actually mean this kind of goes along obviously with reading the book but this is just another way to kind of examine it so in the first case what I've done is I've uploaded or copied his first sort of major example and what we're going to do here is you can see we have a number of if statements and in this particular case they're all on a single line you'll notice there are no curly braces you can do this as long as you only ask it to do one additional function or one additional procedure so in this case I have it on a single line and if this value is true then it's going to go ahead and print out this particular command here X is equal to five if I wanted to do something else like for example equate it to another variable well then I have to break this up use the curly braces and end and spread it out but we'll talk about that in a second the other thing to go over quickly is the comparison operators this is the most confusing one right here where people get my stuff on they'll simply say if X is equal to five because that's what they do in math but in the case of the computer world and C language we're not equating X to 5 we're actually asking if it is 5 we're asking a true or false question in which case you have to use the comparison operator or the double equal sign okay so in this particular case we put in X is equal to 7 okay the single we've equated the variable to 7 and now we're going to ask okay well is it absolutely equal to 5 is that true or false okay the exclamation point is not so in this particular case the answer would be false okay then in the way of the computer world since I don't have any any way for it to escape in other words there's no brakes or no else statements or anything else to sort of separate these if statements it's going to do them all in sequence it's going to go right down okay it is not absolutely equal to five okay then it asks the question well is it not equal to five well that would be true so would in fact put this out all right is X less than five well that would be false is X greater than five well it's seven that would be true okay and it would go ahead and act that one out is it less than or equal to five well that would be false and in this particular case is X greater than or equal to five that would be true so it acts it out so this one here gets called this one here gets called and this one here gets called okay but it goes through them all it never escapes and then it goes into a loop and if I change this to say for example five and run it we should see my output change here a fair a little bit okay it goes through the sequence X is equal to five okay and then it well it's not it's not not all right so it's going to skip that one it's not less than it's not greater than but it is less than or equal to five and it is true that it is greater than or equal to five so these all come out now through the miracles of modern digital recording I'm going to pause this and you'll watch the code change all right well now I've entered the code for example number two and of course I've called it up here in my main loop so it's going to skip one it's going to jump right on down to example two a couple things here I have added the skiff so I can get keyboard input and what we've doing here now is we got to look at an if/then block okay so instead of it being on a single line I've got multiple things I need to do I need to break it up okay that means I need to use the curly braces as if it was an independent function and if this value becomes true if rank is equal to its true number one in other words I'm not equating it I'm comparing it if it in fact is a 1 then the value is true and it's going to print out these three lines and then it's going to go down to the next one if it is in fact 2 it's going to print out these 3 lines if it is in fact 3 it's going to print out these 3 lines once again I have no way of escaping it's going to go right on down this one right after the other until it hits the bottom and it's going to loop back up because it's going to get called again in an infinite loop now let's run it and as we expect if I put in for example a 2 it is not a 1 it is not a 3 it's going to do the number 2 it's going to going to check it and then let's go down the second one it's going to check oh it's true it's going to do what it needs to do and then it is going to go down to the third one and check it oh it's it's not true I'm going to skip it so if I put in a 3 for example you can see here that it checks it oh it's not true so it skips it and it's not true so it skips it and it goes into the third one oh it is true and it does this and then it loops itself back over again okay so in literal difference between this one and the first example is in fact we're dealing with blocks so we have the curly braces I'm going to pause it again right now and let's move on ok an example 3 what we've done is we've expanded the capacity or range of options available to our if statement by adding an else okay so in the previous examples it was always determining whether or not it was true okay like for example in the one above if the rank was absolutely equal to or equated to three well that is and it would run the code in this case we've given it a choice okay we're going to do a simple one here we're going to flip a coin using a random generator I have put the random module in and you can see that it's going to make a decision based essentially on just one choice and it has two outcomes at the coin Previn six there's the heads and there's a tails and in this particular case if it chooses a zero we've decided to assign it heads all right and if it's if this is true it puts out heads but if it's false it jumps to the else and it puts out tails I don't have another if coin is equal to I'm sorry equated to one it does tails although I could in this particular case I've made a shortcut and it just says if it's true it's heads jump if not its tails alright outcome is the same the code is different this is just the beginning of a large expansion but you can see it's working just fine over here it's in an infinite loop it's running down and it's flipping the coin and it's determining whether or not it is heads or tails alright so I think that one's pretty straight forward let's move on to the next example and see how this expands out okay so here you can see we have as example four and what we've done is we've added to it a level of complexity we're still working with the if-else blocks notice there's only one if statement everything else is else--if else--if or else all right which is actually something new I shouldn't just jump to it notice in this case there was only if an else now we have ifs else ifs and else okay what you usually do and typically is you end up using the else kind of as your garbage pail it's your last choice if I'm doing something with sensor values or something in robotics oftentimes I'll have the else as if like something goes awry or something a funny answer that you didn't pops up you end up using the else statement as the catch-all for everything that you did or did not anticipate but in this particular case we're rolling a dice okay and of course there's only six numbers on a die one through six and what we've done here or what Chris has done here is he said if the die is equated to one but out of one okay well if that was true that would happen but it was false so it jumps down is it a - it's a false chime three whatever if it ends up finding that it is a 4 for example it'll print the four and it exits block it doesn't mean it doesn't keep going this is an important point it exits the block it's ready to go back up again okay and what happens if it ends up being not a one not a to none three and a four or a five well I don't have equated to six but we're only rolling a six-sided die so we can then assume that the last one is going to be a six okay so in this particular case I imagine if I set timers up on here which would be fun to do I would find that it'll make its decision that the time requires the output the data is going to be shorter depending on which number it rolls so if it was in fact rolling a four it would only go so far and it would exit the block if it was rolling a two it'll go so far and exit the block it doesn't need to go all the way down it's figured it out it's done with that decision tree so let's go on to the next one and we'll we'll move on this next example is remarkably handy okay 5a and 5b start with 5a in 5a we're making a decision with basically three different positions in this case what Chris has chosen a random number between 1 and 12 and we did to going to determine whether or not the number is small medium or large if it's less than I think how it was in the book if it's 4 or less it's a small number if it's 4 or 5 or 6 it's a medium number if it's greater than if it's 7 8 or 9 it's a big number or 7 mm greater than big number in practice where I use something like this a lot is let's say for example you have a sensor on a robot which is on a table and you're looking down at the table and what you want to do is you want to avoid an object on the table as well as you want to avoid falling off the table so that means if the sensor reads something in front of it okay that's probably an object and you want to avoid it you want to turn to the left or to the right the number and your sensor is probably getting larger because something is getting closer but if it reaches the edge of the table suddenly the number gets smaller okay because it no longer sees the SIRT the surface of the table and it has to make a decision based on something which is a very small number okay because it's up at some elevated height so how would you make that decision okay and this is where you have to start applying some logic and then you can do this in a couple of different ways in this particular case we're going to start sorting from the bottom up okay so if the number is less than five well that's a no-brainer hitting it and if that is true while the number is small okay and it exits a block if it goes on to the next one let's say that the number is six okay well the first question was false it skips it else if is the number less than nine well if that was true it's going to go ahead and print this out and say the number is medium and exit the block okay if the number was say 10 well it's going to check that that's false check that that's false and it's going to go down here into the bucket and say wow you know it's not this is not true and this is not true so the remaining false has to be the case and the number is big okay and it the reason why it never makes it down here for example if you put in a six is because it exits the block and then it repeats itself it breaks it and it's ready to do a new a new data set okay so you can see it's randomly generated number between 1 and 12 and it seems to be working just great okay it's sorting out the numbers and it's exiting the block as soon as the condition is met as soon as the condition is true exits the block let's look at the next one and we'll probably have to end this video okay so in the case of 5b you can see we're doing essentially the exact same thing it just were working in the other direction alright if the number is greater than 8 and if that value is that that condition is true well then we're going to run this little bit right here and it's going to type out the number is big and it's going to exit the block if this is false it moves on down and it checks another well you know is it bigger than 4 okay if it is it's going to say that this is true and it's going to type in medium all right and then it's going to exit the block and lastly if neither of these two are true all right it goes to the bucket and it decides the number must be small okay it's well hopefully it's perfectly logical when you think about it don't get tripped up notice one of the differences here is we've changed the value originally it was because we're looking for less than and greater than so you know we could actually use equals two statements but in this case if it was less than 5 and less than 9 and here we've got it greater than 8 and greater than 4 I'm going to end this and I'll see you soon
Info
Channel: Brian Patton
Views: 25,497
Rating: 4.8205128 out of 5
Keywords: pattonrobotics.com, Patton Robotics, robot, education, online classes, virtual classroom, Arduino, electronics, Programming, tutorial
Id: nXnQ-KY8B9w
Channel Id: undefined
Length: 14min 56sec (896 seconds)
Published: Thu Dec 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.