The full Arduino Workshop in step-by-step format can be found here: https://core-electronics.com.au/tutorials/arduino-workshop-for-beginners.html In this section, we'll explore the use of variables, which will allow you to write more sophisticated code.

Transcript

If you remember back to the previous chapter, you created and uploaded the first program to your Arduino board, where the goal was to turn an LED on and off to make it 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 processor's memory or on the chip's 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're to set 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 are case 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, but takes up more space. And remember that the space, the memory space on your program is limited. And as you'll soon discover, there are many different types of data in coding and variable types can only hold specific types of data. To create a variable, you 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 with a space in between, and then we let it be equal to a value with a semicolon to finish the line off as we looked at 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, perhaps a toggle switch for controlling your project. An integer type variable is perfect for this as it only takes up two bytes of space and is the default variable type for general usage. Our variable declaration would then look like this. Instead of data type, we would use int, which is short for integer. And with the Arduino IDE and most really well fleshed out text editors, you can tell you've done it right because so you see that way that int has 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 and 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 or 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 is to give them a value. To do this, you simply leave out the equal sign and end the line with a semicolon. So for switch state, if we wanted to give it a specific value, we could do that. We could make it equal to zero or one if it was just a binary on off switch, or perhaps a rotary switch where we might have more than two different values. But if we want to declare it without defining it, we can simply end up with a semicolon. Now by default, the value is zero if you do not define the variable unless it's otherwise defined. 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 statement. 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 or functions, or 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 your 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 there all the time. Whereas local variables are created at their declaration and destroyed upon completion of the function they were in, freeing up that space for other uses. Let's take a look at some examples of local variables versus global variables. So again in the Arduino IDE, you can see that we've declared switch state as a global variable. So we might want to use that later on, perhaps in our void loop if we're polling for 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 pin, 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 pin 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, and there's 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 a sensor. Sensor value. Now every time the loop iterates, the first thing it does is create an integer type variable, so 16 bits, 2 bytes, called sensor value, and then we can give it some value. Or we can simply declare it and define it later on. But we could use a digital read function to get the value of an on off switch, or analog read 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 exits out of that function, it will be destroyed, freeing up that space. And it makes it really tight because you can see where the variables are being used and why.

Whereas LED pin is a really good use case of a global variable because you're going to use it in void setup, you're going to use it in void loop. 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 global versus local variables, there's a little bit more to it, but that's a really good broad overview.

So now that you can go and use variables in different ways and make your code really well contained. Because one of the biggest issues with people starting out with Arduino, with programming in general, is that they'll write a function or 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 their code perhaps to a forum or as a 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 easy, even if they are well versed in that particular programming language.

So 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.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.