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

38 lines
895 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
2016-07-18 23:04:15 +02:00
```sig
radio.onDataReceived(() => { });
```
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.
2016-03-26 00:47:20 +01:00
### Example
2016-06-03 01:24:06 +02:00
This program keeps sending numbers that says how fast the micro:bit is
slowing down or speeding up. It also receives numbers for the same
thing from nearby micro:bits. It shows these numbers as a
[bar graph](/reference/led/plot-bar-graph).
2016-06-02 21:52:03 +02:00
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
```package
microbit-radio
```