pxt-calliope/docs/reference/radio/receive-number.md

69 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Receive Number
Receives the next number sent by a micro:bit in the same ``radio`` group.
2016-03-26 00:47:20 +01:00
2016-07-18 23:04:15 +02:00
```sig
radio.receiveNumber();
```
2016-06-10 00:30:47 +02:00
### Returns
2016-03-26 00:47:20 +01:00
* the first [number](/reference/types/number) that the micro:bit received. If it did not receive any numbers, this function will return `0`.
2016-03-26 00:47:20 +01:00
2016-06-10 00:30:47 +02:00
### Simulator
This function only works on the micro:bit, not in browsers.
### Example: Simple number receiver
2016-03-26 00:47:20 +01:00
This example receives the number broadcasted another micro:bit and shows it
as a bar graph.
2016-03-26 00:47:20 +01:00
```blocks
radio.onDataReceived(() => {
led.plotBarGraph(radio.receiveNumber(), 1023);
})
```
### Example: Light level receiver
2016-06-15 13:55:19 +02:00
This example shows the light level from the [light level sender example](/reference/radio/send-number)
as a number.
```blocks
radio.setGroup(99)
basic.forever(() => {
let level = radio.receiveNumber()
basic.showNumber(level)
})
```
### Example: Mailbot
2016-06-15 13:55:19 +02:00
This example receives the light level from the [light level sender example](/reference/radio/send-number)
and shows a text string like **ALERT** if the light level becomes much brighter.
To find when the mail arrives, you can put the light level sender in your mailbox and it will
tell you when someone opens the box. You can try this with a normal
box too, like a present for a friend.
```blocks
radio.setGroup(99)
let max = 0
basic.forever(() => {
let level = radio.receiveNumber()
if (level > max) {
max = level
}
if (max > 10) {
basic.showString("ALERT")
}
})
```
2016-03-26 00:47:20 +01:00
### See also
2016-06-15 13:55:19 +02:00
[send number](/reference/radio/send-number), [on data received](/reference/radio/on-data-received)
2016-03-26 00:47:20 +01:00
```package
radio
```