How does our program think?

Five small programs, five important ideas

Electronics Micro:bit
programming

In the last workshop we made five programs for the micro:bit in MakeCode: a greeting, a beating heart, a reaction to buttons A and B, a digital dice and a score counter.

Today we won’t just put them together again. We will find out why they work the way they do.

For each program we will try to answer four questions:

  • What starts the program?
  • What does the micro:bit do next?
  • Does it do it once, all the time, or only when something happens?
  • What will change if we change one block?

This is how we will work:

predict ⇒ run ⇒ change one thing ⇒ test again ⇒ explain the result

Programming isn’t just putting blocks together. Programming is also understanding what we have put together.

To start: what will happen?

Don’t open the computer yet. First try to answer without running the program.

The workshop leader will show four little plans. For each one, say what you expect:

  1. Show a name. Then show a heart.
  2. Keep showing a big heart, then a small heart.
  3. When I press A, show A.
  4. When I shake the micro:bit, show a number from 1 to 6.

Think: which program does something straight away, which one repeats something, and which one waits for you to do something?

You don’t have to guess every answer. What matters is that you start predicting before you press Play.

Program 1: Hello, micro:bit!

Technique: commands have an order

Our first program looks like this:

WHEN THE PROGRAM STARTS
SHOW MY NAME
SHOW A HEART
END

In MakeCode, the show string and show icon commands are placed inside the on start block.

When the program starts, the micro:bit first shows the name and then the heart. on start is for actions we want to run at the beginning; the show string block shows text on the LED screen.

Predict

Before testing, answer:

  • What will be shown first?
  • Will the name keep showing all the time?
  • What will be on the screen after the name has scrolled past?

Try it

Run the program in the MakeCode simulator. Watch the whole sequence, not just the last picture.

Change one thing

Swap the commands round:

WHEN THE PROGRAM STARTS
SHOW A HEART
SHOW MY NAME
END
Before running it, tell a friend what will happen. Then check whether you were right.

Conclusion: The order of the blocks changes the order of the actions. If we want a different result, sometimes we don’t need a new block – it is enough to move one that is already there.

Extra challenge

  • Add a third command: show your favourite number. Ask a friend to guess the order in which the name, the heart and the number will appear.

Program 2: A beating heart

Techniques: repeating and waiting

In the second program we use forever:

REPEAT ALL THE TIME
SHOW A BIG HEART
WAIT HALF A SECOND
SHOW A SMALL HEART
WAIT HALF A SECOND
END

forever keeps repeating the commands inside it. The pause block sets how long the program waits before it carries on. The time is written in milliseconds: 1000 ms is one second, and 500 ms is half a second.

Remember

forever repeats, and pause slows things down. They don't do the same job.

Predict

  • Will the heart beat only once, or many times?
  • Do you think it will beat faster if we change the pause from 500 ms to 100 ms?
  • And what will happen with a pause of 1000 ms?

Try it

Run the program in the simulator and watch it repeat a few times.

Change one thing

Change both pauses to 1000 ms. Test again. Then change them to 100 ms.

Remember

Don't change the icons and the time at the same time. When we change only one thing, it is easier to know why the result is different.

Extra challenge

  • Make your own animation from two pictures. For example, show a smiley face and a surprised face one after the other. Choose a pause that lets you see the change clearly.

Program 3: Button A and button B

Technique: the program reacts to an event

Until now, the micro:bit either did something straight away at the start or kept repeating a task. Now it can wait for us to press a button.

Here we use an event:

WHEN BUTTON A IS PRESSED
SHOW THE LETTER "A"
WHEN BUTTON B IS PRESSED
SHOW THE LETTER "B"

We call pressing a button an event. The on button A pressed block tells the micro:bit which commands to run when that event happens. MakeCode lets us react separately to A, B and A+B.

Predict

  • What will happen if we don’t press any button?
  • What will be shown when we press A?
  • Can the command for button B run when we press A?

Try it

In the simulator, press A and then B. On a real micro:bit, press the buttons with your finger.

Change one thing

Make button A show a happy face and button B a sad face. Before testing, check that you have put the right icon in the right block.

Conclusion: A program can have several parts that wait for different events. They don’t all run straight away, one after another.

Extra challenge

Add a reaction to A+B: make the micro:bit show a heart. Check whether the program can tell the difference between pressing only A, only B, and both buttons together.

Program 4: A digital dice

Techniques: an event and a random number

The digital dice doesn’t show a new number all the time. It waits for us to shake the micro:bit gently.

Here we use an event too:

WHEN THE MICRO:BIT DETECTS A SHAKE
PICK A RANDOM NUMBER FROM 1 TO 6
SHOW THE NUMBER THAT WAS PICKED

Shaking is an event, just like pressing a button. MakeCode can react to the shake gesture, and the block for a random whole number picks a value in the range we give it – including the smallest and the largest number. So the range 1–6 can give both 1 and 6.

Predict

  • Can the number 0 appear?
  • Can the number 7 appear?
  • Will every shake give a different number?
  • Should the number change if you are just looking at the micro:bit lying on the table?

Important

Random doesn't mean that every next number has to be different. A real dice can also show the same number twice in a row.

Try it

Run the program in the simulator and make it shake. If you are using a real micro:bit, shake it gently. Don’t throw it and don’t pull the USB cable.

Change one thing

Change the range of numbers from 1–6 to 1–3.

Now run the program ten times and write down the numbers that appeared. Can you see any number outside the new range?

Extra challenge

Make a dice with the numbers 1–10. Before testing, say what the smallest and largest possible results are.

Program 5: A score counter

Technique: the program remembers a value

This program brings in a new idea: a variable.

A variable is like a little box with a name. Our box says score on it, and inside it we keep a number that can change.

Before we work in MakeCode, let’s imagine it with a box and a few counters:

  • An empty box means we have 0 points.
  • Every press of button A adds one counter.
  • Pressing button B doesn’t add a counter; it asks us to count how many we already have.

The program works the same way:

WHEN THE PROGRAM STARTS
SET SCORE TO 0
WHEN BUTTON A IS PRESSED
INCREASE SCORE BY 1
WHEN BUTTON B IS PRESSED
SHOW SCORE

The MakeCode examples use a variable to count button presses and the show number block to show the saved value.

Predict

At the start, score = 0.

  • If we press A once, what is the score?
  • If we press A two more times, what is it then?
  • If we then press B, what will the micro:bit show?
  • Will just pressing B increase the score?

Build it and test it

In the Variables category, make a variable called score. Then, in the right blocks, add setting it to 0, increasing it by 1 and showing the number.

  • Press A three times, then B. Compare the result on the screen with your prediction.

An important difference:

A changes the saved number, and B shows it. The number can change even if we don't see the change on the screen straight away.

Change one thing

Change the increase from 1 to 2. Run the program again, press A three times, then B. What number do you expect?

Extra challenge: reset

Add an A+B event that sets score to 0. After the reset, press B again and check the result.

Reset means going back to how things were at the start. We will use a rule like this in other projects too.

Final challenge: Break it, then fix it

Each pair chooses one task. First explain what is wrong, then change only what is needed and test the program.

1. Problem: The greeting shows the heart before the name, but we want it the other way round
Clue to explore: Look at the order of the commands

2. Problem: The heart beats too slowly
Clue to explore: Look at the values in the pause blocks

3. Problem: Button A shows the answer meant for button B
Clue to explore: Look at which commands are in which event

4. Problem: The dice sometimes shows 0
Clue to explore: Look at the smallest number in the range

5. Problem: When the program starts again, the counter doesn’t start from 0
Clue to explore: Check how the starting value is set

For each task, say:

  • We think the problem is in ______.
  • We will change ______.
  • We expect that ______ will happen.
  • After the test, we saw ______.

If the result isn’t what you expected, don’t change all the blocks at random. Go back to the program and check one idea at a time.

How do we know we have understood?

It isn’t most important that all five programs look perfect. For one program of your choice, try to explain:

  • When does the command run?
  • What does the command do?
  • Does it repeat?
  • Does the program remember a value?
  • What did you predict, and what really happened?

If you can answer these questions and change one block on your own, you understand much more than you would by just copying the program.

What did we learn?

Today we didn’t just run programs. We learnt to predict what the micro:bit will do, change one part of the program and check whether we were thinking correctly.

  • A program is made of commands that the micro:bit runs in a certain order.
  • on start runs commands at the beginning of the program.
  • show string, show icon and show number show text, a picture or a number on the LED screen.
  • forever keeps repeating the commands that are inside it.
  • pause sets how long the program waits before the next command.
  • Pressing a button and shaking are events: the program can wait for them to happen and then react.
  • A digital dice can pick a random number from a given range. The same number is allowed to appear several times in a row.
  • The variable score keeps a number that the program can change and show later.
  • set score to 0 sets the score to zero, and change score by 1 adds one to the number that is already there.
  • When a program doesn’t work the way we expect, we check it step by step and change one thing at a time.

Questions to finish

1. What does it mean to predict the result of a program?
To predict means to say what we expect the program to do before we run it. After the test, we compare what we expected with what really happened.
2. Why is the order of the blocks important in the Hello program?
The micro:bit runs these commands in order. If showing the name comes before showing the heart, we will see the name first. If we swap the blocks, the order of what is shown will change too.
3. What is the on start block for?
on start runs commands at the beginning of the program. We use it, for example, for a greeting at the start or for setting the starting score.
4. What is the forever block for?
forever keeps repeating the commands inside it. That is why the big heart and the small heart can keep swapping while the program is running.
5. What is the difference between the forever and pause blocks?
forever repeats commands, and pause stops the program from carrying on for a certain time. Changing the pause changes the speed of the animation, but a pause on its own doesn’t make anything repeat.
6. How long is a pause of 1000 ms, and how long is a pause of 500 ms?
1000 ms is one second, and 500 ms is half a second. A longer pause means the picture changes more slowly.
7. What is the event in our program with the buttons?
An event is something that happens and starts a part of the program. In this example, the event is pressing button A or button B.
8. Will the command for button B run when we press only A?
No. The commands in the event for button B wait for button B to be pressed. Pressing button A runs the commands written for A.
9. What starts the digital dice?
The digital dice is started by the event of shaking the micro:bit. When the micro:bit detects a shake, it picks a number and shows it.
10. Which numbers can a dice with a range from 1 to 6 show?
It can show 1, 2, 3, 4, 5 or 6. It shouldn’t show 0 or 7, because they are outside the range we gave it.
11. If the digital dice shows 4 twice in a row, is it broken?
Not necessarily. A random choice can give the same number twice in a row, just like an ordinary dice can land on the same side twice.
12. What is the score variable?
score is the name of the place where the program keeps the number of points. This number can change while the program is running.
13. What happens when we press A three times and then B, if the counter starts from 0 and A adds 1?
After three presses of button A, the value of the score variable is 3. When we press button B, the micro:bit shows the number 3.
14. Does button B add a point in our basic counter?
No. In the basic program, button A increases the score, and button B only shows how many points we have right now.
15. If A adds 2 points, how many points will we have after three presses, starting from 0?
We will have 6 points: 2 after the first press, 4 after the second and 6 after the third.
16. What does resetting the counter do?
A reset sets the score back to its starting value, which is 0 in our example. For the reset we can use the event of pressing buttons A+B.
17. Why do we change only one thing in the program in an experiment?
That way it is easier to find out why the result changed. If we change lots of blocks at once, it is hard to know which change caused the new result.
18. What do we do if the program doesn't show the result we expected?
First we look at what really happened. Then we check the blocks, change one thing at a time, test again and, if we get stuck, ask the leader.