View on GitHub

v2-pet-hamster

A MakeCode project

@hideIteration true

@explicitHints true

Using variables

A simple task.

Let’s start with a simple task:

input.onButtonPressed(Button.A, function () {
   basic.showString("Hello")
})
input.onButtonPressed(Button.B, function () {
   basic.showString("Bye")
})

An impossible task.

Let’s change the task a little bit:

Note that we are not involving button B at all this time.

Note that the following solution is NOT correct:

input.onButtonPressed(Button.A, function () {
   basic.showString("Hello")
   basic.showString("Bye")
})

This shows one message after the other whenever we press the button. The program does the same thing every time. But we want it to do one thing the first time we press it, and a different thing the second.

Spend a bit of time trying to find a solution. How can we do this?

Variables

The answer is: we can’t! Unless we use the microbit’s memory. The microbit does not remember how many times you have pressed the button. Every time is like the first time.

You need to use the memory of the microbit to remember that the button has already been pressed.

To use the memory of the microbit we need variables.

A variable is a little space in the memory of the microbit that we can manipulate. You can think of it as a box. You can put something in the box, and later come back and look at the content of the box. Most importantly, you can tell the microbit to change the content of the box, and do different things depending on the content of the box.

Let’s create a variable.

let counter = 0

Run your program. What happens?

Displaying variables.

Apparently nothing happens!

Well, something has happened, but we can’t see it. By creating a variable, we have modified the memory of the microbit. But the microbit doesn’t show what goes on in its memory.

~reminder

Let’s recap for a moment what we know about variables:

Let’s show the value of the counter variable on the screen of the microbit when we press button A. Since the value is a number, we have to use the ||basic:show number|| block in the ||basic:Basic|| category. But instead of showing a fixed number, like so:

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   basic.showNumber(0)
})

we will show the value of our counter variable (find it in the ||variables:Variables|| category, in a circle, and drag it into the ||basic:show number|| block):

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   // @highlight
   basic.showNumber(counter)
})

Can you spot the difference between the two programs? Look closely! For the moment, both programs seem to do the same thing. One is showing the number 0. The other is showing the value of the counter variable, which also happens to be 0. To do more interesting things, we need to modify the value of our variable.

Modifying variables (1).

Acutally, we have already learned to modify the value of a variable!

Remember how we used the ||variables:set|| block from the ||variables:Variables|| category to set counter to 0 on start?

We use the ||variables:set|| block to modify the value of the variable. Let’s modify the value when we press the B button:

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
})
input.onButtonPressed(Button.B, function () {
   counter = 1
})

Run this program. Notice:

Did you see that? The second time we pressed button A, something different happened. Remember we didn’t know how to do that?

We can both show and modify the variable with the same button:

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
   counter = 1
})

Before you run this program, answer these questions:

Now run the program and see if you were right!

Notice that the order of the blocks matters. Swap the ||variables:set|| and ||basic:show number|| blocks, like this:

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   counter = 1
   basic.showNumber(counter)
})

Answer the same questions, and then run the program and check your answers:

Modifying variables (2).

There is another way of modifying variables in microbit: the ||variables:change|| block.

Let’s go back to this program, which we will call program 1:

let counter = 0
counter = 0
basic.showNumber(counter)
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
   // @highlight
   counter = 1
})

Now change the ||variables:set|| block for a ||variables:change|| block:

let counter = 0
counter = 0
basic.showNumber(counter)
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
   // @highlight
   counter += 1
})

Let’s call this program 2. What’s the difference between program 1 and program 2? Fill the following table:

  Program 1 (using set) Program 2 (using change)  
On start Shows 0 Shows 0  
First time button A pressed      
Second time button A pressed      
Third time button A pressed      
Fourth time button A pressed      

~reminder

Here’s the difference between ||variables:set|| and ||variables:change||:

By the way, see what program 2 is doing? It is counting how many times we press button A. Now you may understand why we named our variable counter and not jabberwocky!

We can change the value of the variable by numbers other than 1. Try this program and see what happens when you press button A repeatedly:

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
   counter += 3
})

We can also change the value of a variable down instead of up. To do this we need to use a negative change. Try this:

let counter = 9
input.onButtonPressed(Button.A, function () {
   basic.showNumber(counter)
   counter += -2
})

A counting game.

Now let’s play a game.

Now I’ll describe the game, and you code it:

Have you coded it? Then try to win at the game (without changing the code!). The goal of the game is to get the microbit to display the number 1.

Good luck!

Advanced: Back to the impossible task.

Remember the “impossible” task from the beginning?

Can you do it now? You want to do different things depending on how many times you’ve pressed button A, so it can be handy to use a counter variable again. But we don’t want to show the value of our variable (which, remember, is a number), we want to show a text (we need the ||basic:show string|| block):

let counter = 0
counter = 0
input.onButtonPressed(Button.A, function () {
   basic.showString("Hello")
   counter = 1
})

Try it out.

This is not what we want! This always shows the same text!

We want to show different texts depending on the value of the variable. When the variable is 0 (the first time) we want to show “Hello”. When the variable is 1, we want to show “Bye”.

That’s where a new block comes in handy: the ||logic:if - else|| block, in the ||logic:Logic|| category.

Try putting together this program:

input.onButtonPressed(Button.A, function () {
   if (counter == 0) {
       basic.showString("Hello!")
       counter += 1
   } else {
       basic.showString("Bye")
   }
})
let counter = 0
counter = 0

The ||logic:if - else|| block tells the microbit to do different things depending on whether the value of counter is 0 or something else:

Challenges: