Introduction

You are going to code your micro:bit to tell you how compatible you are with your friends.

Step 1: Pressing buttons together

Let’s start your program when the A and B buttons are pressed together.

Activity Checklist

  • Go to jumpto.cc/mb-new to start a new project in the Code Kingdoms editor. Call your new project ‘Rate Your Mates’.

  • When the micro:bit starts, display an image showing that both buttons should be pressed.

screenshot
screenshot

You can use scrolling text instead of an image if you prefer.

  • A rating shouldn’t be shown until both buttons are pressed. Add a new onPressAandB event to your project.
screenshot
screenshot
  • Add code to show a question mark for 1 second, to build the suspense before giving the friends their rating.
screenshot
screenshot
  • Test our your code. When you press buttons A and B together, a question mark should flash onto the screen.

Step 2: Displaying a rating

Show a friendship rating on the screen.

Activity Checklist

  • Click the ‘Globals’ library, and create a new variable called rating.
screenshot
screenshot
  • Drag your variable to the end of your onPressAandB code.
screenshot
screenshot
  • Click the ‘Random’ library, and use the random block to set the rating to a random number between 0 and 100.
screenshot
screenshot
  • Add code to say the rating to the screen.
screenshot
screenshot
  • Finally, after a delay, display the instructions again.
screenshot
screenshot
  • Test your code. Now, if you press buttons A and B together, a random rating should be displayed.

Step 3: Best mates

Let’s display a heart when two friends have a high rating.

Activity Checklist

  • Add an if block to your code, just before the instructions are shown.
screenshot
screenshot
  • You want to display a heart if the rating is high (say over 75). To do this, click the down arrow in your if block.
screenshot
screenshot
  • Drag your rating variable into the left side of your if block, and add the number 75 to the right side.
screenshot
screenshot

Any code inside this if block will run if the rating is over 75.

  • Show a heart image on the screen for 1 second if the rating is over 75.
screenshot
screenshot
  • Test your code. Does an image of a heart show if the rating is over 75? You might have to try a few times to test it!

Challenge: Low rating

Can you display a cross or a sad face for a rating under 25?

Step 4: Adding an animation

Let’s display short animations in your program.

Activity Checklist

  • Here’s the code for displaying a question mark for 1 second before clearing the screen:
screenshot
screenshot
  • To make this question mark flash 5 times, you’ll need to put this code inside a for loop.
screenshot
screenshot
  • Finally, you just need to tell the for loop to start at 1, and count up to 5. This will make the code repeat 5 times.
screenshot
screenshot

Challenge: More animations

Can you make your other images flash on the screen?