pxt-calliope/docs/projects/messenger.md

66 lines
1.2 KiB
Markdown
Raw Normal View History

2016-08-09 03:12:34 +02:00
# messenger
![](/static/mb/projects/a9-radio.png)
Use the radio to create an app that sends "YO" messages.
## Step 1
Use [on button pressed](/reference/input/on-button-pressed) to send the number "0" over radio.
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0);
});
```
## Step 2
Use [radio on data received](/reference/radio/on-data-received) display "YO" when the number ``0`` is received
by radio.
```blocks
let message = 0;
radio.onDataReceived(() => {
2016-08-09 05:44:43 +02:00
message = radio.receiveNumber();
2016-08-09 03:12:34 +02:00
if (message == 0) {
basic.showString("YO")
}
})
```
Download the program and **upload the same .hex file to 2 devices!**
2016-08-09 03:12:34 +02:00
## Step 3
Use [on button pressed](/reference/input/on-button-pressed) to send the number "1" over radio.
```blocks
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1);
});
```
## Step 4
Add blocks in [radio on data received](/reference/radio/on-data-received) to display "BYE" when the number ``1`` is received
by radio.
```blocks
let message = 0;
radio.onDataReceived(() => {
2016-08-09 05:44:43 +02:00
message = radio.receiveNumber();
2016-08-09 03:12:34 +02:00
if (message == 0) {
basic.showString("YO")
}
if (message == 1) {
basic.showString("BYE")
}
})
```
```package
microbit-radio
```