Introduction

You are going to code your micro:bit to read the future! Simply ask the micro:bit a quesition, and press a button to find out the answer!

Instructions: If you’re reading this online, ask the micro:bit below a question and press A to get your answer!

Step 1: Scrolling text

Let’s start by scrolling some text instructions on your micro:bit.

Activity Checklist

  • Go to jumpto.cc/pxt-new to start a new project in the MakeCode (PXT) editor. Call your new project ‘Fortune Teller’.

You can delete the forever block by dragging it over the palette, you don’t need it for this project.

  • Drag a show string block inside your start start.
screenshot
screenshot
  • Test out your code. You can test it out in the emulator or on the micro:bit itself.

Save your project

Step 2: Making a decision

Let’s get your micro:bit to make a decision by randomly choosing a number (0 for ‘No’ and 1 for ‘Yes’).

Activity Checklist

  • Add a new on button A pressed event to your code.
screenshot
screenshot
  • Let’s create a new variable to store the answer. Click the ‘Variables’ and then click ‘Make a variable’.
screenshot
screenshot
  • Name the new variable called answer.
screenshot
screenshot
  • Drag a set block fom Variables into your on button A pressed block and select the answer variable.
screenshot
screenshot

As you can see, the to in the block means that you can set the answer to display.

  • Click ‘Math’ and drag a pick random block after the to:
screenshot
screenshot
  • Tell the random block to choose a number between 0 and 1. Here’s how your code should look:
screenshot
screenshot
  • Next, you want to display the word No on the micro:bit only if the answer is 0.

To do this, drag an if block onto the bottom of your on button A pressed event:

screenshot
screenshot
  • Next drag an = block as the condition in the if:
screenshot
screenshot
  • Drag your answer variable onto the left side of the if block.
screenshot
screenshot
  • Any code inside the if block will only run if the answer is 0. As 0 is No, let’s add a show text block.
screenshot
screenshot
  • Test your code.
    • Sometimes the answer will be 0, and the micro:bit should say ‘No’.
    • Sometimes the answer will be 1, and nothing will happen!

Save your project

Challenge: Multiple answers

Can you add code so that ‘Yes’ is displayed on your micro:bit if the answer is 1? You can even change the text shown to something more interesting than just ‘Yes’ and ‘No’!

You can even make your micro:bit say something like ‘Maybe’ or ‘Ask again’ if the answer is 2. To get this working, you’ll also need to change your code to choose a random number between 0 and 2!

Tip: You can right-click on an if block to dupicate the block and its contents.

screenshot
screenshot

Save your project

Challenge: Shake your micro:bit

Can you code your micro:bit to make a decision when it is shaken instead of when a button is pressed?

Save your project