Microsoft MakeCode

While Loop not looping

I’m trying to get up to speed on MakeCode for EV3 for FLL. It appears that the While Loop doesn’t actually loop on the EV3 or the simulator. When I make a While Loop for the motor to run when the button on the Touch Sensor = pushed, it won’t stop running when the Touch Sensor /= pushed. From what I can tell, instead of being a loop, the While Loop is acting more like an If True logic block.

Here’s my code:

pauseUntil(() => sensors.touch1.isPressed())
while (sensors.touch1.isPressed()) {
    motors.largeBC.steer(0, 50)
}

Out of curiosity, could you reorganise that loop to test a variable in the while, and then update the variable inside the loop…?

i.e.:

test=sensors.touch1.isPressed()
while (test) {
    motors.largeBC.steer(0,50)
    test=sensors.touch1.isPressed()
}

If that works, then it sounds like the while code is cacheing the result of that isPressed() function call.
Not terribly useful to you, but might be a quick workaround?

My guess would be that steer is permanent, ie it keeps driving until you call steer(0,0). Try adding that after the loop.

A better way to write this would be to have pause until pressed, steer 50, pause until not pressed, steer 0. A yet better way would be to use event handlers instead of while loop.

Well, those solutions didn’t really work with the ev3 blocks in MakeCode upon conversion. But your suggestions did lead me to a possible work around. I added in what Ev3 folks would call “wait blocks” (called Pause in MakeCode) in lieu of using the While Loop. Here’s what I’ve got.

pauseUntil(() => brick.buttonEnter.isPressed())
motors.largeBC.tank(50, 50)
pause(100)
pauseUntil(() => sensors.touch1.isPressed())
motors.stopAll()

@SusanMcN please again your original program at https://makecode.mindstorms.com/beta . We have made fixes to the runtime to handle this kind of loops.