1.5 Sending Messages
-
Computers can achieve a lot more when they talk to each other. Your remote control talks to the TV, and a smartwatch is useful because it talks to your phone. The micro:bits can talk to each other too, and send different types of information.
We’re going to use the built-in radio, combined with all the other things you’ve learned so far, to send some information between multiple micro:bits. -
One of the useful things about the micro:bit is that it has a radio built right into the board. This radio component enables the micro:bit to send and receive messages to other micro:bits in the same radio group. To use the radio, we need to use multiple micro:bits.
Here are some tips on how to use the micro:bit radio:- You can find all of the code blocks for the radio in the Radio menu in MakeCode.
- Use the radio set group block to give your radio a group number. Multiple micro:bits with the same radio group number will communicate with one another and will ignore messages from micro:bits in other groups.
- Use the radio send number, radio send value, and
radio send string blocks to send a message over the radio to other micro:bits in your radio group. - Use the on radio received blocks to trigger an action when your micro:bit receives a specific message. There are separate blocks for receiving numbers, values, and strings.
- You can find all of the code blocks for the radio in the Radio menu in MakeCode.
-
The Challenge
- When you press the A button on one micro:bit, the text "Hello World!" is shown on a different micro:bit.
Required Devices
- 2 micro:bits
Suggested Blocks
- on start
- show string
- on button pressed
- radio set group
- radio send string
- on radio receivedString
Hints
- Remember to make sure that your micro:bits are in the same radio group and that you set your radio group before you try to send and receive radio messages.
- When you use an on radio received block, the receivedNumber and receivedString blocks are variables. You can put the receivedNumber block anywhere you can put a number, and the receivedString block anywhere you could put a string.
Bonus Challenges
- Can you trigger your message by sending a number over the radio instead of strings?
- What happens when you have three or more micro:bits in the same radio group, instead of just two?
- Can you figure out the range of the radio in your micro:bit? How far can you move the micro:bits from one another before the message stops getting through?
-
Sometimes, it’s helpful to compare strings with one another and use that comparison to decide whether to take a certain action or not. In the same way that we can compare numbers using a number comparison block, we can also compare strings using a string comparison block.
You can use the “ “ = “ “ block to check whether two strings are equal, or not equal, to one another. By pairing that block with an if true then block you can create a program that takes different actions based on whether strings are identical to one another.
This becomes very useful when you combine it with the idea of variables. We can store strings in variables, just like we can store numbers in them. We can then use a string comparison block to check whether the contents of the variable matches any of a number of strings that we’re interested in.
For example, you could create a variable called “favourite colour”. That variable might store colour names, like red, yellow, green or blue. Using an if true then block, you could check what string is currently stored in the favourite colour variable and then take some action based on that information. For example, you might change the colour of an RGB LED to match the colour that has been stored.
-
The Challenge
- When you press the A button on the first micro:bit, you send a single word to the second micro:bit.
- When the second micro:bit receives the single word, it shows a full sentence on its display.
Required Devices
- 2 micro:bits
Suggested Blocks
- on start
- show string
- on button pressed
- on radio receivedString
- radio set group
- radio send string
- if true then else
- " " = " "
Hints
- If you put the same code on both micro:bits, your messages and triggered actions should work in both directions.
Bonus Challenges
- Can you make the micro:bit do something else when it receives your chosen code word? What about triggering a sound or animation instead?
- Can you create multiple code words and have the receiving micro:bit do different things based on which code word it receives?
-
The Challenge
Work in a larger group with two micro:bits between you.
- When you press the A button on each micro:bit, display a unique icon on both micro:bits.
- When you press the B button on each micro:bit, display a unique icon on both micro:bits.
Required Devices
- 2 micro:bits
Suggested Blocks
- on start
- show icon
- on button pressed
- radio set group
- radio send string
- on radio receivedString
- if true then else
- " " = " "
Hints
- You will need to put slightly different code on each of your micro:bits to solve this challenge.
Bonus Challenges
- Once you have this working with 2 micro:bits, can you combine with another group to get it working with 4 micro:bits instead? Make sure you don't use the same icon twice.
-
In this lesson, you should have learned how to use the micro:bit radio to send messages from one micro:bit to another, and how to use code words to trigger actions.
You've finished Module 1! In the next module we'll learn about the kinds of devices and programs that make up a Smart Home. We'll explore how to make a Smart Doorbell, an Automatic Fan, an Automatic Pet Feeder, and a Smart Light.