2018-06-22 23:34:24 +02:00
# Mood Radio
## @description A mini mood messaging app using radio
![@boardname@ sending moods around ](/static/mb/projects/mood-radio.png )
2018-06-25 21:29:58 +02:00
This project uses the [radio ](/reference/radio ) to share your mood with other @boardname@s .
When you press ``A``, your friends will see a **smiley** face. When you press ``B``, they will see a **frowny** face.
2018-06-22 23:34:24 +02:00
## Sending a smiley
The @boardname @ can't understand mood but it is pretty good with numbers. In fact, it can send numbers
2018-06-25 21:29:58 +02:00
between @boardname@s using the radio antenna, just like a phone can send text messages.
2018-06-22 23:34:24 +02:00
2018-06-25 21:29:58 +02:00
Let's add blocks that send a number when button ``A`` is pressed. We assume that `0` is the "mood code" to send for **smiley** .
2018-06-22 23:34:24 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0)
basic.showIcon(IconNames.Happy)
})
```
## Receiving a smiley
2018-10-16 00:32:09 +02:00
We add a ``||radio:on received number||`` block that will run code whenever a new "mood" message comes in.
2018-06-25 21:29:58 +02:00
The ``receivedNumber`` variable contains the numeric value that was sent. Since we've decided that
`0` is **smiley** , we add a conditional ``||logic:if then||`` statement to show this icon.
2018-06-22 23:34:24 +02:00
```blocks
2018-10-16 00:32:09 +02:00
radio.onReceivedNumber(function (receivedNumber) {
2018-06-22 23:34:24 +02:00
if (receivedNumber == 0) {
basic.showIcon(IconNames.Happy)
}
})
```
2018-06-25 21:29:58 +02:00
## Sending a frowny
2018-06-22 23:34:24 +02:00
2018-06-25 21:29:58 +02:00
Adding another mood to our messaging app done in a similar way. We decide that the "mood code" of `1` means **frowny** . We can add a ``B`` button event that sends that code.
2018-06-22 23:34:24 +02:00
```blocks
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1)
basic.showIcon(IconNames.Sad)
})
```
2018-10-16 00:32:09 +02:00
If the ``||radio:on received number||`` block, we add another conditional ``||logic:if then||`` statement to handle the **frowny** "mood code".
2018-06-22 23:34:24 +02:00
```blocks
2018-10-16 00:32:09 +02:00
radio.onReceivedNumber(function (receivedNumber) {
2018-06-22 23:34:24 +02:00
if (receivedNumber == 0) {
basic.showIcon(IconNames.Happy)
}
if (receivedNumber == 1) {
basic.showIcon(IconNames.Sad)
}
})
```
2018-06-25 21:29:58 +02:00
That's it. Download your code to multiple @boardname@s and try it out!
2018-06-22 23:34:24 +02:00
## Challenges
2018-06-25 21:29:58 +02:00
Try adding a new code and use the ``||input:on shake||`` event to send it.
2018-06-22 23:34:24 +02:00
## Full sources
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0)
basic.showIcon(IconNames.Happy)
})
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1)
basic.showIcon(IconNames.Sad)
})
2018-10-16 00:32:09 +02:00
radio.onReceivedNumber(function (receivedNumber) {
2018-06-22 23:34:24 +02:00
if (receivedNumber == 0) {
basic.showIcon(IconNames.Happy)
}
if (receivedNumber == 1) {
basic.showIcon(IconNames.Sad)
}
})
```
```package
radio
```