pxt-calliope/docs/projects/messenger.md

67 lines
1.3 KiB
Markdown
Raw Normal View History

2017-12-07 05:23:17 +01:00
# Messenger
2016-08-09 03:12:34 +02:00
![](/static/mb/projects/a9-radio.png)
2017-12-07 05:23:17 +01:00
Use the radio in an app that sends "YO" messages.
2016-08-09 03:12:34 +02:00
## Step 1
2017-12-07 05:23:17 +01:00
Use ``||input:on button pressed||`` to send the number `0` over radio.
2016-08-09 03:12:34 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0);
});
```
## Step 2
2017-12-07 05:23:17 +01:00
Use ``||radio:on data packet received||`` display "YO" when the number ``0`` is received
2016-08-09 03:12:34 +02:00
by radio.
```blocks
let message = 0;
radio.onDataPacketReceived(({ receivedNumber }) => {
message = receivedNumber;
2016-08-09 03:12:34 +02:00
if (message == 0) {
basic.showString("YO")
}
})
```
2017-12-07 05:23:17 +01:00
Download the program to one @boardname@ and then to another. Press button **A** on one and see if the other gets a message.
2016-08-09 03:12:34 +02:00
## Step 3
2017-12-07 05:23:17 +01:00
Use ``||input:on button pressed||`` to send the number `1` over radio.
2016-08-09 03:12:34 +02:00
```blocks
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1);
});
```
## Step 4
2017-12-07 05:23:17 +01:00
Add blocks in ``||radio:on data packet received||`` to display "BYE" when the number ``1`` is received
2016-08-09 03:12:34 +02:00
by radio.
```blocks
let message = 0;
radio.onDataPacketReceived(({ receivedNumber }) => {
message = receivedNumber;
2016-08-09 03:12:34 +02:00
if (message == 0) {
basic.showString("YO")
}
if (message == 1) {
basic.showString("BYE")
}
})
```
2017-12-07 05:23:17 +01:00
Download the program to the @boardname@s again and try your messenger!
2016-08-09 03:12:34 +02:00
```package
radio
2016-08-09 03:12:34 +02:00
```