pxt-calliope/docs/reference/radio/on-received-number.md
Peli de Halleux 2b504d863d
Radiodocsupdate (#1430)
* a few updates

* more updates

* reorder radio blocks

* more shuffling of new radio apis

* fixing hot or ocold

* more doc fixes

* more updates

* fixing docs issues

* more doc fixes

* restore docs errors

* missing packate

* renamed argument of callback

* mssing radio

* more odcs fixes

* lock turtle

* ignore docs for now
2018-10-15 15:32:09 -07:00

1.7 KiB

on Received Number

Run part of a program when the @boardname@ receives a number over radio.

radio.onReceivedNumber(function (receivedNumber) {})

Parameters

Examples

Tell me how fast

This program keeps sending numbers that say how fast the @boardname@ is slowing down or speeding up. It also receives numbers for the same thing from nearby @boardname@s. It shows these numbers as a bar graph.

basic.forever(() => {
    radio.sendNumber(input.acceleration(Dimension.X));
})
radio.onReceivedNumber(function (receivedNumber) {
    led.plotBarGraph(receivedNumber, 1023);
})

What's the distance?

This program uses the signal strength from received packets to graph the approximate distance between two @boardname@s.

basic.forever(() => {
    radio.sendNumber(0)
})
radio.onReceivedNumber(function (receivedNumber) {
    led.plotBarGraph(
        Math.abs(radio.receivedPacket(RadioPacketProperty.SignalStrength) + 42),
        128 - 42
    )
})

Troubleshooting

The ||radio:onReceivedNumber|| event can only be created once, due to the hardware restrictions.

The radio set group might need to be set, synchronized , before the radio events will function.

See also

on received string, send number, send string, send value, set group

radio