Uso de bucles#
This tutorial walks you through using the repeat block to drive your robot in a square in VEXcode IQ (1st gen). By the end, you’ll have a project that repeats the same two blocks four times to complete the square.
If you want your robot to repeat the same action multiple times, like driving in a square, you could keep duplicating the same blocks over and over until they move in the pattern of a square. A simpler way, however, is to use the repeat block within the Logic - Control category.
Attach a drive for block and a turn for block below the when started block.
cuando inicie
conducir [adelante v] por (200) [mm v] ▶
girar [derecha v] por (90) grados ▶
Attach a repeat block below the when started block.
cuando inicie
repetir (10)
fin
conducir [adelante v] por (200) [mm v] ▶
girar [derecha v] por (90) grados ▶
Drag the drive for and turn for blocks inside the repeat block.
cuando inicie
repetir (10)
conducir [adelante v] por (200) [mm v] ▶
girar [derecha v] por (90) grados ▶
fin
Select the repeat number and change it to 4, so the blocks inside run once for each side of the square.
Make sure your robot is connected to VEXcode, then select Download to download the project to the Brain.
Select Run to run the project. The robot will drive in a square.
You can also repeat a sequence of actions forever, using the forever block instead of repeat.
por siempre
fin
To break out of a loop, use the break block within a loop, to make the code exit the looping block immediately.
interrumpir