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

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:

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:

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()

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.

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 :slight_smile:

1 Like