Using Loops#

Using Loops

This tutorial walks you through using the repeat block to move the 6-Axis Arm up and down in VEXcode CTE. By the end, you’ll have a project that repeats the same two blocks three times.

If you want your arm to repeat the same action multiple times, you could keep duplicating the same blocks over and over. A simpler way, however, is to use the repeat block within the Logic - Control category.

  1. Attach two increment arm position blocks below the when started block. Set the z parameter to 50 on the first block and -50 on the second, to move the arm up, then back down.

Moving the arm up 50 mm, then back down.#
    when started
        increment [arm v] position by x:[0] y:[0] z:[50] [mm v] ▶
        increment [arm v] position by x:[0] y:[0] z:[-50] [mm v] ▶
  1. Attach a repeat block below the when started block.

The repeat block, attached above the increment arm position blocks.#
    when started
        repeat (10)
        end
        increment [arm v] position by x:[0] y:[0] z:[50] [mm v] ▶
        increment [arm v] position by x:[0] y:[0] z:[-50] [mm v] ▶
  1. Drag the increment arm position blocks inside the repeat block.

The increment arm position blocks, now inside the repeat block.#
    when started
        repeat (10)
            increment [arm v] position by x:[0] y:[0] z:[50] [mm v] ▶
            increment [arm v] position by x:[0] y:[0] z:[-50] [mm v] ▶
        end
  1. Select the repeat number and change it to 3, so the blocks inside run three times.

Screenshot of the repeat block's number highlighted, set to 3.
  1. Make sure your arm is connected to VEXcode, then select Start to run the project. The arm will move up and down three times.

Screenshot of the VEXcode CTE toolbar with the Start button highlighted.

You can also repeat a sequence of actions forever, using the forever block instead of repeat.

The forever block.#
    forever
    end

To break out of a loop, use the break block within a loop, to make the code exit the looping block immediately.

The break block.#
    break