Arduino Lesson 4 - If Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the last video we set up a digital read in this video I'm going to show you how to use an if statement which will help us make use of this digital read so I'm gonna start by wiring up this button and this switch and the aim of what we're doing today is to turn the LED on when we press the switch using an if statement in the code so if I wire this up so I've wired the button up to ground on this side five volts on this side and the digital read wire is hooked up in the middle to pin seven of the Arduino now I explained how these buttons worked in the in the last episode of tutorials but I'll go over it again so basically the digital read wire and the ground wire are connected via this PCB through this resistor which is between 1 and 10 K so when we press the button it bridges the gap between the digital wire and the 5 volt line because there's no resistor between those two the digital wire is pulled high the LED is wide to ground on one side and the LED circuit is just a green LED and a 220 ohm resistor and then on the other side of the circuit it's wired to one of our digital pins on the Arduino in this case it's wired to pin 8 so if we digital right to pin 8 if we digital write low the LED will turn off if we digital write high it will turn on so now we can start writing some code so now I've wide all that up we can start coding little circuit so the aim is to be able to turn the LED on when we press the button so first off we're gonna set the button and the LED up right at the top here by giving them English name so we can call our English name instead of calling the pin number so we can int and then you can call it whatever you want but I'm gonna call it button equals and then I've wired my button to pin seven so I'm gonna write seven and semicolon now we have set up the LED so we're gonna get LED equals I've void my LED to pen eight so I'm gonna write eight so now we come down to the void setup and we write pin mode with a capital N to setup the inputs and outputs and we're gonna write button and button is an input because we're gonna read a signal from it so we write input in all capitals semicolon and then we're gonna write pen mode again and this time we're gonna set up the LED as an output because we write a signal to an LED we don't read one so it's an output so I've explained this more in previous tutorial so if you don't understand what I'm doing here then go back and take a look at the previous ones so now we've finished all our setup code we can move on to the void loop and we can start with our if statement so we start if statement by just writing if in the code and then we open and close some brackets and then we open and close some squiggly brackets so this here is our if statement now between these brackets here we have to make a statement which if it if the statement we make is true then we're gonna run the code between this squiggly bracket and this squiggly bracket if the statement we make here is false then we're going to ignore the code that's between this squiggly bracket then this squiggly bracket so in this example what we're trying to do is check if a buttons been pressed or not if the buttons been pressed we're going to turn an LED on so what we do is in here we'd write a statement which says if the button has been pressed then in this pot here you'd right turn the LED on and then underneath you'd right otherwise turn the LED off so first off let's start by writing our true or false statement up here that is in relation to the button so first off we have to check the status of the bottom so you have to say digital read and then we have to tell it what we're checking so you brackets right button and then we close the brackets so so we're digital reading the button and as I said at the end of the tutorial on digital reads that I did two tutorials ago I think now this returns either a high or a low so at the moment that on its own isn't there's no comparison there so you can't check whether it's true or false so we have to give it something to compare against so we know this is either going to return a high or a low so let's say if the digital reader of a button is equal equal which means equal to so there's two equals there and then we write either high or we write low in this instance I want this if statement to be triggered when we are pressing the button which means I want it to be triggered when it goes high so what we're saying here is if we read the button and the buttons value is equal to high then do what is ever whatever is in between these brackets so in this instance we want the LED to turn on so we write in here digital right and then we open the brackets tell it which pin we're talking about which in this case you could either write eight or because right at the top we've set up LED equals eight we can write LED then comma and then either high or low in this instance we want to turn the LED on so we write high close the brackets semi-colon so that's the if statement finished the only problem with this is the LED will start off being turned off we'll press the button once it will turn the LED on and then the LED will never turn off again because we've not got a digital write low anywhere in this void loop so what we can do is write an else statement which is in some way it's connected to if statement so it basically means else just means if what we're saying here isn't true then do this instead so we write else and then we open our squiggly brackets and then we close our squiggly brackets here and then in between we write digital right LED low so basically that means if we're not pressing the button then we're just going to turn the LED off so now we're going to upload that and see how it looks on our Arduino when we press the button it's going to pull our digital Reed pin high which will make our if statement true that means that we can then digital right to our LED high so when we press the button the LED will turn on the else statement means that when we're not pressing the button the LED turns off [Music]
Info
Channel: Benduino
Views: 67,958
Rating: 4.901031 out of 5
Keywords: arduino, arduino uno, digitalwrite, digitalread, arduino course, learn arduino, arduino programming, what is an arduino, what is arduino, arduino 101, arduino tutorial, how does an arduino work, if statement, if statements, arduino if statement, arduino code, arduino for beginners, arduino programming for beginners, arduino beginners course
Id: SKQzljqd3cQ
Channel Id: undefined
Length: 7min 25sec (445 seconds)
Published: Tue Dec 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.