Events#

When started#

The When started block is used to run the attached stack of blocks when the project is started.

    when started

A when started event will run when the project is started from the start button in VEXcode VR Blocks.

All new projects will automatically include a when started block.

In this example, the When started block is used to start the project. When the start button is selected, the robot will drive forward, turn right, and drive forward again.

    when started
    drive [forward v] for (200) [mm v] ▶
    turn [right v] for (90) degrees ▶
    drive [forward v] for (200) [mm v] ▶

When bumper#

The When bumper block is used to run the attached stack of blocks when the selected Bumper Switch sensor is pressed or released.

    when [LeftBumper v] [pressed v]

Choose which Bumper to use.

Flowchart illustrating VEXcode VR blocks: When started, When bumper, When timer, When I receive, Broadcast methods.

Select which action will trigger the event - pressed or released.

Image illustrating the When bumper" block in VEXcode VR

In this example, when the Bumper Switch sensor is pressed, the robot will stop moving.

    when [LeftBumper v] [pressed v]
    stop driving

When timer#

The When timer block is used to run the attached stack of blocks when the Brain’s timer is equal to the given value.

    when timer > (1) seconds

The Brain’s timer begins at the beginning of each project or whenever the Brain’s timer is reset.

Choose an amount of time. The when timer event will run once the Brain’s timer is equal to the entered amount.

    when timer > (3.25) seconds

In this example, after 5 seconds have passed, the VR Robot will print the message to the Print Console.

    when timer > (5) seconds
    print [5 seconds have passed.] ▶

When I receive#

The When I receive block is used to run the attached stack of blocks when the selected message is received from a broadcasted message.

    when I receive [message1]

Choose which message to listen for. A new message can also be created.

VEXcode VR Blocks interface showing the When I receive" block in a programming context for robot actions.

In this example, the code will continuously check if the Left Sensor Bumper was pressed. Once pressed, the When I recieve block will trigger once the bumper_press message is received.

    when started
    forever
    if <[LeftBumper v] pressed?> then
    broadcast [bumper_press v]
    end
    end
    when I receive [bumper_press v]
    print [Bumper has been pressed.] ▶

Broadcast#

The Broadcast block is used to broadcast a message to activate any stacks that begin with a When I receive block listening for the broadcasted message.

    broadcast [message1 v]

The stack that sends the broadcast will continue to run blocks at the same time as other stacks that receive the broadcasted message.

Choose which message to broadcast. A new message can also be created.

Image depicting VEXcode VR Blocks for event handling: When started, When bumper, When timer, and When I receive.

In this example, the code will continuously check if the Left Sensor Bumper was pressed. Once pressed, the Broadcast block will trigger the When I receive block with the message bumper_press.

    when started
    forever
    if <[LeftBumper v] pressed?> then
    broadcast [bumper_press v]
    end
    end
    when I receive [bumper_press v]
    print [Bumper has been pressed.] ▶

Broadcast and wait#

The Broadcast and wait block is used to broadcast a message to activate any stacks that begin with a When I receive block listening for the broadcasted message while also pausing the rest of the stack.

This block broadcasts a message to activate any stacks that begin with a when I receive block listening for the broadcasted message while also pausing the rest of the stack.

    broadcast [message1 v] and wait

The stack that sends the broadcast and wait will pause until the other stacks that receive the broadcasted message have completed.

Choose which message to broadcast. A new message can also be created.

Image depicting VEXcode VR Blocks for event handling: When started, When bumper, When timer, and When I receive.

In this example, the code will continuously check if the Left Sensor Bumper was pressed. Once pressed, the Broadcast and wait block will trigger the When I receive block with the message bumper_press. No code underneath the When started block will execute until everything underneath the bumper_press When I receive block is finished.

    when started
    forever
    if <[LeftBumper v] pressed?> then
    broadcast [bumper_press v] and wait
    end
    end
    when I receive [bumper_press v]
    turn [right v] for (90) degrees ▶

When Eye Sensor#

The When Eye Sensor block is used to run the attached stack of blocks when the Eye Sensor detects or loses an object.

    when [FrontEye v] [detects v] an object

The When Eye Sensor event can be used to create actions or behaviors based on whether the Eye Sensor detects an object in range of the sensor or loses an object that was in range of the sensor.

Choose an Eye Sensor.

Diagram illustrating the When Eye Sensor block in VEXcode VR, showing its function in detecting objects.

Choose whether the When Eye detects/loses event is triggered when the sensor detects an object, or loses an object.

Diagram illustrating the When Eye Sensor block in VEXcode VR, showing actions triggered by object detection.

In this example, once the Down Eye Sensor has detected an object, the VR Robot will print a message to the Print Console.

    when [DownEye v] [detects v] an object
    print [An object is detected beneath the robot.] ▶