Infrared remote for multi-tasking

Hi, i’m trying to use an infrared remote to control my robot for doing different kind of tasks.

Just like by using button A/B for interrupt a loop. However, it doesn’t work. The coding only allow button A to interrupt the loop but not infrared button NUM1. Here’s my simple coding.

Anyone can help to solve it? Many thanks!

Put a “set a to true” in “on start” and test it.
I think otherwise the variable is block-scoped which means in reality you have three different Variables named a and not one which is changed.

Hi MK,

Thanks for your suggestion. However, it doesn’t work. In this simple coding example, the purpose is to show “small heart” when infrared remote control button “NUM1” is pressed, so as to testify the while loop has been interrupted by infrared remote control.

However, as said, it seems only button “A” in microbit can interrupt the while loop not the infrared remote control.

Is there any solution for that?

Many thanks!

Hi Shirley,

Try adding a basic.pause(100) in your while(a) loop. When using while loops, you run the risk of event based items not getting a chance to run. When put a pause in, it gives the scheduler a chance to run.

I also agree with the block-scoped variables. I’d delete all instances and start with a new variable name on start, then add to the others just to be safe.

What IR extension are you using?


Hi Josh,

Thanks for your advice. However, your suggestion also doesn’t work. My IR extension is You may try to see if you can solve this problem. Thank you so much.


For this specific example I think you can just change where you set the icon and omit the loops and flag variable (a). For example, set it to the large heart in on num0 pressed and set it to the small heart in on num1 pressed (and on button A pressed).