Control#

Introduction#

The Control blocks in VEXcode manage the flow of a project by handling loops, conditions, and delaying blocks from running.

A loop is when a computer repeats the same step or group of steps over and over until it is told to stop. A condition is a rule the computer checks to decide what to do next. For example, a light can turn on if the arm is moving an object.

Below is a list of available blocks:

  • wait – Pauses blocks from running for a specific duration.

  • wait until – Pauses blocks from running until a specified condition is met.

  • repeat – Repeats a set of blocks a specific number of times.

  • forever – Repeats a set of blocks indefinitely.

  • repeat until – Repeats a set of blocks until a condition is met.

  • while – Repeats a set of blocks while a condition remains true.

  • if – Runs a set of blocks if a condition is true.

  • if / else – Runs one set of blocks if a condition is true, otherwise runs another.

  • if / else if / else – Runs different sets of blocks depending on multiple conditions.

  • break – Exits a repeat, forever, repeat until, or while loop immediately.

  • stop project – Ends the project.

wait#

The wait stack block pauses blocks from running for a specific amount of time before moving to the next block.

wait stack block#
    wait (1) seconds

Parameters

Description

time

The amount of time to wait, as a positive whole number or decimal in seconds.

Example

When started, moves forward for one second, then stops all movement.#
    when started
    [Move to the left, then the right after one second.]
    increment [arm v] position by x:[100] y:[0] z:[0] [mm v] ▶
    wait (1) seconds
    increment [arm v] position by x:[-100] y:[0] z:[0] [mm v] ▶

wait until#

The wait until stack block pauses blocks from running until a specified condition is met before moving to the next block.

wait until stack block#
    wait until <>

Parameters

Description

condition

The condition that must be met before moving on to the next block.

repeat#

The repeat C block runs the blocks inside it a set number of times.

repeat c block#
    repeat [10]
    end

Parameters

Description

times

A whole number that sets how many times the repeat block runs.

Example

    when started
    [Move in a square pattern.]
    repeat [4]
        increment [arm v] position by x:[2] y:[0] z:[0] [inches v] ▶
        increment [arm v] position by x:[0] y:[2] z:[0] [inches v] ▶
        increment [arm v] position by x:[-2] y:[0] z:[0] [inches v] ▶
        increment [arm v] position by x:[0] y:[2] z:[0] [inches v] ▶
    end

forever#

The forever C block keeps running the blocks inside it again and again without stopping.

forever c block#
    forever
    end

Parameters

Description

This block has no parameters.

Example

When started, continuously moves in a square path by moving forward 50 mm and turning right 90 degrees in an infinite loop.#
    when started
    [Move in a square path forever.]
    forever
        increment [arm v] position by x:[2] y:[0] z:[0] [inches v] ▶
        increment [arm v] position by x:[0] y:[2] z:[0] [inches v] ▶
        increment [arm v] position by x:[-2] y:[0] z:[0] [inches v] ▶
        increment [arm v] position by x:[0] y:[2] z:[0] [inches v] ▶
    end

repeat until#

The repeat until C block runs the blocks inside it repeatedly while the specified condition is not met.

repeat until block#
    repeat until <>
    end

Parameters

Description

condition

An expression or variable that is checked before each loop. If it is False, the blocks continue repeating. If it is True, the loop will stop.

Example

When started, keeps all LEDs red until the screen is pressed, then turns them off.#
    when started
    [Increment along the x-axis until it's no longer possible.]
    repeat until <not <[arm v] increment position by x:[100] y:[0] z:[0] [mm v] ?>>
        increment [arm v] position by x:[100] y:[0] z:[0] [mm v] ▶
    end
    set [arm v] to control stopped

while#

The while C block runs the blocks inside repeatedly while the specified condition is met.

while c block#
    while <>
    end

Parameters

Description

condition

An expression or variable that is checked before each loop. If it is True, the blocks continue repeating. If it is False, the loop will stop.

Example

A stack of blocks that begins with a when started block, followed by a comment block reading Display the time after 2 seconds have passed. A wait until block pauses execution until the timer reaches or exceeds two seconds. Once this condition is met, a print block displays the timer value on the screen.#
when started
[Display the 6-Axis Arm's position as it moves.]
move [arm v] to position x:(-100) y:(200) z:(100) [mm v] ◀ and don't wait
while <not <[arm v] is done moving?>>
print ([arm v] position [y v] in [mm v]) on console ◀ and set cursor to next row
wait [0.25] seconds

if#

The if C block runs the blocks inside if the condition is True.

if block#
    if <> then
    end

Parameters

Description

condition

An expression or variable that is checked when the statement runs. If it is True, the blocks inside the if block will run. If it is False, the blocks are skipped over.

Example

when started
[Check if the 6-Axis Arm can move to a position.]
if <not <[arm v] move to position x:[0] y:[0] z:[0] [mm v] ?>> then
print [The 6-Axis Arm can't move to this position.] ◀ and set cursor to next row

if / else#

The if / else C block determines which set of blocks runs based on whether the condition is True or False.

if then else block#
    if <> then
    else
    end

Parameters

Description

condition

An expression or variable that is checked when the statement runs. If it is True, the blocks inside the if block will run. If it is False, it will runs the blocks inside the else block.

Example

when started
[Check if the 6-Axis Arm can or can't move to a position.]
if <not <[arm v] move to position x:[100] y:[100] z:[100] [mm v] ?>> then
print [The 6-Axis Arm can't move to this position.] on console ◀ and set cursor to next row
else
print [The 6-Axis Arm can move to this position.] on console ◀ and set cursor to next row

if / else if / else#

The if / else if / else expandable C block selects which set of blocks runs based on conditions:

  • if runs its block of code if the condition evaluates as True.

  • else if checks additional conditions only if all previous conditions evaluated as False. Multiple else if statements can be used.

  • else runs its block of code only if none of the previous conditions evaluated as True.

if else if else block#
    if <> then
    else if <> then
    else 
    end

Parameters

Description

condition

An expression or variable that is checked when the statement runs. The first condition that is True runs that set of blocks. If all conditions evaluate as False, the blocks inside the else block will run.

Example

when started
[Move until the 6-Axis Arm is at the corner of the tile.]
if <[arm v] increment position by x:[50] y:[0] z:[0] [mm v] ?> then
increment [arm v] position by x:[50] y:[0] z:[0] [mm v] ▶
else if <[arm v] increment position by x:[0] y:[50] z:[0] [mm v] ?> then
increment [arm v] position by x:[0] y:[50] z:[0] [mm v] ▶
else
print [The 6-Axis Arm has reached the corner.] on console ◀ and set cursor to next row

break#

The break stack block exits a loop immediately. This block can be used inside repeat, repeat until, while, and forever blocks. Break is useful when a loop needs to stop early based on something that happens during the project, like a button being pressed or a sensor detecting an object.

break stack block#
    break

Parameters

Description

This block has no parameters.

stop project#

The stop project stack block ends a running project.

stop project block#
    stop project

Parameters

Description

This block has no parameters.