Can i force my program to check the "if" section while it's running the "else" segment?

so, i have a traffic light system that when it receives a specific number through radio, it will change it to green + amber at the same time, but i’ve noticed it only checks for that number being received when it finishes what’s in the “else” loop, but I need/want it to change to green + amber as soon as it receives the number. is anybody able to help with this?

1 Like

When you introduce pauses in your code, you create something called blocking code. The program kinda gets stuck in the else clause and won’t “let go” until it reaches the end, pauses and all.

What you want to use, in JavaScript terms, is something called setTimeout. This queues up a piece of code to run at a later time, but lets your main code (your else clause, in this instance) complete without waiting. We have an extension for MakeCode Arcade:

Timers 1.1.0 (microsoft/arcade-timers) (

Right now, though, you can’t use that extension in MakeCode for micro:bit.

@jwunderl , @richard , et al.: I don’t see anything in the code that ties it specifically to MCA. Do you think we can open that extension up to all platforms? I’m going to play around with this later in the week (looking like Thursday) and, if I get it to work, then I’ll submit a pull request on that repo.

1 Like

Hmmmm… This was unexpected.


Hi @cpull31,

This can be a pretty tricky to do. Below I share a link to an example program that does something similar that you may want to study/test.

The example program has two different behaviors. It’ll either switch between a big and little heart every 5 seconds or switch between a big and little square every 5 seconds. It uses a mode variable to figure out which picture to show next. Since there are 4 pictures, there are 4 modes, numbers 0, 1, 2, and 3.

It uses the current microbit time (millis) and a variable (startTime) to keep track of when 5 seconds have passed since the last time it updated the picture. If the current time - startTime > 5s, it’ll do an update. This approach is sometimes called Delta Timing. (It’s often done with Arduinos:

In my example the radio can be used to cause it to change which mode it’s in. Button A will change the mode immediately to the heart modes. Button B will change it to the square modes when the next 5s ends.

Here’s the example:

Good luck with your traffic light!


1 Like

Hmmm … you’ve given me an idea, @bsiever !

It appears that setTimeout() and its friends are not available on the micro:bit, but it might be possible to reimplement them with delta timing. I use delta timing all the time in Arcade; it’s a really useful technique.

I won’t be able to look at this again until next week, but I’ll see if I can implement a variation of the Timers blocks on the micro:bit with this in mind.

Stay tuned!

Hi @AlexK,

The every X block (JavaScript: loops.everyInterval) in the loops toolbox already does basic delta-timing, but it can’t be made to trigger immediately (which I did in my example). It also isn’t quite the right structure to chain together events (like do this, then in X seconds do this, etc.).

You can also do some cool things with control.inBackground. You could create a block set to schedule one-time events (do x in y seconds) and repeating events (do x every y seconds, like setTimeout). You could use an array of structures or parallel arrays to keep track of when an event should trigger and the code to call.

It’d be great to create a more powerful version of timers as an extension.


1 Like

I’ve made a first pass at a port of the arcade-timers extension to the micro:bit environment. You get the same functionality as in the MakeCode Arcade version in Blocks. As a bonus, I have exported the support functions so that you can use timer.clearInterval(), timer.clearTimeout(), timer.setInterval(), and timer.setTimeout() in JavaScript and Python just as their MakeCode Arcade counterparts.

Install the extension with this URL:

Devs: Feel free to do a code review and submit pull requests. If you’d like me to transfer ownership of the repo so that you can use this to build an official extension, then just let me know.

1 Like