pxt-calliope/docs/reference/radio/on-data-received.md

31 lines
883 B
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# On Data Received
2016-06-02 21:52:03 +02:00
Run part of a program when the micro:bit receives a
[number](/reference/types/number) or [string](/reference/types/string) over ``radio``.
2016-03-26 00:47:20 +01:00
### Parameters
2016-06-02 21:52:03 +02:00
* **body** is the part of the program to run when the micro:bit receives information over ``radio``.
2016-03-26 00:47:20 +01:00
### Example
2016-06-02 21:52:03 +02:00
This program never stops. It keeps sending numbers that says how
fast the micro:bit is slowing down or speeding up. It also receives
numbers that say how fast nearby micro:bits are going. It
shows these numbers as a [bar graph](/reference/led/plot-bar-graph).
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
radio.sendNumber(input.acceleration(Dimension.X));
})
radio.onDataReceived(() => {
led.plotBarGraph(radio.receiveNumber(), 1023);
})
```
### See also
2016-06-02 21:52:03 +02:00
[receive number](/reference/radio/receive-number),
[send number](/reference/radio/send-number), [set group](/reference/radio/set-group)
2016-03-26 00:47:20 +01:00