Events#

When Started#

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

aria-description goes here#
  when started :: hat events  

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

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.

aria-description goes here#
  when started :: hat events 
  drive [forward v] for (200) [mm v] ▶
  turn [right v] for (90) degrees ▶
  drive [forward v] for (200) [mm v] ▶

When Autonomous#

Autonomous mode is used with the Field Control System during competitions. For more information about the V5 Blocks Competition Template, go here.

The When Autonomous block is used to run the attached stack of blocks when an “autonomous” mode signal is received from a competition field or competition switch.

aria-description goes here#
  when autonomous :: hat events 

In this example, the When Autonomous block is triggered when the “autonomous” mode signal is received, making the robot turn to the right for ninety degrees.

aria-description goes here#
  when autonomous :: hat events 
  turn [right v] for (90) degrees ▶

When Driver Control#

Drive control mode is used with the Field Control System during competitions. For more information about the V5 Blocks Competition Template, go here.

The When Drive Control block is used to run the attached stack of blocks when a “driver control” mode signal is received from a competition field or competition switch.

aria-description goes here#
  when driver control :: hat events

In this example, the When Driver Control block is triggered when the “driver control” mode signal is received, enabling the controller.

aria-description goes here#
  when driver control :: hat events
  Controller [Enable v]  

When Bumper#

The Bumper Switch is a 3-Wire Sensor that can be used with V5 Brains. For more information about the Bumper Switch, read Using the V5 3-Wire Bumper Switch v2 & Limit Switch.

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

aria-description goes here#
  when [BumperB v] [pressed v] :: hat events

Choose which Bumper to use.

Image depicting the When Bumper" block used in VEXcode to trigger actions based on bumper switch sensor events.

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

Diagram illustrating the When Bumper" block in VEXcode.

In this example, when the Bumper Switch sensor is pressed, a message will be printed on the Brain.

aria-description goes here#
  when [BumperB v] [pressed v] :: hat events
  print [Bumper has been pressed.] on [Brain v] ◀ and set cursor to next row  

When Limit#

The Limit Switch is a 3-Wire Sensor that can be used with V5 Brains. For more information about the Limit Switch, read Using the V5 3-Wire Bumper Switch v2 & Limit Switch.

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

aria-description goes here#
  when [LimitSwitchA v] [pressed v] :: hat events

Choose which Limit Switch to use.

Image showing the When Limit" block used to trigger actions based on Limit Switch sensor status in VEXcode.

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

Diagram illustrating various event blocks in VEXcode, including When Started, When Autonomous, and When Limit.

In this example, when the Limit Switch sensor is pressed, a message will be printed on the Brain.

aria-description goes here#
  when [LimitSwitchA v] [pressed v] :: hat events
  print [Limit Switch has been pressed.] on [Brain v] ◀ and set cursor to next row  

When Controller Button#

For Controller blocks to appear in VEXcode, a V5 Controller must first be connected to VEXcode V5.

Refer to these articles for more information:

The When Controller Button block is used to run the attached stack of blocks when the selected V5 Controller Button is pressed or released.

aria-description goes here#
  when Controller button [L1 v] [pressed v] :: hat events  

Choose which V5 Controller Button to use.

Image of VEXcode block labeled When Controller Button" for programming controller button events.

Select which action will trigger the event block: pressed or released.

Image showing the When Controller Button" block in VEXcode

In this example, pressing the A Button on the V5 Controller will print a message to the Brain’s screen.

aria-description goes here#
  when Controller button [A v] [pressed v] :: hat events 
  print [A Button was pressed.] on [Brain v] ◀ and set cursor to next row 

When Controller Axis#

For Controller blocks to appear in VEXcode, a V5 Controller must first be connected to VEXcode V5.

Refer to these articles for more information:

The When Controller Axis block is used to run the attached stack of blocks when the selected V5 Controller joystick axis is moved.

aria-description goes here#
  when Controller axis [1 v] is changed :: hat events 

Choose which V5 Controller joystick axis to use.

Image illustrating the When Controller Axis" block used to trigger actions based on joystick movement in VEXcode.

In this example, moving the 4th joystick axis on the V5 Controller will print a message.

aria-description goes here#
  when Controller axis [4 v] is changed :: hat events 
  print [Controller axis 4 moved.] on [Brain v] ◀ and set cursor to next row

When Brain Screen#

The When Brain Screen block is used to run the attached stack of blocks when the V5 Brain’s touchscreen is pressed or released.

aria-description goes here#
  when Brain screen [pressed v] :: hat events

Select which action will trigger the event block: pressed or released.

Diagram illustrating various event blocks in VEXcode, including When Started, When Autonomous, and more.

In this example, pressing the V5 Brain’s touchscreen will make a message appear on the screen.

aria-description goes here#
  when Brain screen [pressed v] :: hat events
  print [You pressed the touchscreen!] on [Brain v] ◀ and set cursor to next row

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.

aria-description goes here#
  when timer > [1] seconds :: hat events

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.

aria-description goes here#
  when timer > [3.25] seconds :: hat events

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

aria-description goes here#
  when timer > [5] seconds :: hat events
  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.

aria-description goes here#
  when I receive [message1 v] :: hat events

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

Flowchart illustrating various event blocks in VEXcode, including When Started, When Autonomous, and more.

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.

Flowchart illustrating various event blocks in VEXcode, including When Started, When Autonomous, and When I Receive.

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.

aria-description goes here#
  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 of VEXcode Blocks illustrating various event triggers for robotics programming.

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.

Flowchart illustrating various event blocks in VEXcode, including When Started, When Autonomous, and When I Receive.

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.

aria-description goes here#
  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 of VEXcode Blocks illustrating various event triggers for robotics programming.

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.

Image illustrating various event blocks in VEXcode, including When Started, When Autonomous, and When Controller Button.

When Optical Sensor#

The Optical Sensor must be connected to the V5 Brain and configured in VEXcode V5 before it is able to be used in projects. For more information about the Optical Sensor, read Using the Optical Sensor with VEX V5.

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

aria-description goes here#
  when [Optical7 v] [detects v] an object :: hat events

Choose an Optical Sensor.

Image of an optical sensor detecting a gesture in a robotics programming context.

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

Flowchart illustrating the events triggered by an optical sensor detecting gestures in VEXcode.

In this example, once the Optical Sensor has detected an object, a message will be printed to the Brain.

aria-description goes here#
  when [Optical7 v] [detects v] an object :: hat events
  print [Optical Sensor has detected an object.] on [Brain v] ◀ and set cursor to next row

When Optical Sensor Detects a Gesture#

When Optical Sensor#

The Optical Sensor must be connected to the V5 Brain and configured in VEXcode V5 before it is able to be used in projects. For more information about the Optical Sensor, read Using the Optical Sensor with VEX V5.

The When Optical Sensor Detects a Gesture block is used to run the attached stack of blocks when the selected Optical Sensor detects a gesture.

aria-description goes here#
  when [Optical7 v] gesture [up v] :: hat events

Important: The When Optical Sensor Detects a Gesture will not work unless the Optical Set Mode block is used to set the Optical Sensor to detect gestures.

aria-description goes here#
  set [Optical7 v] to [color v] mode

Choose an Optical Sensor.

Image of an optical gesture sensor detecting a gesture in a robotics programming environment.

Choose which gesture will trigger the event block.

Flowchart illustrating the When Optical Sensor Detects a Gesture" block functionality in VEXcode Blocks.

In this example, once the Optical Sensor has detected a left gesture, a message will be printed to the Brain.

aria-description goes here#
  when [Optical7 v] gesture [left v] :: hat events
  print [Left gesture detected.] on [Brain v] ◀ and set cursor to next row

When GPS Heading Changed#

The GPS (Game Positioning System) Sensor needs to first be configured to be used in VEXcode V5. For a guide on how to configure the GPS Sensor, read Configuring the GPS Sensor in VEXcode V5. For more information on how the GPS Sensor works, read Using the GPS Sensor with VEX V5.

The When Optical Sensor Detects a Gesture block is used to run the attached stack of blocks when a GPS Sensor detects a heading change.

aria-description goes here#
  when [GPS15 v] heading changed :: hat events

Choose a GPS Sensor.

GPS sensor detecting a heading change, triggering reorientation to 0 degrees in VEXcode Blocks.

In this example, once the GPS Sensor detects a heading change, it will re-orient itself back to face a heading of 0 degrees.

aria-description goes here#
  when [GPS15 v] heading changed :: hat events
  turn to heading (0) degrees ▶