Arduino Tutorial 13: Understanding Arduino If Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy dot-com and we are here today with lesson number 13 in our incredible new Arduino tutorial series where we're going to teach you to be an Arduino programmer in an engineering quiz person I started to say whiz kid but I know that there are some of you older guys out there following this following this tutorial so I hope all of you guys will learn something today very important lesson we are going to learn about Arduino if statements so I need you to pull yourself a big mug of iced coffee I need you to get your most amazing eel egg Oh super starter kit if you don't have one yet this is the kit that we are using for this entire magnificent set of tutorials look in the link below hook a brother up and order your kit there are 35 bucks and this is enough components to keep us busy for a long long time okay so what we are going to learn today is we are going to learn about we are going to learn about if statements but to do that we are going to go back to what we had worked on in Lesson number 12 which was a potentiometer so we're going to teach you about an if statement but we're going to teach it to you using a circuit in a little simple project based on a potentiometer and so if I come over here what I want to show you is I want to kind of show you a little bit of an introduction about an if statement and then we're going to come over here and build a circuit and do some coding and this is something we talked about very early on and it's kind of the idea of like a clause in in an Arduino program and so far we've had like two types of clauses the first type of Clause was a void setup these are things that you're going to do one time in the clause begins with a curly bracket and it ends with a curly bracket in any commands that you put between those two curly brackets are in this clause the second clause is a void loop it begins with a curly ends with a curly in any commands that you put between those two Curly's will be executed over and over because you sit in loop there there are other clauses in the clause that we are going to learn today is the if clause or the if statement and those are statements that are only executed based on a condition okay only statement they're statements that are only executed if a certain condition is true so let's just jump right in and get busy here and I need to figure out my most suitable shot here which is the most suitable shot let's work on this one for right now okay you remember in your Arduino kit your most excellent Arduino kid I'm just gonna kind of start over here on this okay in your most internal kit the I am like too small there I don't want to completely disappear on you there how you tell you like that okay you have a potentiometer in your kit and we talked about excuse me we talked about how to hook up this potentiometer in lesson number 12 but we're gonna start again I'm going to go through this fast and if you can't keep up go back to lesson number 12 but we plugged this in with the two it's hard for me to see we plug it in with the two leads right above that Center trench and then the one lead right below the center trench alright what did we learn in this this one is a little hard to plug in but if you mash it firmly it will go all the way in okay so what do we need to do to hook it up well we need to hook five volts across those outside or top leads and so I have a red wire here connected to the 5 volt pin on the Arduino it will come to the left pin or my left pin okay and then I will get ground the ground pin is next to pin 13 here and I will hook it to the right to the rightmost pin and then I'm going to read the voltage off of the center tap and so I'm going to cook that up down here to the center tap and now what will happen is this voltage will vary if I turn it all the way to the left it should read zero volts if I turn it all the way to the right it should read five volts because I'm going into pin a2 so let's set that program up really quickly you did this in you did this and let in the earlier lesson you might ask why are you repeating well it's just like sometimes reviewing stuff is good so I'm gonna go real fast this time I'm gonna go real fast this time because we have done this before and I need to switch over to a shot where you can see what is going on here I think this would be a good shot all right so we are connected to a pin we are connected to a pin and so we are going to say int and then that pin is going to be my pen let's say and then that is going to be a2 because we're connected to a 2 semicolon now we are going to be reading a value from the potentiometer so that's going to be int we'll call it read Val and that is going to we're going to read that so we're not going to put a value into that we just got to declare it okay we're going to convert that to a voltage and the voltages might be the in-between numbers and so we're gonna make that a float because it might be a decimal number we're gonna call that v2 for voltage to that we are not going to put a value in initially because it is going to calculate that all right what else will we need here I am sure we're gonna need something else but we will come down here and when I figure out what I'm forgetting then I'll go back and put it in we want to print out to the serial monitor so we need to set it up serial dot begin happy orange color and that's going to be a baud rate of 9600 again doesn't matter what baud rate you use but your serial monitor has to use the same one now what will we do we are going to read from the potentiometer we are going to convert that to a voltage we're gonna print it and then we're gonna delay so let's do this somewhat quickly here let's try to do this kind of quickly so what do I want to do well I want to say that read ah that was not good okay read val is equal to analog read one parameter what is the parameter where am i reading from I'm reading from my pin which was a two now what is V 2 if you don't remember this go back and look at the old lesson but remember that the the voltage off of the the coming off of the potentiometer can be between zero and five volts but the Arduino doesn't read from zero to five it reads from 0 to 1023 so we have to scale 0 to 1023 to read - we've got to scale that to the real-world voltage value which is 0 to 5 and so we've got to do that V 2 is equal to and we are going to have five point divided by ten twenty three point why do we have to put those points in there because we've got to force it to do floating point math five is an int if you said 5 divided by 10 23 5 is an int 10 23 isn't it you would get an int answer which is zero and you would sit and pull your head out and then you would throw your little Arduino kit away and you would never come back to do floating point math you've got to make sure these are floating point numbers so 5 divided by 10 23 it needs to be 5 point divided by 10 23 point and then we multiply by what we multiply by read Val and let's see if read Val was 10 23 10 20 3 divided by 10 23 is 1 times 5 is 5 and so if you're reading 10 23 it converts to 5 if you're reading 0 it converts to 0 so this takes this number between 0 and 10 23 and it changes it into the real world voltage the actual voltage of zero to five so we now have a real voltage let's make sure by printing it so we're gonna say seer serial dot print L in ah miss bullet print L end and then what are we gonna print V - okay you know what I didn't do a real good job at last time I didn't do a real good job at formatting okay so I'm going to say here's serial dot print L in and I'm gonna print the or just print because I'm gonna stay on the same line print potentiometer I hope I spelled that right potentiometer voltage is and I put a space and it semicolon uh-oh I didn't put us in my colon up here I get in a hurry and I don't do that sometimes and now we are going to pause and so we will put a delay and then let's say quarter of a second delay 250 right no no don't use constants down here use variables and so I'll call it DT for delay time and then I come up here and I need to set that up int DT delay time is equal to 250 I think we said and that should be good okay for you to see this I think I need to go to a different shot I think if I go to this shot you are going to be able to see the serial monitor when I pop it up but let's go ahead and download the program what does it not like it does not like something here do I have Oh sometimes if you have the serial mount I'm glad this happened because sometimes if you have the serial monitor open already you can't download the program I am hoping that that was it let's try it again yes that was it serial monitor open you need to close the serial monitor before you town try to download the program and now that the program is downloaded we can open the serial monitor and oh look at that we are getting values out isn't that great potentiometer value is five okay and that's all the way to the right you see my hand now I'm gonna start turning it gradually to the left and you see how smoothly that voltage on that Center pin is coming down to zero back up as I turn the knob so you see boom back up to five okay now what does an if-statement have to do with any of this what is this nonsense okay let's imagine let's imagine that you wanted to give a warning like let's say that if you had a voltage over four volts that that like maybe you shouldn't turn this to more than four volts if you have more than four volts and you could generate a problem so you need to make a warning you need to warn the person if the voltage is above 4 volts so what we're going to do is we are going to get into our most excellent kit I need you to get a red LED out so I need to find my little bag of LEDs why red because red is a warning so we are going to use a red LED okay if we are going to use an LED what else do we need we need a resistor we need to get one of these 330 ohm resistors okay so be getting that out as we talk I've got several here so I got to kind of look at the code this one is a 330 ohm resistor you guys I hope you're keeping your kits neat keep your kits neat keep your components all organized ok this way and now we are going to build the circuit a little more circuitry here I am going to need a couple of wires as well let me get a few wires out here and now let's build the circuit to build this circuit let's switch to a more suitable view I think this one will be more suitable okay so we want the long leg of the LED I'm gonna put it in column 35 okay and yeah let's see I'm gonna put it in 40 long leg of the LED is in 40 now I need a resistor so the resistor is gonna go one leg and column 35 in one leg in 40 and what does that do that connects do you want me to zoom in okay let me see if I can focus it so the long leg of the LED and the right leg of the resistor are both in column 40 which means they will be connected together through that column then I need to come to the top of the resistor which is 35 and I need to take that over to pin 9 on the Arduino let's say put it on 10 9 and then the bottom leg of the LED has to complete the circuit and I've already used one ground GND and I've used the ground over there about 10 13 and I could run the ground over and create a ground rail but if I have an extra ground I like to use it instead of trying to create that and if you look there's a G and D up here okay you see there's a G and D right there in fact there's two G and E's there so I'll put that like that okay so I now have a warning LED in my circuit and you can see how that's cooked up to ten nine right let's come back over to our code I love my circuit to be level I don't like crooked things and now my papers crooked oh my I'm gonna go crazy I'm gonna go crazy alright I think that's pretty good okay so yeah I'm working with pin nine now I am working with pin nine so what do I need I need int and it's red ah you can't see me let me come back over here okay so now you can see that I am right here on int red pin is equal to what nine all right so now I am gonna be using red pin ooh you know what I forgot to do I forgot to do my pin mode I need to do that that's very poor programming practice pin mode and that was my pin and that was an input okay and now I'm gonna do pin mode and then what is it red pin and that is going to be an output okay now what does the program do down here well it reads the analog value it converts it to a voltage and then it prints what the voltage is but what do we want to do we want to warn the user if they're getting over 4 volts and tell them that the voltage is getting too high how are we going to warn them we're gonna warn them by turning the LED on so what do we want to do here we want to put in an if statement in an if statement works like this you just put in cliff and then you put in what your condition is inside the parentheses we'll talk about the conditions and if that condition is true you start executing this Claus a Claus starts with a open curly in the clause int with a closed curly and notice that anything between this open curly in this closed curly it put one in and I put one in one of the things I don't like that the Arduino does let me see if I can get it to do it because it really bugs me and I could probably turn it off if I figure it out but you see I start and statement and then I put the open curly and when I put the open curly for some reason sometimes it will put in the closed one but not this time so I feel kind of silly so you know I'll have to put it in now what you have to see is this curly opens and closes that's associated with the if statement this opened curly and this closed curly is associated with a void loop so now anything that I put between this open curly in this closed curly will be executed if the condition is true well what condition do I want if v2 is what greater than 4.0 so if V 2 is greater than 4.0 what do I want to do well I want to say I want to say digit all right which pin red pin and what do I want to do I want to make it high and then that needs a : notice our semicolon notice that the if statement does not have a semicolon because it's not a command it's creates a clause just like void loop doesn't have a semicolon and void setup doesn't have a semicolon so similarly when we start this if statement it's an open curly and a closed curly how many commands do I put in there one digitalwrite red pen hi all right but now what you have to think is you have to think that if the two you got to turn it off right because if you come in here and when it gets above 4 it's going to turn on but then if you turn the potentiometer below for that led is stuck on so then you need to say if V 2 is what less than 4.0 then open Kerli and then you see when I hit enter it automatically closed it for me so you've got to be careful to not add an extra and that causes a lot of confusion I really don't like that so if it's less than four-digit all right and then what do we want red pin and then what I want though we want to turn it off and that one will have a semicolon all right this is pretty darn good so what this is going to do is if we turn that led to a voltage above four it should if we turn that potentiometer to a voltage above four it should give us a warning and turn the red LED on okay hold your breath I'm going to hit the download happy happy happy boom it is working let's open up our serial monitor I will need to go to this other shot here and then I will need to open up the serial monitor I hope the serial monitor opens oh it's hiding down here okay so look at that voltage is zero voltage is zero voltage is zero the potentiometer voltage is zero zero zero okay so now do you see I am going to very carefully start turning the voltage up and you see we're up to likes point seven volts we're coming up to one and a half volts and then we're coming up as I'm turning the potentiometer we're coming up to two and a half three I got to be real careful here oh my goodness it went to four and what happened the LED came on you see 4.04 4.0 5 the LED is on right so I better back off a little bit and as I bring it right under 23.5 the LED is off as it comes above for the LED turns on boom do you see how powerful this is now all of a sudden I can close the serial monitor and I can just sit and look only at the circuit I don't have to have a computer connected or anything I can just look and see ok I can turn it until it kind on that's too much that's my warning light I need to back off okay I need to back up so what are the conditionals that we know well we can say some variable that's greater than a number we can say some variable equal to it or great some variable greater than a number oh you can't see what I'm doing here I'm so sorry I hate it when I do that I really do okay so what conditionals have we learned the conditionals in the if statement go inside of the conditionals in the if statement go inside of parenthesis one is you can have a variable greater than a number as your conditional or you can have a variable less than a number for your conditional what would be another one another one would be equal okay so let's try that what we're going to say is is that if v2 equals but you know what and this is like the number one problem that I see in helping students find mistakes in their programs the conditional for equal when it's inside of an if statement is equal equal if you just said if v2 equals four Arduino says I guess he wants to make v2 equal four and it would put four in the variable V two it would change v2 just like when you say v2 equals it changes v2 if you say v2 equals here it changes v2 so for a conditional you don't say v2 equals you say v2 equal equal and we say equal equal it's like you're asking a question so if v2 equal equal let's make it five turn the light on and now here to turn it off it would be if v2 does not equal how do you write does not equal that is the exclamation equal so exclamation means not equal so if v2 equal equal five you turn it on it v2 does not equal you turn it off so let's roll that down and then I do think that we will look at the serial monitor again here okay so that's three point four and then we're going to turn it up turn it up and I can't quite get it to five that did not work okay let's see what I did wrong what if we set a fee to equal five I hate it when my programs don't work in real time okay that is that and that is not coming on that is not coming on let me look one more time and then what I am afraid is I'm afraid that I am comparing I'm afraid I'm comparing a float to an int and you really can't floats aren't exactly equal to anything and so I think that is the problem let's do it this way if Reed Val which was this up here if Reed Val equal equal 10 23 I want to I want to make this work and I'm embarrassed that it didn't work but I just kind of make examples up as I go and then I am sad if they don't work okay still the LED is not coming on do you guys see what I'm doing wrong did I miss something red pen is nine okay let's see I said one it's that was what my problem was did you guys see that or try this now did you see that I was turning it on here but then it would immediately turn it off there and I think that was the problem okay let's try one more time five and it is still not coming on I hate these types of problems so if Oh Reed bail I need to go back to v2 are you guys yelling and screaming at me I really hope you are but you know I do these problems kind of in real time and I sort of been making the problems up okay five and boom it is on if I turn it down boom it goes off okay the point of all this was to show you conditionals all right so if the two equal equal five it turns on if v2 is not equal 5 it turns off all right I need to look here to make sure that I tell you right because you know what the problem is I do a lot of different programming languages and sometimes I forget what the right command is all right you can do compound conditionals and that means you can use ands and you can use ORS so let's say if you want to turn the red the red warning light on if the voltage is between if the voltage is between 2 & 3 if the voltage is between 2 & 3 the way you would do that you would say if the voltage is greater than 2 and in the end is and and like that and v2 is less than 3 so you only get in here if both conditions are true that is v2 has to be greater than 2 and it has to be less than 3 in that case you will turn the light on now on this one to turn it off you would say if v2 is less than two now in this case you would not say and right you would not safe and because you want to turn it off either way so here you would want an or and the way you do or is these bars two bars that bar is like shift slash on my keyboard it is above the enter key so if V 2 is less than 2 you want to turn it off or if V 2 is greater than 2 R greater than 3 you want to turn it off do you guys understand the difference between and and or do you okay let me just take a second and do that because it's a pretty pretty important thing the difference between and and or alright so let's understand the difference between and and or if this is a and this is B what is a and B well a and B have to both be true and so that would be like that only in this region are you in the region of a and the region of B if you have a or B that would be everything because here it's true because B is true here it's true because a is true it's like a or B that would be sort of like if you said you know with the boys stand up or anyone over 12 it would be girls that were over 12 and it would be boys all boys and it would be boys over 12 so it would be all so and is more restrictive and or is more inclusive so for turning it off we want to turn it off at v2 is less than two or if V 2 is greater than or if V 2 is greater than 3 and or so these are compound statements where you can or conditionals together or you can and conditions together so if this works right if I'm thinking about it right the light should turn on between 2 and 3 and otherwise it should be off what happened here oh were you guys yelling at me what's wrong uppercase v there's a difference between uppercase and lowercase V and that was the problem problems like that are really hard to debug sometimes ok it is happy let's get our serial monitor opened ok four point seven nine the potentiometer is off that's good let's turn it down tension builds tension builds tension builds boom under three the warning light came on if I go back above three boom it goes off now let's see if I can get all the way boom it's on and now I go a little under two and boom it is off do you see the power now we've done kind of it's been kind of slow getting learning certain things it's been kind of slow for us to learn how to just get things going and how to do circuits and stuff but all of a sudden this is a really powerful thing I mean you can imagine applications where you would want to warn a person if they were in certain ranges without having a computer or a computer screen we could run this Arduino off of a battery and then we could get a warning if we are in this danger zone okay so let's go back and let's think about what we learned we learned greater than we learn less than we learned equal equal and we learn and not equal there of course you could imagine there is also you could do less than or equal you could do greater than or equal to and in that case it would just be will equal go second less than or equal to greater than or equal to so it would be greater than or equal to less than or equal to you could put greater than or equal to you could put less than or equal to and that just kind of changes how it behaves at the boundary but it recognizes though so we know equal equal not equal we know less than greater than less than or equal to greater than or equal to we also know and and which is and and we know barbar which is or so now you know all of these different conditionals this is your assignment I have an assignment for you go back and look at your little handy thing of LEDs okay and what I want you to do is I want you to do a project where you have three LEDs okay three LEDs not one but three how many pin modes you're gonna need three how many resistors you're gonna need three how many pin how many LED variable names you're going to need three but what I want you to do is hook up a green LED a yellow LED and a red LED and here are the conditions if the voltage is under three volts I want the LED to be green I want to have a green LED turn on indicating hey you're in the safe zone everything's okay but if you are between 3 & 4 volts what I want you to do is give a warning which would be a yellow LED and then if you get above 4 volts then what I want you to do is above 4 volts I want you to turn the red LED on now the kind of tricky part about this is again you've got to remember that like if you're in this zone of three to four where you turn the yellow on inside that if statement you've got to make sure that you turn the red off and you got to make sure that you turn the green off so inside of those if statements you have to make sure that you are turning the other LEDs off and so this is going to be easy to hook up but you're really gonna have to think about your if statements because you got to think about how to turn those LEDs off as well okay guys this has been a really fun project I think it's been an important one I hope you have enjoyed it as much as I have I hope you all give me a thumbs up I hope you will subscribe to the channel love to hear your comments down below cuz man I just don't know is anyone even watching these things does anybody just do these projects or I'm just am I just talking to the wall put your comments down below also on the homework right I want to know are you guys able to do this am i giving you something that's too hard or have I taught you enough where you can figure it out you see this is a problem if I just give you a homework like Oh make a blue LED turn on when it's between one and two that's like almost exactly what I showed you but I want to show you something and then I want to give you something different and see if you can figure it out okay palma quarter from top tech boy comm I will talk to you guys
Info
Channel: Paul McWhorter
Views: 163,681
Rating: 4.9765759 out of 5
Keywords: IF Statements, Arduino, STEM, Tutorials
Id: ORNted-NgRM
Channel Id: undefined
Length: 36min 50sec (2210 seconds)
Published: Thu Jul 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.