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) (makecode.com)

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.

image

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: https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing)

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: https://makecode.microbit.org/_7JcLEq4VFCdJ

Good luck with your traffic light!

Bill

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.

Bill

1 Like