This time we're going to play with the radio on the Micro:bit. We're also going to use the https://makecode.microbit.org/ editor to see if that makes it easy to get started.
We're going to work our way to a radio based hide-and-seek game. Here are the steps:
- Hello World with Micro:bit and MakeCode
- Send and receive on a button press
- Build a beacon and a detector
- Add in a measure of signal strength
Objective: Run basic code to check your Micro:bit is working.
Steps:
- Find a Micro:bit. Make sure you have a battery pack and USB connector
- Go to: https://makecode.microbit.org/
- Find a "On button A pressed block
- Add in a "Show string" block into the button press block
- Change the message to something interesting
- Try it out using the Micro:bit emulator on the webpage
- Press the "Download" button on the bottom left of the webpage
- Plug in Micro:bit into a USB port on your laptop
- Copy the hex file you downloaded over to the "MICROBIT" drive on your computer
- Press the A button and see if your message displays
Objective: Get the radio to send and receive when buttons are pressed.
Steps:
- Open the code editor at https://makecode.microbit.org/
- Start a new project
- To set up the radio: 1. Find the "on start" block 1. Find the "Radio" collection of blocks 1. Add the "radio set group" block to on start. Leave the value as 1 for now
- To make the receiver: 1. Find the "Radio" collection of blocks 1. Grab the "on radio received receivedString" block 1. Add a block to show the string that has been received 1. Hint: You'll need to look in the "Variables" collection of blocks
- Make the sender 1. Set up a block to trigger when A is pressed 1. Add a "radio send string" block to send out "A"
That's the basic idea, but to make this work better you'll need to find a partner with another Micro:bit to try out the code and think how you can improve it.
Objective: See how far the radio will broadcast. We will set up one Micro:bit to broadcast a "ping" constantly and a second Micro:bit to try to detect it.
Find a partner and decide which one of you will be the beacon and which will be the detector. With your partner agree on a radio channel number to use. This needs to be a number between 0 and 255. This is the number you will put in the "radio set group" block. Try to pick a number that nobody else is using.
For the beacon:
- Make sure you are setting the radio group on start up
- Find the "forever" block
- On the radio send the string "PING"
- Add in a "pause (ms) " block to wait a second before sending another ping
For the detector:
- Make sure you are setting the radio group on start up
- Add a on string received radio block
- Check that the string was "PING" using a block from the "Logic" section
- Hint: Look under the "Advanced" section to find the "Text" blocks to allow you to compare to "PING"
- If it was ping then flash all the LEDs on and off
Once you have this working try moving the Micro:bits apart and see how far the signal can go.
Objective: Improve the detector code to show how close you are to the beacon.
Steps:
- With your partner look at the code blocks for the beacon
- Find the "on radio received" block
- On the top left hand corner of that block click the blue cog icon to get settings menu
- Find the "signal" block and add it just below the receivedNumber block
- You should now be able to use the "signal" value in the radio received block
- The documentation says this signal will be "how strong the radio signal is from -128 (weak) to -42 (strong)."
- Use "show string" and "pause (ms)" to display how strong a signal you are receiving
With your partner again try moving further apart and see what happens to the signal strength. Can you make a hide-and-seek game from this?
Can you improve how you show the signal strength? Maybe look at the "plot bar graph of" block. You'll need to do a bit of maths to signal value to make the graph work.
Short link to this page: https://goo.gl/f1VLHF