You are going to code your micro:bit to tell you how compatible you are with your friends.
Instructions: If you’re reading this online, press A+B on the micro:bit below with a friend to find out your friendship rating.
Step 2: Displaying a rating
Show a friendship rating on the screen.
- Click ‘Variables’ and make a new variable called
- Drag a
setblock to the end of your
on button A+B pressedcode and select the
- Click ‘Math’, and use the
pick randomblock to set the rating to a random number between 0 and 100.
- Add code to show the rating on the screen.
- Finally, after a delay, display the instructions again. Remember that you can use right-click then ‘Duplicate’ to copy a block.
- 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.
- Add an
ifblock to your code, just before the instructions are shown.
- You want to display a heart if the rating is high (say over 75). To do this, drag a comparison block from ‘Logic’ to your ‘if’ and select ‘>’:
- Drag your
ratingvariable into the left side of your
ifblock, and add the number
75to the right side.
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.
- 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.
- Here’s the code for displaying a question mark for 1 second before clearing the screen:
- To make this question mark flash 5 times, you’ll need to put this code inside a
repeatloop and set the number of repeats to ‘5’:
Challenge: More animations
Can you make your other images flash on the screen?