If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Variable block (counter)

Store values using the variable block (build a counter). Created by Brit Cruise.

Want to join the conversation?

Video transcript

Here, I'm going to show you how to build a simple counter, which is a very important fundamental unit of many programs. Let's say we wanted the ability for our robot to count the number of claps which occur. So we had a sound sensor, and any time it detected a sound level let's say at or above 50, we count one, two, three, four claps occurred. So to build something like this, we need to understand how the variable block works first and also the constant block. The variable block, think of it as a box which can hold a value and also allow you to read that value. It's a little placeholder. Think of it as a very basic memory. It can remember one thing. The other thing we need to understand is the constant block. The difference between the constant block and the variable block is you can't write to the constant block. All you can do is read from it. So it can store a value, such as say the number 1, and at any time you can read this number 1. And you may wonder, what's the point of this block? And you'll see in a moment. Now, we also need the math block, and that's the heart of the counting operation. Remember, the math block has two inputs, A and B, and then it allows us to do something with A and B. And in this case, we would want to add A and B together. And it's the total result. So let's just think for a moment before I wire this together. When you count, let's say we are incrementing x. Every time we increment x, we are actually doing this operation. We are saying x equals whatever x was before plus 1. So if x is 0 at the beginning, which let's say it is 0. The variable x, we wrote 0 to it at the beginning. The answer to x plus 0x plus 1, if x is zero is 1. And if x was 1, the answer would be 2 and so on. So for example, this little operation let's build it using blocks. We would plug this constant 1 into our math block and then plug the variable x into B. So now this math block is doing x plus 1 every time, but x is going to be changing. And the output we wire back into x. But I'm just going to draw x here, because you can call up your variable in different places in your program, and that's the powerful thing to realize. We plug whatever the result is into x, and let's say this is inside a loop so it runs again. Let's just walk through it once. At first, 0 is fed into the math block under B and 1 is fed under A. So it does 0 plus 1, and then it spits out 1 into x. Then when it runs again, this time the x provided will be 1, because remember it's a little memory. So the math block would say, oh, I got 1 and I always have 1 under part A, and that will give me 2. And we write 2 to x. Next time it executes, x will be 1 and 1 won't change-- I think you see the pattern here hopefully-- and we write 3 to x. And this seems like a complicated or convoluted way to add, but this is the way it's done. So in our simple example, let's say we had a switch statement here. And I'm not going to build a full working program, just a subset of it, and this was a sound sensor. And we had it setup, so any time the sound sensor's greater than 50 I want to count. So let's just grab our blocks you need. It's all under the data section. The constant block is the suitcase with a lock on it, and that lock there is to remind you, you can't write to this suitcase. You can just give it a value and it always has that value. The variable block is the suitcase without the lock on it, and this is our little memory. It can hold a value, we can write a value to it, or read from it. And let's grab our math block. Remember we need to add these two things together. Let me align this. And at the end of the math block, we're going to put the value back into our variable. Now, the most important step here is to remember how to create a variable. And to do so, first you go up here to the top under Edit, and you go to define variables. So let's say we want to build a variable. I'm going to click on Create. I'm going to give it a name. I'm going to call it "count." And the data type is important. It's set to logic here, but we want it to hold a number. It could hold text, it could hold a true or false, but in this case I want to hold a number. OK So now, I just have that variable set up, but I still haven't configured any of these blocks. So let's click on the first one, which is our constant. It's still set up, the data type is logic, so I'm going to switch it to a number. And by default, it's holding a value 0. And remember, we want to give our constant block the number 1. So now this block will always just provide the number 1 whenever you need it. Now, the variable block is setup such that first you click on it, and now that variable we just created, you'll see it down here in the list. I see counter type, number. Great. And now every time I have an action, I can either read or write to it. So remember, here we're reading from it, so I'm going to select it to read. And the math block is set to addition. Good. So let's do the first step. Let's wire the constant into A or B. It doesn't matter if they were in A or B. They are both just inputs. So now, we have a and b going into our math block, and the result is down here. It's this little pound sign-- it's called result-- and we want to put it into our variable. So first I'm going to click on this variable block-- and this is also the important step that might confuse you-- is I can assign this to be our counter as well. So our variable is here, and it's also here, and it could also be a mile down in our program when we need it later. It's not one block, it is one memory location that you can call up by bringing this block wherever you need it. I could have the value down here. So I know that's a little subtle step there, but you'll get comfortable with that. And so now this variable block counter, we want to write to it at the end. Because you remember, we take the value and we put it back into our variable. And now when I select it to write, you'll see there's a little port here now, an input port. I can take the result of the math block, put it in this counter, and there is our counter's complete. Every time the sound sensor detects a sound level above 50, it will execute this little path here, which will take 1, the value in counter, add them together, and put it back in counter. So if this ran 5 times, at the end the counter variable would be holding the value 5. And let's say we were somewhere else in our program. This is the first phase of our program. Let's say our robot is scanning the room and counting every time it sees a light or a clap. I can use that value later, so I could put the variable block over here and select counter number. And now this counter block will hold the count after this is executed. So that's a quick introduction account, and it kind of introduced variables and constant blocks to you. So good luck with that.