Arduino Workshop - Chapter Two - Using Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you [Music] if you remember back to the previous chapter you created and uploaded the first program to your Arduino board where the goal is to turn an LED on and off to make a blink which is great and that was a really good first example however even in a basic program such as that there are always ways to make it more elegant and easy to understand and that's the goal right so now we'll learn about a new aspect to coding which will make everything you do after this so much easier we're going to take a look at variables so what is a variable well a variable is used to store information in your code it has a name a data type and it takes up space in your processors memory or on the chip memory a variable allows you to store a value perhaps from a sensor or an input and use that value in another part of your code perhaps you know to send an output pin or compare it against another value variables are fairly straightforward and can be thought of as containers on a shelf every container needs a name so that your code knows how to identify and access it every variable has to have a unique name and as with most things in programming these things that tape sensitive then you have to decide on what type of container or variable it is a small container can hold less information but therefore takes up less space on the shelf whereas a bigger container can hold more information to take up more space and remember to space the memory space on your program is limited and as you'll soon discover there are many different types of data encoding and variable types can only hold specific types of data to create a variable use the following format so we'll open up the IDE and take a look variables are usually declared or created at the very top of the sketch and we have the data type then the variable name is a space in between and then we let it be equal to a value with a semicolon to finish the line off as we will start in our program structure section so let's say that you wanted to create a variable to store the state of an on/off switch that has the toggle switch for controlling a project an integer type variable is perfect to as it only takes up two bytes of space and is the default variable types for general usage our variable declaration would then look like this instead of data type we would use int which is short to integer and with your dueƱo ID a and most really well fleshed out text editors you can tell you've done it right because so you see that way that enters highlighted to a light blue that indicates that is the correct formatting for that element so we use int and then we give it a name so it could be switch or switch state perhaps and you'll see how I've got a capital S at the start of state this isn't strictly necessary you can call it whatever you like as long as there's no spaces between multiple words however it just gives a really nice flow in continuity to your code if every variable is styled in the same way so it's all lowercase with a capital letter at the start of each consecutive word to break it up and we would then set the value of switch off switch state in the code to use it later however you can also declare variables without defining them remember that declaring is to create the variable and defining us to give them a value to do this you simply leave out the equal sign and end the line with a semicolon so to switch state if we wanted to give it a specific value we can do that we could make it equal to 0 or 1 if it was just a binary on/off switch or perhaps a rotary switch we might have more than two different values but if we want to declare it without defining it we can simply end it with a semicolon now 5 is Fault the value is 0 if you do not define the variable unless it's otherwise the time and there are many different types of variables but we'll cover each one as we get to it in the course something important to note is that there are two different scopes of variables global and local a global variable is declared at the top of your program as we've seen already outside of any function or statements a global variable can be accessed anywhere in your code and is unique for your entire program a local variable however is declared within a function or a statement is unique within that level and can only be referenced or accessed from that level this means that you can have multiple local variables in your program in different states for functions all with the same name or without conflict and with different unique values so why bother using local variables versus global variables well it comes back to the best practices that we were talking about earlier the scope of your program or how well everything is integrated should be as tight as possible variables should be declared as close to their use case as possible making our code more elegant simple and easier to understand from a system perspective a global variable will take up more memory in your code because it is persistent throughout your entire program it stays that all the time whereas local variables are created at their declaration and destroy it upon completion of the function they were in freeing up that space by other users let's take a look at some examples of global variable versus global variables so again in the Arduino IDE you can see the we've declared switch States as a global variable so we might want to use that later on perhaps can avoid Lukas what's holding to the state of a button now we could create another local variable and call it button pin now the reason for doing this or perhaps let's call it led pin because in the previous example we were controlling an LED and we gave the number of the pins we simply wrote that number in our function whereas we can use LED and let's say we're using pin 13 if we create LED pin and make it equal to pin 13 now we can simply use LED pin wherever we want to use 10 13 because they're equal to one another and then we only have to change the value once if we want to change pins it's pretty handy so that's an example of a global variable now a local variable goes in a function or a statement so let's say we want to create a variable in void loop there's a lots of reasons for doing this as we looked at before the loop iterates over and over and over again so if we create let's say we're reading at sensor sensor value now every time the loop iterates the first thing it does is create some integer type variable for 16 bits to vise call sensor value and then we can give it some button some value or we can simply declare and define run but we could use a digital read function to get the value of an on/off switch or analog ready to get the value of an analog sensor now every time it iterates through that loop it will be created and every time it reaches the end of the loop or X's out of that function it will be destroyed freeing up that space it makes it really tight because you can see where the variables were venues and why whereas LED pin is a really good use case of a global variable because you're going to use the invoice set up you're going to use the in void aleut it's a really clean case because you're going to be using the output pins usually throughout the majority of your code which is fantastic so it's global versus local variables there's a little bit more to it that's a really good broad overview and now that you can go and use variable in different ways and make your code really well contained because one of the biggest issues with people starting up with Arduino with programming general is that they'll write a functional they'll write a program and something will trip them up it won't work the first time and so they'll ask someone else for help and they'll upload that code perhaps to a forum or the message to a friend or an email but because it's poorly formatted it makes it very difficult for other people to understand what's going on and read it either even if they are well-versed in that particular programming language so if by formatting and structuring your code properly and using variables in some of the outlines and guides that we've presented here you'll be able to create really easy really readable elegant code
Info
Channel: Core Electronics
Views: 53,105
Rating: undefined out of 5
Keywords: arduino workshop, arduino tutorial, how to use arduino
Id: fqsSAjQFI7s
Channel Id: undefined
Length: 8min 8sec (488 seconds)
Published: Tue Feb 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.