# Weird values coming out of radio.onReceivedValue - race condition?

**URL:** <https://forum.makecode.com/t/weird-values-coming-out-of-radio-onreceivedvalue-race-condition/33502>\
**Category:** micro:bit\
**Created:** [January 30, 2025, 7:59pm UTC](https://forum.makecode.com/t/weird-values-coming-out-of-radio-onreceivedvalue-race-condition/33502 "2025-01-30T19:59:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cal8](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@cal8](https://forum.makecode.com/u/cal8)\
**Post date:** [January 30, 2025, 7:59pm UTC](https://forum.makecode.com/t/weird-values-coming-out-of-radio-onreceivedvalue-race-condition/33502/1 "2025-01-30T19:59:39Z")

</div>

Hi

I created a basic joystick with xaxis on analog Pin0 and yaxis on analog Pin1.  
the values of these were read and then sent with `radio.SendValue(pinName, value)`.  
on the receiver microbit, these were picked up with `radio.OnReceivedValue()`  
This all worked totally fine.

Now i’m trying to add in a third and fourth analog pin and things are starting to go quite crazy.  
there seems to be a mixup of names and values that get sent. Almost like there’s some kind of race condition…

would appreciate any help

here is the sender code:

```auto
let lesX = 0
let lesY = 0
let yfinal = 0
let lesP2 = 0
let lesP3 = 0
let normP2 = 0
let normP3 = 0

basic.forever(function () {
    lesX = pins.analogReadPin(AnalogPin.P0)
    lesY = pins.analogReadPin(AnalogPin.P1)
    yfinal = (lesY / 1024 - 0.5) * 20
    lesP2 = pins.analogReadPin(AnalogPin.P2)
    lesP3 = pins.analogReadPin(AnalogPin.P3)
    normP2 = -((611 - lesP2) / 611) * 50
    normP3 = ((990 - lesP3) / 990) * 50
    radio.sendValue("xaxis", lesX)
    radio.sendValue("yaxis", yfinal)
    radio.sendValue("P2", normP2)
    radio.sendValue("P3", normP3)
    //basic.showNumber(normP2,100)
})
radio.setGroup(59)

})

```

here is the receiver code:

```js
let divTenVal = 0
let convertedVal = 0

radio.onReceivedValue(function (name, value) {

    if (name == "xaxis") {

        wuKong.setMotorSpeed(wuKong.MotorList.M1, 0 - divTenVal)
    }
    if (name == "P2") {
        wuKong.setMotorSpeed(wuKong.MotorList.M2, value)
    }
    if (name == "P3") {
        wuKong.setMotorSpeed(wuKong.MotorList.M2, value)
    }
    else {
    }
})

radio.setGroup(59)

```

things ive tried:

- delays between `radio.sendValue()`
- moving the calculations from the receiver code to the sender code
- different number in `radio.setGroup()`

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [January 31, 2025, 12:58am UTC](https://forum.makecode.com/t/weird-values-coming-out-of-radio-onreceivedvalue-race-condition/33502/2 "2025-01-31T00:58:53Z")

</div>

Hi @cal8 ,

Is there any more to your receiver? It looks like it never uses the recieved value (just `divTenVal`, which never changes and isn’t based on the received value). And both the `"M2"` and `"M3"` blocks use the same motor, `wuKong.MotorList.M2`, so there’s no way to distinguish between them.

---

<div class="post-metadata">

**Author:** ![cal8](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@cal8](https://forum.makecode.com/u/cal8)\
**Post date:** [February 3, 2025, 7:04pm UTC](https://forum.makecode.com/t/weird-values-coming-out-of-radio-onreceivedvalue-race-condition/33502/3 "2025-02-03T19:04:26Z")

</div>

oh dear this is embarrassing!! an error in 4 lines of code…  
tried 100 things and think i got myself confused in the end  
thanks! got this working in the end 🙂
