Does anybody know why i have input delay when sending inputs via radio to my other microbit? its for a school project

heres the code
def on_received_number(receivedNumber):
if receivedNumber == 0:
basic.show_leds(“”"
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
“”“)
elif receivedNumber == 1:
basic.show_leds(”“”
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .
“”“)
elif receivedNumber == 2:
basic.show_leds(”“”
. . # . .
. . . # .
# # # # #
. . . # .
. . # . .
“”“)
elif receivedNumber == 3:
basic.show_leds(”“”
. . # . .
. # # # .
# . # . #
. . # . .
. . # . .
“”")
radio.on_received_number(on_received_number)

radio.set_group(1)
while True:
if input.button_is_pressed(Button.AB):
radio.send_number(3)
elif input.button_is_pressed(Button.A):
radio.send_number(1)
elif input.button_is_pressed(Button.B):
radio.send_number(2)
else:
radio.send_number(0)

im thinking it might be the while true loop?

I can’t get that code to go into MakeCode. A share link would be better. I can see what you are trying to do with that while loop and yes that is the problem. You want to return to a blank screen after pushing a button (or two).

Presumably you have this running on two micro:bits with each one able to affect the other. A while loop is never a good idea like that. Use the event handlers instead.

There are some timing issues involved in using that loop. You can send so many 0 messages that the receiver gets behind and there is a delay as it tries to get through all the 0 before it can see a 1, 2, or 3. It is best to not send that many messages. You may want to explicitly send a clear screen message when the micro:bit is shaken or something.

If that doesn’t work for you consider a forever loop that knows when to clear the screen based on the last time a message was sent. Since this is for school I leave that up to you.

2 Likes