Arduino Lesson 5 - Boolean logic - OR & AND statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so this tutorial is called logic gates but actually it's on boolean operations which is basically logic gates but used in the code so we're going to be looking at and and or gates so to help us understand boolean operations we're going to look at logic gates and truth tables first so first we'll look at and gates which if you're looking at an actual physical and gate it looks like this so you've got input 1 here input to here an output so we can make a truth table of this and it would look something like this so we got in 1 in 2 and an output so with an and gate so this is a and K you if you if an input one you input a zero so zero volts and in input two you input zero item zero volts you'd get zero volts out if you input zero into input 1 and 5 volts or a 1 into input 2 you'd get a zero out if you input 5 volts or a 1 into input 1 and a 0 into input 2 you'd get 0 out the only way to get a 1 out is to input 1 and 1 which is why it's called a NAND gate so the way we write a NAND gate in boolean is double ampersand so that's like that so it's a double and so now we're going to look at all gates so all gates look like this so that's an or gate so you got in one here into here and out here so if we draw that in a truth table we have N 1 into out so if we put 0 into both inputs we get a 0 out if we put 1 into 1 input and 0 into other we get a 1 out if we put 0 into the other one and one into the other one we also get a 1 out and if we put 1 into both we get a 1 out so they're called or gates because if you have 1 or 0 you get a 1 if you have 0 or 1 you get 1 if you have a 1 and a 1 you get a 1 as well so the only way to get 0 is to have two zeros the way we draw an all logic gate in boolean is to have two uprights I don't know what they're called but if you press shift and the backslash key on a keyboard it's character just on a side note you won't need this for boolean but if you're working with logic gates if you see an or gate with an N in front of it it means that these outputs are inverted so if this was an or gate the outputs would be 1 0 0 0 and that is the same with a NAND gate so if it was a NAND gate the outputs would be 1 1 1 0 so it's just the inverse of what it should be so I've wide all my buttons and LEDs up to the corresponding digital pins as well as to the 5 volt line at the ground line so now when I press the left button the left LED will turn on I want to press the right button the right LED will turn on now when I press both at the same time the blue LED will turn on so now we're going to move on to code and so I'm just going to fly through the setup quickly and took you go through it we've already done it a few times so I'm just going to talk very quick so we've got the left button on the right button so I'm going to go int button l4m button left equals pin 6 int button r4 button right I'm going to setup on pin 7 and then we've got our 3 LEDs so we've got int red left equals 1 because we're wired to pin 1 in red right in two and in blue equals 3 and then in the pin modes we write pin don't forget the capital F of mode and we write button L is the output bolt note sorry final as an input my bad in mode button R is an input pin mode red left is an output don't forget this is all capitals and don't forget the semicolons as well at the end of the lines red R is an output pin mode blue is an output and don't forget that's because we're digital writing to the LED so that outputs whereas with digital reading from the buttons which means their inputs so now we've done all this setup we're going to move into the void loop and we're going to use if statements to work out which buttons have been pressed and create outputs based on that so what we want to happen is we're going to turn the right LED on when we press the right button we're going to turn the left LED on when we turn when we press the left button but when we press both at the same time we want the blue LED to turn on so we're going to use if statements and logic gates to make this happen so we can start by saying if and we're going to take a digital read of the left button so button L is equal to hi so we're saying if the digital read of the left button is high and then we're going to say and and which means that's the symbol for a NAND gate in audrina code so because we want to check both the left button and the right button at the same time that will become clear in a second if digital read button are it's also equal equal to high and now we're going to close our if statement so what we're doing is we're checking both buttons at the same time [Music] and if the left button and the right button are high at the same time then remember from why I said at the start we want to turn the blue LED on so we say digital right blue high and that's going to turn the blue LED on and then we're going to want to use an else statement to turn the LED off if that statement isn't true so I'm going to say digital right blue low so that's going to turn it off when this isn't true so now what we've done there is when we press both buttons at the same time it's going to turn the blue LED on now we want to say if one button is pressed but not the other then we want to turn on the corresponding LED so let's start by whirring up the left side to turn on when we press the left button so we'd say if digital read button L is equal equal and we'd white hike in here because we want this to be triggered when the left buttons pressed now some of you might be thinking or we could just end the if statement there surely because that would be true when we press the left button but because we're not checking the value of the right button it would mean that this statement and this statement could both be true at the same time which isn't what we want because then the blue and the red left LED would be on at the same time which we don't want we want either the red left the blue or the red right to turn on so what we're going to do is we're going to add an out another hand statement and say if the digital read of button R is equal to low which means basically so if this button has been pressed but this one hasn't been pressed then we're going to turn the right LED on [Music] because remember up here we want the blue one and only the blue one to turn on if we press both buttons if we didn't have this second part of this statement then the blue one would turn on we'd exit that code and then this statement would also be true and we turn on the left LED so by adding this statement here we make sure that this statement is false when this one is true so now we're going to add our digital right so I'm going to say digital right and we're going to say the red LED on the left it's going to get high place brackets semi-colon and then we have to add an else statement for that else digital right red left is low okay and now we have to write a final if statement for the right-hand side so we start by saying if the digital read of button left is equal equal to low and the digital read of the button right is equal equal to high so in other words if button left isn't being pressed a button right is being impressed then we're going to turn the right LED on so I'm going to say digital right red right high and then we're going to add that last else statement we're just going to check that it's all good so I'm going to upload that to the board and see how that translates to the Arduino you so now instead of using an and gate we're using an or gate so if you press this button or this button then the blue lights can I turn on so we've started afresh in the void loop at the top I still have the same setup code as I had before so what we're trying to do is turn the blue LED on if we press button left or button right so let's start writing it up using an if statement so we say if digital read of button right is equal to high i if we press the button on the right and then we sit use an or statement and boolean which is these two uprights like this this is saying if the digital root of button right is high or if digital read the button left is high then we can close this the if statement open us with your brackets and digital bright blue LED high which is on and we can just use our else statement to turn that blue LED back off again when we're not pressing either at the buttons and there we go we can upload that and see how that works on the board so return me press this one or this one it's going to turn on and if we press both it's still get a turn on [Music]
Info
Channel: Benduino
Views: 45,413
Rating: 4.9305353 out of 5
Keywords: boolean, boolean logic, logic gates, what is boolean logic, arduino tutorials, arduino for beginners, learn arduino, boolean arduino, arduino boolean
Id: 9G0UFQAFWUQ
Channel Id: undefined
Length: 13min 17sec (797 seconds)
Published: Wed Dec 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.