Guide the students in creating programs that use the radio communication blocks to send and receive data between two micro:bits.
Notes:
* When using the radio blocks, the micro:bit simulator will show two micro:bits
* In the simulator, a radio transmission icon will appear in the top right corner of the micro:bit. The icon will light up as the micro:bit is transmitting data.
* In the simulator, all the code in the coding workspace runs on both virtual micro:bits. You should include for how to send data as well as what to do when it receives data.
## Marco Polo
Send and receive strings between micro:bits.
On button A pressed, we will send the string Marco and on button B pressed we will send the string Polo.
* When communicating between micro:bits, it is important that the micro:bits involved are all using the same group ID. So, the first thing we will do is set the group ID number.
* From the Radio menu, drag a 'radio set group' block to the coding workspace and place the block into the on start block.
* In the 'radio set group block', leave the default value of 1 for the group ID
```blocks
radio.setGroup(1)
```
* Drag 2 'on button pressed' blocks to the coding workspace
* Leave one with the default value A and change the other button to B
* From the Radio Toolbox drawer, drag 2 'radio send string' blocks to the coding workspace
* Place one 'radio send string' block into the 'on button A pressed' block, and the other'radio send string' block into the 'on button B pressed' block
* In the 'on button A pressed' block, change the default empty string value of the 'radio send string' block to the string "Marco"
* In the 'on button B pressed' block, change the default empty string value of the 'radio send string' block to the string "Polo"
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendString("Marco")
})
input.onButtonPressed(Button.B, () => {
radio.sendString("Polo")
})
```
* To display the data sent between the micro:bits, drag an 'on radio received receivedString' block to the coding workspace
* From the Basic Toolbox drawer, drag a 'show string' block into the 'on radio received receivedString' block
* From the Variables Toolbox drawer, drag a 'receivedString' variable block into the default string value of "Hello" in the 'show string' block
* From the Music Toolbox drawer, drag 2 'play tone' blocks to the coding workspace. See [hack your headphones](/projects/hack-your-headphones) for how to connect a speaker or headphones to the micro:bit.
Depending on the button pressed, send a different number value between micro:bits. On receiving a number, display a different image unique to the number sent. One number will represent a dot, another a dash and another a space or stop.
* Add a 'show string' block to the 'on start' block, to identify the program.
* We choose to change the default string value of "Hello" to the value "Morse Code"
```blocks
radio.setGroup(1)
basic.showString("Morse Code")
```
* Drag 3 'on button pressed' blocks to the coding workspace.
* Leave one with the default value A, change the value in the second block to B, and change the value in the third block to A+B.
* From the Radio Toolbox drawer, drag 3 'radio send number' blocks to the coding workspace.
* Place one radio send number block into each of the 'on button pressed' blocks.
* In the 'on button A pressed' block, leave the default number value of the 'radio send number' block as 0.
* In the 'on button B pressed' block, change the default number value of the 'radio send number' block to the value 1.
* In the 'on button A+B pressed' block, change the default number value of the 'radio send number' block to the value 2.
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0)
})
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1)
})
input.onButtonPressed(Button.AB, () => {
radio.sendNumber(2)
})
```
* From the Radio Toolbox drawer, drag an 'on radio received receivedNumber' event handler to the coding workspace.
* Since we will display a different image depending on the number value received, we need a logic block.
* From the Logic Toolbox drawer, drag an 'if...then' block to the coding workspace and place it in the 'on radio received receivedNumber' event handler.
In order to know whether to display a dot, a dash, or a space/stop image, we need to compare the number received to the values 0, 1, and 2.
* From the Logic Toolbox drawer, drag a 0=0 comparison block into the coding workspace.
* Replace the default value 'true' of the 'if...then' block with the comparison block.
* From the Variables Toolbox drawer, drag a 'receivedNumber' variable block into the coding workspace, and drop it into the first slot of the comparison block
* Leave the righthand side default value of zero in the 0=0 block.
* Press buttons A, B, and A+B together on the micro:bit
Challenge question: Can our code be made more efficient?
* Whenever you look over a program and see the same lines of code repeated, there is usually a chance to improve the code making it more efficient by reducing the number of lines of code
* What lines are repeated in our program? If...then, pause, clear screen
* Can we edit the code to use only one 'if...then' block, one 'pause' block, and one 'clear screen' block? Yes!
## Making our code more efficient
Remind students that they can edit the 'if...then' block, adding as many 'else if' conditions as needed.
![Add else-if to if-then block](/static/courses/csintro/radio/if-then-else-if.png)
A final else
In a conditional that might receive a number of different values, it is good coding practice to have a catch-all 'else' clause. In our example, if any number value other than the ones we coded for (0,1, and 2) is received, we can signal the user that an error has occurred by using a 'show icon' block to display an X.
The pause and clear screen
Rather than repeat these lines of code 3 times, we can move the 'pause' block and the 'clear screen' block outside of the edited 'if...then…else' block.
Now our program runs as we designed it to run and is more efficient, too!