Variables


What are we going to do?

Objectives

  • Understand what variables are and why and when to use them in a program.
  • Learn how to create a variable, set the variable to an initial value, and change the value of the variable within a micro:bit program.
  • Learn how to create meaningful and understandable variable names.
  • Understand that a variable holds one value at a time.
  • Understand that when you update or change the value held by a variable, the new value replaces the previous value.

In order to learn that, we are going to use our humbot mi:pet to keep score when playing Rock, Paper, Scissors.

Materials

micro:bit board

The micro:bit was designed to be classroom-friendly from day one. More approachable than just coding, it's a great way to achieve fun, practical results with motivated students. Buy from here starter kit (recommended) or board only.

micro USB cable

If your micro:bit board didn't came with a USB cable, any microUSB cable should do the trick

Computer

The code editor is web based, so any computer with a web browser (and Internet connectivity) will do

Instructions

1 Open editor

Once you've opened the editor, pick a name for your project. Remember that it should be something that easily identifies it. To create a program, you just have to select some blocks from the left side of your page and drag and drop them to your workspace.

Click to zoom

2 Code with blocks

From the list of possible blocks, choose the ones needed to code something like the picture below. You can test your program with the simulator on the left of the screen. Once you're happy with the result, click on "Download".

Click to zoom

3 Upload to the board

When the program is ready and you click "Download", an HEX file will be downloaded to your computer (normally to your download folder). If download succeeded, a message will appear on your screen. Click "done".

Now, connect your micro:bit board to your computer using the micro USB cable. Your computer should now detect your micro:bit just as if it was an external drive. Search the HEX file you just downloaded and drag and drop them on your micro:bit drive. On windows you can right button on the HEX file and click send to MICROBIT.

Click to zoom

4 Final result

After a few seconds, your program should start on your micro:bit board.

Congratulations, you're on the right path to be a maker!, now continue with the following challenges! 🙂

Now it's your turn!

It was about time to let you try it for yourself! I now propose some challenges that I encourage you to do. I think they are fun and I trust you can pass them!

Remember that each time you change the program by adding or removing some blocks, you can see how it looks at the left side of the editor. When you're happy with the result, you'll have to download it again and send it to micro:bit board following same steps as before.

Here you have:

  • Change the A and B player names to match your initials
  • Extra challenge.There is more we can do with the input we received using this program. With maths functions you can do some operations. You can find these operations under the maths section of the toolbox. With that in mind, are you able to show the total number of ‘rounds’ that were played? To do this, we can add the values stored in the variables we created to keep track of how many times each player won and how many times they tied. (You can find the solution here but don't use it unless it's completely necessary, try to do it by yourself first)

If you managed to finish this activities, you can search for more challenges on micro:bit's website

What else?

Now that you have your micro:bit code ready, it's time to use it! Play a couple of Rock, Paper, Scissors rounds and see who is the Rock king!!


Continue your maker journey with the next activity: Activity 4, conditionals

or return to maker workshop main page