Nested if statements with ultrasonic sensors

I used to equip an EV3 with three ultrasonic sensors for the purpose of navigating through a landscape with obstacles. Using nested if statements I had the EV3 stop when it came across an obstacle after which it tested left and right for obstacles with the other two ultrasonic sensors, before turning in one of the directions. This worked fine in the Lego Mindstorms programming environment but I haven’t managed to achieve the same in the MakeCode environment.

So far I haven’t found a way of making nested if statements based on ultrasonic sensor information. Perhaps any of you can see a way of achieving the desired functionality with or without if statements.

I would be really grateful for some help with this.

Perhaps, you could split this question into two parts. One related to the ultrasonic sensor and the other to if statements.

Related to the if statements, I have not observed any nesting restrictions either in the Blocks or JavaScript.

Thank you very much for the response.

I probably need to be more clear in my explanations. Sorry for that! The question in not about nested if-statements and ultrasonic sensors separately, but rather about nested if-statements based on ultrasonic sensor information. For example I have no problem creating nested if statements based on color sensor information. Here is a short example code with nested if-statements polling two color sensors and requiring the one to detect blue color and the other red color before running a motor:


What I am looking for is a way to achieve the same functionality, but with ultrasonic sensors instead. For ultrasonic sensors I can’t find the corresponding hexagonal code inlays (found in the example above), compatible with if-statements. Is there perhaps something I have missed when it comes to if-statements in conjunction with ultrasonic sensors or is there a way of achieving this functionality?

You can show the following JavaScript code in block side to see how it is done

if (sensors.ultrasonic1.distance() < 100) {
// Do Something

In other words, you have to use the comparison logic for the ultrasound sensor.

Thank you so much for helping me out! It’s working and now I can see how it’s done block side too.

To anyone who is involved with the Make Code platform I still would like to put forward a wish. Couldn’t there be a ready hexagonal inlay for ultrasound sensors that fits directly into the if-statement, similar to what exist for say color sensors, instead of having to make it work through the comparison inlay?

I have no problem doing it, but for my students it’s already a great challenge to understand the straightforward way that I am proposing, so I guess they will end up a bit confused, since there is no logical reason why it couldn’t work the same way for ultrasonic sensors as for color sensors, or perhaps there is?

@Rob best to start a new thread for new questions, things get lost.

Is this what you are looking for?


It is quite easy to make your own functions that will make the ultrasonic sensor to have similar usage than the color sensor. For a simple implementation, you can do that in main.ts either by using block or JavaScript. You can hide the implementation and gain some additional features by implementing it in custom.ts using JavaScript.

You should use to be able to use parameters in the function.

Cheers, Ollie

Yes, that is what I’m referring to. I’m wondering if there couldn’t be a similar one for ultrasonic sensors, but perhaps it doesn’t apply to that?

Thank you OliviliK! I will look into that.