Variables are an essential backbone of programming, and we're going to dive into them now that we have a little bit of familiarity with coding. Variables are a way to store data and information in your code. An easy way to think of a variable is like thinking of them as a box which we can store numbers or words or any sort of information in.
Let's say that with code we wanted to keep track of how old a dog is. We could create a box called age and let's say this year my dog is 3 years old. We would start by storing the number 3 in it. Then every birthday we would go, okay, whatever is in the box, add 1 to it. And every year we would keep adding 1 to it without having to keep track of what number is inside the box. We don't need to know what's inside, we just add 1 to whatever is in there. Then at any time we could check how old my dog is by just checking the number in the box. We don't need to do any calculations or remember how long we've been counting for or anything like that because we've kept track of that with a variable.
Now that example might seem a bit easy, but when you have to keep track of dozens of variables and numbers and states and countdowns on a microcontroller that can perform millions of calculations a second, variables become an incredibly important and simple way to keep track and use data in your code. And there are also many different ways to use variables. That was just one example.
We've already been using variables. When we set up pin 15 as an input, we store that pin instance in a box called button. And then when we wanted to read the value of that pin, we kind of read the box called button, which has the pin instance inside of it. Now that's just another way that we use variables. And the best way to learn is through some examples and to see them in action. So let's get into some.
So copy and paste the example code into a new tab and save it. The example code can be found on our course page on our website. Link to that in the description if you're on YouTube.
Now this code is very similar to the dog's age example. And what we're doing here is we're simply counting how many seconds the button has been pushed down for. So we import our libraries as usual, set up a button on pin 15, and then we create a variable here called timer. And as you can see, it is super easy to create one. You just type a name for the variable, equals then, and then whatever data you want here. We will be going over some of the rules on creating a variable in a bit though.
Then in our while true loop, we start with a one-second delay. Then after every second, we check the button. And if that is equal to one, meaning that it's been pressed, it will set the variable called timer to timer plus one. We're just increasing the count of timer here by one, really straightforward. Then we just print whatever number is in the variable box called timer. Also worth noting is in this print function here, we don't need the inverted commas. And you don't need them whenever you're printing a variable or a number directly. We only need that if we want to print some text directly with like hello world or something.
Plug in your Pico. And if we run our code, you can see that every second the Pico will print out the current value of timer. We aren't pressing the button, so it's not increasing. And if we hold it down, it will count how many seconds it's being held down for. It's very slow. It's a little bit slow. And if we let go, it'll stop counting.
Let's do another example by modifying the button and LED code from the previous video. The sample code is also found in the same place if you already don't have that. In this code, we turned on an LED if the button was pressed, very, very straightforward. But let's say on top of that, we also wanted...To explain why it's more efficient, I want you to imagine that instead of the Pico reading the button, the Pico is you and you are checking if there are clouds in the sky or not. So the first line would be, if there is a cloud in the sky, turn the LED on. You would need to get up, go outside, check for a cloud, and then come back in. And if there are clouds, you turn the LED on. Then immediately after that, we have a print function here. And this command tells you again to get up and go outside and check for clouds and then print if there are clouds or not. But you literally just checked. You already know what it is, right? You're not being very efficient.
Your Pico is so fast that the time between the two button checks in the if and the print statement is probably about a millionth of a second. The state of the button isn't going to change in that tiny amount of time. I'm going to modify the code now to do it the better way instead. And I'm going to start by creating a variable called button state. I'm just calling it bState for short. And I'm going to set that to our digital read. So now we're going to take the digital read and we're going to store it in bState. And then I'm going to modify our if statement to just use bState and our print statement to use bState as well. So now we start the loop by checking the button and then storing it in a variable. And now our if and print statements just use that variable instead. And this is way more efficient, right?
If you had to check for clouds, you would go outside once and check. And in your head, you would store it as a variable and remember whether there were clouds outside or not. It's not like the amount of clouds in the sky is going to change in the 10 seconds it takes you to do those things, right? And exactly like you having to get up and go outside and check for clouds, it takes the Pico time and resources to read the state of its pins.
In this case, it doesn't really matter whether we use variables or not like this. The code will work the same. But when you start to get more complicated and lengthy code, telling the Pico to check every time the state of the button can slow it down quite a bit. But I would say the way we've done it with variables here is technically the better way to do it. It's also better practice and it helps to make it a little more human-readable.
Something really important worth mentioning here is that variables are temporary. If you turn off the Pico, all the data stored in those variables is lost, completely wiped, and you will start fresh again when you turn the Pico back on.
A really great thing about MicroPython is how easy it makes using variables. And because of that, you can kind of get away with not knowing the thing that I'm about to talk about. But we're going to quickly cover it because it is very important. And that is that variables have different types and these types matter a lot. There are quite a few different types of variables, but just to introduce them, we have three extremely common ones, integers, floats, and strings.
Strings are easy. You can think of them as a box that only stores words and sentences or anything you can type on your keyboard, really. So I'm going to create a string here and I'm just going to go, this is a string. There it is. And I put it in those inverted commas and I'm going to store it in the variable called X.Sure, here is the transcript formatted into paragraphs:
We can just print X. And if I run that, it prints it to the shell. Again, I don't need to put inverted commas if I'm printing a variable, even if I'm printing a variable that is a string. But if I'm printing that string directly, I need to put it in inverted commas like so.
Integers and floats, on the other hand, are types of variables that store numbers. And the way to think about them is that integers store whole numbers like 1, 3, 5, nice whole numbers, while floats store numbers with decimal points like 2.3341.
Now knowing what variable type to use is a bit confusing for beginners, but when we create a variable in MicroPython, it automatically chooses the correct variable to use for the data we want to store. So if I just go X is equal to 2.5, MicroPython is going to automatically assign that as a float. We can also override that and manually set the variable type with something like int and then brackets.
And if I store 2.5 as an integer, and here I'm just going to click run, and now I'm just going to punch X into the shell, which is going to print the value stored in X, it prints us 2. So in this case, we're trying to fit 2.5, which is a float value, into an integer box and the decimal just gets cut off.
So strings hold words and sentences, integers hold whole words, and floats hold numbers with decimal points. But MicroPython automatically assigns the right variable type, which is another reason why MicroPython is so easy to use.
So far I have been saying you can name variables anything you want, but that's technically not true because there are some rules about naming them. First off, they can't have spaces. They can only have letters, numbers, and underscores. For example, when we use the variable called bState, we used an underscore for that exact reason. Variables also can't start with a number. They must start with a letter or an underscore. They're also case-sensitive, so bState and bState, with a capital B, are not the same variables. And you can't use a name that is any of the MicroPython keywords. So you can't name a variable import because MicroPython won't know whether you're talking about a variable called import or you're trying to import something. It can't collide with existing words or phrases or anything like that in MicroPython.
One more thing. In the first example, when we were counting for how long the button was pressed down, we used the plus operator to increase the variable called timer, but MicroPython also gives you access to a variety of other math operators. If you want something to do, go back to that example and change it to a minus, or you could use divide or multiply, exponential, modulus. If you import the math library, you can access trigonometry functions like cos and sine and tan, all very, very helpful when your project has to deal with angles. You don't need to know how to use all those math-related things. We're just making you aware that if you need to do math on your Pico to process data, there is definitely a math function to do so. And math and variables kind of go hand in hand.
All right. Three key takeaways. One, variables are a convenient way to store and keep a track of data in your code. Two, variable types do matter, but thankfully, MicroPython does a really good job of managing them for you. And three, variables can only have letters, numbers, and underscores. They can't start with a space, they are case-sensitive, and you can't call them something like import.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.