# Microbit ignoring radio message when in "while" loop

**URL:** <https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100>\
**Category:** micro:bit\
**Created:** [December 17, 2020, 5:01pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100 "2020-12-17T17:01:13Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolfman2004](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@Wolfman2004](https://forum.makecode.com/u/Wolfman2004)\
**Post date:** [December 17, 2020, 5:01pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/1 "2020-12-17T17:01:13Z")

</div>

Hi. I have a program where one microbit board is blinking (dim up / down) some neopixel led’s. It should leave the “while” loop when its radio module receives a certain message, thus setting the “Rx\_side\_valg” to eigher “1” or “2”. The “while” loop condition should no longer be present and the program should leave the loop.

However the program stays in the loop and completely ignores the radio input,and just keeps on fading the led’s up and down.

But if it receives the message BEFORE the “while” loop, it jumps over the loop and continues on.

So BEFORE the loop it listens to the incoming radio signals, but completely ignores them in the loop. I have tried changing the delays to 1000us, but still it gets stuck in the loop.

How can I force it to listen to the radio even when it is in the loop?

 ![Microsoft_MakeCode_for_micro_bit_-_2020-12-17_15.10.40](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9781ba8faf180a44ed91f9dabd26c2f3b59d47f6.jpeg)

---

<div class="post-metadata">

**Author:** ![joshkeys19](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/joshkeys19/32/2182_2.png) [@joshkeys19](https://forum.makecode.com/u/joshkeys19)\
**Post date:** [December 18, 2020, 4:46pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/2 "2020-12-18T16:46:08Z")

</div>

Hi wolfman,

Try using a basic.pause instead of a wait. The pause allows the micro:bit scheduler to check for any events whereas I believe the waitMicros will only check if you sleep longer than 50000 us. You can see this in scheduler.cpp:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/d91d3c1bc6cb54e974203c66ceba55e912504570.png)

There you see that when you use the basic.pause( which calls sleep\_ms), it will call the scheduler. If you use waitMicros, it calls the sleep\_us and will internally call the sleep\_ms if us \>50000.

So in short, try using basic.pause instead to allow the scheduler to check for radio events.

---

<div class="post-metadata">

**Author:** ![Wolfman2004](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@Wolfman2004](https://forum.makecode.com/u/Wolfman2004)\
**Post date:** [December 18, 2020, 6:21pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/3 "2020-12-18T18:21:01Z")

</div>

Thank you thank you thank you, joshkeys19.

That did it. Now the board listens for possible incoming radio message.

I just thought that it was a time factor difference. Did not know that the board reacted in different ways to it.

Problem seems solved.

---

<div class="post-metadata">

**Author:** ![joshkeys19](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/joshkeys19/32/2182_2.png) [@joshkeys19](https://forum.makecode.com/u/joshkeys19)\
**Post date:** [December 19, 2020, 4:40pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/4 "2020-12-19T16:40:35Z")

</div>

You’re welcome! Glad I could help. 🙂

---

<div class="post-metadata">

**Author:** ![Wolfman2004](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@Wolfman2004](https://forum.makecode.com/u/Wolfman2004)\
**Post date:** [December 19, 2020, 6:10pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/5 "2020-12-19T18:10:38Z")

</div>

Oh you help alot there.

Mind if I ask for another help?

I am using a second board as a “remote” that sends messages to this first board.

However it remembers if button “A” (example) has been pressed twice, perhaps by mistake.

Is there a way to make the board ignore any buttons pressed for a period of a second or two? I have not been able to find any information on disabling and re-enabling the buttons on the board itself.

---

<div class="post-metadata">

**Author:** ![joshkeys19](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/joshkeys19/32/2182_2.png) [@joshkeys19](https://forum.makecode.com/u/joshkeys19)\
**Post date:** [December 19, 2020, 7:17pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/6 "2020-12-19T19:17:53Z")

</div>

Hi Wolfman,

It sounds like you want a bit of a denouncer enabled. An easy way would be to implement a software denouncer.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9d3ae78179671681afef7d3e3c635cf35ff3d0e4.png)

Your code would be where the hearts are. Every time the button A is pressed, it first checks to see if at least 2000mS have passed since the last time it was pressed.

I hope that helps.

Thanks,  
Josh

---

<div class="post-metadata">

**Author:** ![Wolfman2004](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@Wolfman2004](https://forum.makecode.com/u/Wolfman2004)\
**Post date:** [December 19, 2020, 7:39pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/7 "2020-12-19T19:39:20Z")

</div>

Once again - Thank you.

I will try implementing a debouncer function.

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 22, 2024, 10:23pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/8 "2024-02-22T22:23:46Z")

</div>

Hi @Wolfman2004, Hi @joshkeys19,

I am having exactly the same problem - no radio messages are processed while in a loop.  
However, it doesn’t change behaviour when I increase wait time or change the wait function.

Neither waiting for \> 50000 us works

```auto
control.waitMicros(500001)

```

nor pausing for 5 seconds

```auto
basic.pause(5000)

```

is working.

Before the loop - messages get through. After I end the loop with a press on A button on the device itself, all messages pop in that have been received but not processed during the loop.

Do you have any idea?  
I am using a BBC:micro V2 as sender and receiver.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [February 22, 2024, 10:24pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/9 "2024-02-22T22:24:28Z")

</div>

@Karsten can you please share a link to your code?

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 23, 2024, 7:12pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/10 "2024-02-23T19:12:27Z")

</div>

@richard sure, please find it at

> **[ringbit Car controller](https://makecode.microbit.org/S05952-14191-32197-00440)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

Thanks for taking the time looking into it 🙂

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [February 23, 2024, 7:20pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/11 "2024-02-23T19:20:03Z")

</div>

@Karsten do you have the code for the other micro:bit as well? the one sending the button messages?

---

<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:** [February 23, 2024, 11:51pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/12 "2024-02-23T23:51:30Z")

</div>

Maybe @richard can confirm…but Neopixels may not be very compatible with certain uses of radio. The radio reception may be unreliable in cases like this.

The underlying ws2812 library uses some tight loops in assembly language and [disables interrupts (needed to detect the incoming radio messages)](https://github.com/microsoft/pxt-ws2812b/blob/2e452298bfaab12103ab4c9269f4ceb090a60c23/sendBuffer.asm#L30). It’s possible that there will be few chances to “catch” a radio message in any loop that repeatedly does neopixel operations.

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 23, 2024, 11:56pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/13 "2024-02-23T23:56:27Z")

</div>

The code for the controller unit can be found here:

> **[Remote Control](https://makecode.microbit.org/S42428-43863-79991-76109)**
>
> Erstellt mit ❤️ in Microsoft MakeCode for micro:bit.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [February 23, 2024, 11:58pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/14 "2024-02-23T23:58:16Z")

</div>

@bsiever I’m not particularly familiar with the neopixel library code, unfortunately, but that does sound like a possibility. @peli would know better

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 26, 2024, 5:48pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/15 "2024-02-26T17:48:22Z")

</div>

Well, turns out this is working!  
Movement is now part of the “forever” loop and radio messages are being received.

Sorry, if that is a known design pattern that I should have followed from the start.

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 26, 2024, 6:06pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/16 "2024-02-26T18:06:16Z")

</div>

Thanks @richard and @bsiever for your thoughts.  
I removed all neopixel components from a copy of the file to give it a try.  
Unfortunately, there is still the same behavior.

I am thinking of breaking up the loops to be part of the “forever” block or part of a “each xxx ms” block.

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 26, 2024, 9:30pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/17 "2024-02-26T21:30:30Z")

</div>

so far; final code can be found here:

- [ringbit Car remote controlled](https://makecode.microbit.org/S05952-14191-32197-00440)
- [ringbitCar remote control](https://makecode.microbit.org/S42428-43863-79991-76109)

Thanks for your support!

---

<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:** [February 26, 2024, 11:21pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/18 "2024-02-26T23:21:06Z")

</div>

@Karsten Can you provide a little more detail on the specific behavior you’re seeing with those last two links? Is it that you never change modes as a result of a radio message (`on radio received` doesn’t appear to be working?)

I noticed that you still have Neopixel operations occurring every 100ms and 500ms. That may still disable the radio for substantial time periods. (The radio is being disabled for a moderate amount of time about 12 times per second). Can you try the code without any Neopixel operations at all, like: [https://makecode.microbit.org/S68524-93668-13686-66273](https://makecode.microbit.org/S68524-93668-13686-66273) ?

Bill

---

<div class="post-metadata">

**Author:** ![Karsten](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/karsten/32/13806_2.png) [@Karsten](https://forum.makecode.com/u/Karsten)\
**Post date:** [February 27, 2024, 9:49pm UTC](https://forum.makecode.com/t/microbit-ignoring-radio-message-when-in-while-loop/5100/19 "2024-02-27T21:49:52Z")

</div>

Hi @bsiever, sorry for the confusion. I think my posts got approved in the wrong order. Now it looks like the issue is still not resolved.

I am happy to summarize, should anybody else stumble upon the behaviour in the future:

**Problem**

- When looping within one of my functions; no radio messaged were handled (radio.on\_received blocks).

**Details**

- When ending the loop with one of the physical buttons on the BBC:micro, all the queued received messages got processed.
- No matter which kind of sleep/pause (control.waitMicros, basic.pause) and which amount of delay (up to 5 secs) I used, while being inside my custom function loop no radio interaction was possible.
- Potential influence of neopixel libraries could be precluded by removing all neopixel code from the project. Still the same bahaviour.

**Solution**

- I refactored the code in my custom functions to be part of the standard on\_forever() loop and radio messages are being processed at all times, also with neopixel running at the same time. 👍

Thanks for your different thoughts along the process!
