Tutorial 11: If Statement Conditionals: Arduino Course for Absolute Beginners (ReM)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello hope you're doing well welcome to this tutorial we are going to talk about the if-else statement so in the last tutorial we learned about the if statement and the if-else statement is just an expansion upon the capabilities of that really awesome function so this is the control section this is the first tutorial in the control section and what the if-else statement allows us to do is have more control over how things happen in our program so let's go ahead and jump right in for this tutorial you're going to need your Arduino board I've got the Arduino Uno here you can use any Arduino or clone should work fine I would recommend those sticking with the Arduino brain for these tutorials that way we know we're on the same page you're also going to need a potentiometer the resistance range doesn't matter you're going to need at least three jumper wires you'll need a 220 ohm resistor and you'll need an LED now technically you don't need the resistor in the LED if you're dueño board has a built-in LED at pin 13 it probably does but you can also add the resistor and the LED without any issues if you do have an LED there so I like the external stuff and you're also going to need one of those mallets that like a doctor uses to hit your knee alright to set up the circuit go ahead and take your potentiometer and put it in a breadboard and the leftmost pin on the potentiometer hooked to five volts the rightmost pin hook that to ground and then the middle pin you want that to go to analog pin zero so that's a zero and then you're going to want to hook your 220 ohm resistor to pin 13 and it doesn't matter which direction it goes and then you're going to want to hook your light emitting diode your led the short leg that's called the cathode you want that hooked up to pin out to the ground pin and then you'll connect the leg the long leg of the LED and the other free leg of the resistor with like an alligator clip and then you can go ahead and plug that into your computer so now go ahead and open up your Arduino IDE and go to file examples control if statement conditional and let's just start at the top so the first big block of code is a very large multi-line comment it's really not that big it just looks big because this is a lot more than we're used to seeing but it does a good job of explaining what the sketch is for shows you how to set up the circuit and then it also gives some attribution and lets us know that the code is in the public domain and then it also links to a really helpful tutorial on the Arduino website that I highly recommend you take a look at any time you get into a program a new program definitely read the comments see what the author had to tell you alright so that's enough with about the comments so now we move on to the next block of code and that is where we declare and initialize some variables so these are kind of interesting you see there's this Co NS T before int the int keyword that we're familiar with so that stands for constant okay so what we're doing is we're defining some variables here and we're calling them all constants what constant is is a qualifier of a variable so the first line they're constant qualifies the integer analog pin what it does is it protects the variable from ever being changed that might seem a little counterintuitive like isn't the whole idea of having a variable so that it can change well in some cases you don't want your variables to change you want them to be constants because you don't want at any point in the program a new number being assigned and what can happen occasionally is by accident you can assign a new value to something that shouldn't be changing and so when you make something a constant you protect yourself from future error if you know that that number will never change so that's the whole idea of a constant first it seems a little odd but in some cases and in this case it's a it's a good you know it's a a good time to use a constant okay so we've got three constant integers the first one is analog pin we've set that equal to a zero that's where we have the middle pin of our potentiometer hooked up we've got LED pin so that's the pin that our LED is attached to and then we have a threshold value now what we're going to do with this threshold value basically what we're doing in this program is we're going to set a value within the range of the potentiometer and to the left side of that value we are going to tell our LED to be off and to the right side of that value we're going to tell our LED to be on and so that threshold determines that cutoff point okay so those are the variable declaration and initialization x' so moving on to the next block of code we've got void setup so void setup doesn't have too much going on here we've got our good old pal pin mode so again pin mode sits the mode of a pin and what pin well led pin and that happens to be pin 13 and we're setting it as an output now if you recall all pins are by default set to be inputs okay so we didn't have to do pin mode analog pin input you don't have to do that it's done by default however I personally like to do it now it's not done here but I like to explicitly say whatever pin I'm going to use I like to set the mode not a requirement but I like to do it just for my own benefit so when I look at the code I know hey it was set as an input or an output okay and then the next thing we need to do is initialize our serial communication so we use the begin function from the serial library and we set the baud rate at 9600 because we're going to want to see information displayed to our computer from the Arduino so that is the setup okay now let's move on to the loop okay so the first thing in the loop we do and this should start to become familiar to you the first thing we do is we initialize and or rather we declare and initialize a variable so we're initializing a variable called analog value and we're setting it equal to the output of the analog read function okay so we've done this several times before this should be old hat to you so what we're doing is analog read is looking at the value that's at a zero so what it's reading the voltage there basically and it's turning that into a number a number between zero and a thousand and 23 so any number in between there based on the voltage from 0 to 5 volts it's going to throw a number out there so if there's zero volts it's going to return to zero there's five volts it's going to return a thousand at 23 and if it's somewhere there in between it's going to return that appropriately so that should be pretty straightforward so now the next line of code and this is where we run into our if-else statement so what we've got here so first we're familiar with the if statement so when we're looking at it in a statement the first thing we want to do we look at that condition what is the conditions set by the if statement so here it's asking is the analog value greater than the threshold so well what is analog value well that's that's that integer that we just declared and initialized so it's asking is the value is the voltage at your pin at pin a zero is it at such a value that it's higher it's going to be higher than the threshold and when I say voltage I actually mean the output value from analog read so that's kind of a misnomer there so is the analog value variable greater than the threshold that's the condition and if it is what do you do well look at the start curly bracket there right after the if statement and then the next curly braket one line below what's the code in there well that's digital right led pin high so digital right that's going to apply 5 volts to a pin well what pin will LED pin and that is pin 13 and well what kind of voltages are can it right is it going to write high or low it's going to write high it's going to write five volts because we say high if we wrote low it would be zero volts okay so that's what gets executed if the analog value is greater than threshold and in our case threshold is four hundred so if analog value is four hundred and one then that led at pin 13 is going to come on well okay well what if it's not what if that conditions not met and then what what else happens well hey look right after that if statement after that second close curly bracket what do we see we see the word else and the word else is a key word okay and then notice else we've got another curly bracket and then what do we have we've got another command and it says digital right LED pin low so now we're going to we would be writing low voltage to pin 13 so that would turn the LED off so let's let's reevaluate this condition basically what it's saying is if the analog value is greater than the threshold it wants you to turn the LED p.m. pin the LED pin high you want you to turn on and then if it's anything else so if that condition is not met so anything else it wants you to write the analog pin low so this is kind of a catch-all so the condition here the first condition is going to specify what it wants you to do in that condition and if that condition is not met the elf statement is everything else so if the first one doesn't go the second one will go for sure okay so that's the if-else statement else is always going to get executed if that condition is not met and if the condition is met then the only thing that's going to get executed is what's in that first line what's in between those first curly brackets okay so I think that's enough about the if if-else statement now in later tutorials there's actually an if else if stay so you can set multiple conditions but we're not going to talk about it now just kind of keep you excited for the next tutorials here cool more cool stuff down the road ok so then once we've once we've either turned that the LED on or turn the LED off based on the position of our potentiometer then what we're going to do is we are going to print that analog value to the serial port because we want to make sure like is it working like we say so when we see that the analog value printed on our computer screen is greater than 400 so it's like 402 we should expect when we look at our breadboard that the LED is going to be illuminated and then when we have it less than that we should expect the LED to be out and so that just kind of helps us verify if our program is actually working right if our program in our circuit are lining up and then we have a delay and then the delay just allows like a stability between the reads is if you happen to be turning that potentiometer quickly ok so then that's the end of the program so again it would loop back over we take a new value for the new position of the potentiometer in case you moved it and then it would compare that new value against the threshold if it's above the threshold then it's going to write high it's going to turn the LED on and then if it's anything else it's going to go ahead and turn that LED off ok so that's pretty much it let's go ahead and verify this code and upload it and then let's go ahead and serial monitor ok so right now you can see here's 500 so I'm above the threshold and my LED is on now I'm going to kind of lower it okay I'm right this down below and my LED goes off what if I'm right you know it's kind of at that junction work it's a little interesting you can get you can actually get your led to blink right at that Junction because the value that's getting red isn't always that stable so you can see if you can get yours to blink I know I can get mine to blink but you can see when you go below it goes out and when you go above it goes high so that's pretty much it this is a relatively short tutorial kind of straight into the point again talking about the if-else statement really allows to give you another option with an if statement a little more control kind of like a default response if your condition isn't met so that's it look forward to seeing you in the next tutorial and make sure please make sure to do the try it on your own challenge that's where you're really going to learn something alright see you next time you you
Info
Channel: Programming Electronics Academy
Views: 170,865
Rating: 4.927711 out of 5
Keywords: Arduino (Computing Platform), Conditional, Tutorial, Learn, Intro, Introduction, electronics, microcontrollers, arduino course for absolute beginners, challenegs, learning arduino, open source hardware group, serial port. serial monitor, leds, pinMode, digitalWrite, outputs, arrays, void loop, void setup, potentiometer, serial.begin, analogRead, serial.println, if statement, if-else statement, Arduino (Brand), Statement, c++, C (Programming Language)
Id: w-yfj1VXW8I
Channel Id: undefined
Length: 14min 12sec (852 seconds)
Published: Mon Oct 07 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.