on Received Number

Run part of a program when the micro:bit receives a number over radio.

radio.onReceivedNumber(function (receivedNumber) {})

Parameters

Watch this video to see how the radio hardware works on the micro:bit:

Examples

Tell me how fast

This program keeps sending numbers that say 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.

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 micro:bits.

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, received packet, send number, send string, send value, set group

radio