Understanding Boolean Data Types and Using the Boolean NOT (!) operator to Switch Arduino Pin States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing well in today's tutorial we're going to talk about boolean data types and the boolean not operator so specifically what we're going to go over in this tutorial is what a boolean data type is and then we're going to talk about how to use the boolean not operator for switching pin States so let's go ahead and jump right in so let's start our discussion with boolean data types now boolean sounds really weird like I know I think I have some boolean in my fridge like chicken or beef bullion you know this is a little bit different this is programming speak so in programming speak the easiest way to think about a boolean data type is to think of like a switch like a light switch a really simple old-school light switch which is either on or it's off a boolean data type can hold two states it can either be 0 which is off or can be 1 which is on in boolean speak 0 is the same thing as false and false is the same thing as low so 0 false and low all me and 0 so if I say something is false I'm saying it's 0 in boolean if I say something is low that means it's 0 and boolean ok that's easy enough now if I say a one boolean that means that it's true and that also means that it's high so one true and high are all synonymous in boolean speak now I'm going to throw a little caveat in here for 1 when we're talking about boolean because the truth of the matter is is that any non-zero integer is in fact true when we're talking about boolean so the number 42 is the same thing as 1 is the same thing as true is the same thing as hi I know that's really kind of strange but just remember that Andy's non 0 integer is also true so it's easy for false 0 is false low is false 0 is false we're good there but for true it's one high or any non-zero integer so like negative 14 142 1953 all those numbers are true so how do we declare and initialize a boolean variable well it's quite simple let's go ahead and take a look at the Arduino IDE and we'll take a look at how it's done so let's look at the first part of this statement which is the data type which we've been talking about which is boolean notice that it's a keyword and it will turn orange for you and then I've got the name of the variable I've just called it boolean on and then I've set it equal to one which is our initial initialization of that variable we're sitting equal to a value and I've set it equal to one so if you recall one is equal to true so I'm going to write a couple equivalent statements here so the value of all of these statements is equivalent because it's a boolean so remember boolean can only exist in two states either on or off and all of these variables right here describe something that's on so you don't believe me well let's let's find a way to test this really quick you so what I've done is write a set up in a loop and in the setup I just set the mode of the pin pin 13 as an output and then in the loop I do a simple digital right to pin 13 and I set it to the boolean on variable so I'm going to do up here is I'm just going to go ahead and comment out these other boolean's I'm going to upload it and then let's go ahead and take a look at our board all right so we can see that the LED is on as we would expect because we have boolean on set to 1 well now let's comment out this and instead do this let's upload that and see if there's any change oh the LED is on again hey so we know true is equivalent to 1 well let's comment that out and turn this one on well upload that hey the LED comes on that's good let's try this one and the LED comes on again so we can be sure that all of those are equivalent statements well let's try the same thing for false okay so zero false and low are all boolean equivalents and I'm not going to test it here I trust that you trust me and you should try it on your own to see if all these work to turn this off what I want to do now is kind of switch gears now that we've talked about boolean data types now I want to talk about the boolean not operator the boolean not operator is written as an exclamation point now there's a couple cool ways to use it but the way we're going to use it right now is to use it to switch a boolean variable state so let's go ahead and do it and then we'll talk about it okay so I've made several changes let's start at the top so the first thing I did is I changed our boolean variable declaration to change the name to boolean state and I've set it equal to zero which again is false or low I didn't make any changes in the setup but in the loop I changed the digital right call we're still writing the pin 13 but now we change the variable to boolean state so the first time through the loop we'd be writing it low and then we delay 500 milliseconds essentially that allows us to see the state of that led now let's look at this last line of code where we use the boolean not operator so I'm setting the variable boolean boolean state equal to not itself so that exclamation point again is the boolean not operator and what this statement does is it changes a boolean variable to the opposite state so we started out with boolean state equals zero now boolean state is going to be equal to one so what behavior is this going to get us in this loop so let's run through the loop the first time through so we said the boolean state is set equal to zero so that would be low so the LED would be turned off and we would delay for 500 milliseconds so for 500 milliseconds half a second all we do is see it turned off LED not very exciting next line of code we switch boolean States and boolean states are no longer 0 now it's 1 and remember 1 is equivalent to high and then we would go through loop again so digital right pin 13 its boolean state well what is boolean state now well it's high okay so the LED turns on we delay 500 milliseconds so we can see that shining LED and then we get back to this statement and what does this do it says boolean state equal to not boolean State well it was high what is not high well that's low or that's 0 so it would turn the LED off so you can see essentially what we're doing is blinking an LED every 500 milliseconds so let's go ahead and upload this and take a look at it so our LED is blinking kind of like we thought it would you can see that this not operator is a pretty handy little tool for switching pin States it can be used for all types of things and probably in other tutorials we'll use it for other types of things it's great an if condition statements and that type of thing so I hope you found that this tutorial was a useful way to use boolean not operator and maybe familiarize you a little bit more with boolean data types look forward to seeing you in the next tutorial and definitely try out the challenge all right take it easy bye you
Info
Channel: Programming Electronics Academy
Views: 31,475
Rating: 4.9458647 out of 5
Keywords: Arduino, Boolean Data Type, Software (Industry), Switch pin states
Id: HzSiJn62TCY
Channel Id: undefined
Length: 8min 11sec (491 seconds)
Published: Mon Sep 15 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.