Skip to content

Instantly share code, notes, and snippets.

@davegoopot
Last active June 12, 2018 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davegoopot/934df392571f04208c50b6b51c57da33 to your computer and use it in GitHub Desktop.
Save davegoopot/934df392571f04208c50b6b51c57da33 to your computer and use it in GitHub Desktop.

Manchester CoderDojo Sep 2017 - Micro:bit Radios

Introduction

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:

  1. Hello World with Micro:bit and MakeCode
  2. Send and receive on a button press
  3. Build a beacon and a detector
  4. Add in a measure of signal strength

Hello World With Micro:bit and MakeCode

Objective: Run basic code to check your Micro:bit is working.

Steps:

  1. Find a Micro:bit. Make sure you have a battery pack and USB connector
  2. Go to: https://makecode.microbit.org/
  3. Find a "On button A pressed block
  4. Add in a "Show string" block into the button press block
  5. Change the message to something interesting
  6. Try it out using the Micro:bit emulator on the webpage
  7. Press the "Download" button on the bottom left of the webpage
  8. Plug in Micro:bit into a USB port on your laptop
  9. Copy the hex file you downloaded over to the "MICROBIT" drive on your computer
  10. Press the A button and see if your message displays

Send and Receive on a Button Press

Objective: Get the radio to send and receive when buttons are pressed.

Steps:

  1. Open the code editor at https://makecode.microbit.org/
  2. Start a new project
  3. 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
  4. 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
  5. 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.

Build a Beacon and a Detector

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:

  1. Make sure you are setting the radio group on start up
  2. Find the "forever" block
  3. On the radio send the string "PING"
  4. Add in a "pause (ms) " block to wait a second before sending another ping

For the detector:

  1. Make sure you are setting the radio group on start up
  2. Add a on string received radio block
  3. Check that the string was "PING" using a block from the "Logic" section
    1. Hint: Look under the "Advanced" section to find the "Text" blocks to allow you to compare to "PING"
  4. 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.

Add in a measure of signal strength

Objective: Improve the detector code to show how close you are to the beacon.

Steps:

  1. With your partner look at the code blocks for the beacon
  2. Find the "on radio received" block
  3. On the top left hand corner of that block click the blue cog icon to get settings menu
  4. Find the "signal" block and add it just below the receivedNumber block
  5. You should now be able to use the "signal" value in the radio received block
  6. The documentation says this signal will be "how strong the radio signal is from -128 (weak) to -42 (strong)."
  7. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment