GiggleBot - The Micro:Bit Robot
GiggleBot - The Micro:Bit Robot
Cart 0

micro:bit Programming - Use a While Loop with a robot

logic makecode while loop

In this tutorial we'll show how to use a While loop with the GiggleBot.  

 

What's a While Loop?

A while loop runs some code and makes the robot do something while something is true.  What needs to be true?  Whatever we want to be true.

In English, we might use a while loop as follows:

While a button is pressed, we want the robot to drive forward.

Below, we will try to run code that has the same logic as the statement above.

Using a While Loop

We find a "While Loop" code under the "Loops" section in MakeCode.

while loop in robot code with makecode

Drag this onto the your programming area:

We have a few things here to note.  First, we see the "while" loop, and we see a "true" boolean (true of false block) on the right hand side.  The "boolean" is where we will put our button control.  

Programming a While Loop in MakeCode

Let's put it all together to make this happen:  

While a button is pressed, we want the robot to drive forward.

Start with our while loop.

Next, add the button pressed.

Let's add our command, the robot to drive forward.

Finally, one thing we don't say, but we need the robot to do, is what to do after the button press is no longer TRUE.  In this case, we want to robot to stop.

drive forward and stop the robot

Learn More

See more of our coding tutorials here, and check out the GiggleBot micro:bit robot here.

 


Older Post Newer Post