22 tra Send and receive
create 2 programs for 2 micro:bits – sender and receiver. Micro:bit sender needs to send your name when button A is pressed, and your age when button B is pressed – it will display a yes icon when the message is sent. Second micro:bit plays a melody when it receives a number or text message which is than displayed on its screen. Micro:bit can communicate wirelessly with other micro:bits using radio waves – it is a way of transmitting and receiving messages over a distance. To avoid interference, micro:bits that communicate with each other need to be set to the same radio group (0-255).
Micro:bit sender
1.
Inside the on start block, place the radio set group command from the Radio category. Change radio group number 1 to number 5. Put the show icon command afterwards to display a smiley.
2.
From the Input category take the on button A pressed block and inside place the radio send string command from the Radio category. In the empty field of the radio send string command write your name. To know that the message is sent, display the yes icon for 100 milliseconds.
3.
Again, take the on button A pressed block and choose button B from the dropdown menu. Place the radio send number command from the Radio category. In the empty field of the radio send number command write your age. To know that the message is sent, display the yes icon for 100 milliseconds.
Micro:bit receiver
1.
Inside the on start block, place the radio set group command from the Radio category. Change radio group number 1 to number 5.
2.
From the Radio category select the on radio received receivedNumber block. Inside it, place the command play melody jump up from the Music category. To show the received message, place the show number 0 from the Basic category and in place of number 0, drag-and-drop the receivedNumber variable from the on radio received receivedNumber block. Use the pause and clear screen commands to display the message for 1 second.
3.
From the Radio category select the on radio received receivedString block. Inside it, place the command play melody jump up from the Music category. To show the received message, place the show string Hello from the Basic category and in place of Hello, drag-and-drop the receivedStringvariable from the on radio received receivedString block. Use the pause and clear screen commands to display the message for 1 second.
Although your programs are created using Blocks, you’ll need to copy the JavaScript code of your created program for solving certain escape room tasks. After you create a program in MakeCode Blocks editor, you can see its JavaScript version by going to JavaScript mode.
Copy the JavaScript code from your Send and receive program and paste it into the box below:
This publication reflects the views only of the author, and the Commission cannot be held responsible for any use which may be made of the information contained therein.