Custom Controller Code in VEXcode IQ

Using the Controller can make it easier to drive and move your robot to complete a task. However, there are limitations to using the Drive program, and depending on your robot build or the task at hand, you may want different controls. Coding the Controller allows you to optimize the Controller to make it fit your robot and the task at hand better. There are several ways to code the Controller in VEXcode IQ. Each has its advantages and limitations, and some methods are better suited for certain situations depending on the desired outcome.

This article will walk you through three different options for custom coding the Controller in VEXcode IQ. Each method will be described with its advantages, limitations, and an example use case to help guide you when choosing a method. For the purposes of this article, all code examples shown were created for the Clawbot. However, the same concepts could be applied to numerous other builds found on builds.vex.com, and custom builds.

Option 1: Assigning Buttons in the Device Configuration

This option is great when you are using a standard build, like a BaseBot or Clawbot, and want to get up and running quickly.

This option lets you assign motors, a drivetrain, or motor groups to buttons on the Controller in the Device Configuration. For more information about how to assign buttons to the Controller in the Device Configuration, view this article.

Screenshot of a VEX IQ Blocks tutorial interface, showcasing programming blocks and tools designed for educational robotics, aimed at helping beginners understand and utilize the VEX IQ platform effectively.

Summary of Option 1: Assigning Buttons in the Device Configuration

Advantages

Limitations

Example Situation

  • Quick setup with no coding needed
  • Simplest method
  • Easily adjustable
  • Can assign individual motors, a standard drivetrain, and motor groups to buttons
  • Button assignments are limited to the number of buttons on the Controller
  • Drivetrain cannot exceed 4 motors or be customized (only a standard drive is supported, not an H drive, holonomic, or other custom drivetrain)
  • Manipulating a standard build such as a BaseBot, Clawbot, or a simple modification of those standard builds. For example, a BaseBot with an intake attached to the front controlled by a motor group.
  • Manipulating which buttons correspond to different behaviors on the robot quickly without much coding

Option 2: Using a Forever Loop

If you are using a custom build instead of a standard build, or want to be able to have more customization in your Controller, this option is a good one. Using a Forever loop is a great introduction to create custom code for your Controller.

This option places all of the conditions for the Controller, and its associated buttons, in a Forever loop. This provides more flexibility, especially with custom build designs, but also requires some coding experience. One consideration when using this option however, is the length and complexity of your project. The more conditions that are added, the longer the code stack may become. This means that multiple blocks have to be executed in order, and when there are a lot of blocks, this can slow down project execution. Slower project execution may create a lag between pressing Controller buttons and seeing the robot behavior.

The specific example shown below is one way that you can use a Forever loop with a custom design robot (such as a robot with a custom drivetrain) to drive the robot and manipulate the claw and arm in order to interact with objects.

Diagram illustrating the VEX IQ Blocks programming interface, showcasing various blocks and their functions for educational robotics projects, as part of the Blocks Tutorials section in the VEX IQ knowledge base.

Download the "Option 2" VEXcode IQ (2nd generation) project file >

Note: If using a 1st Gen Clawbot the arm motor will need to be reversed in the Device Configuration to work as intended in the project above.

Explanation of Option 2 Code.

Code Piece

Explanation

Screenshot of a VEXcode IQ Blocks tutorial interface, showcasing programming blocks and options for VEX IQ Robotics, designed to assist educators and students in learning robotics concepts.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

Screenshot of a VEX IQ Blocks tutorial interface, showcasing programming blocks and coding elements designed for educational robotics, aimed at beginners and educators. Diagram illustrating the VEX IQ Blocks programming interface, showcasing various programming blocks and their functions for educational robotics projects.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

A Forever loop is used in order to continuously check for which buttons are being pressed on the Controller.

The [Set motor velocity] blocks are used to set the motor velocity to the current Controller’s position along the A and D axes. This is the equivalent to setting a car in drive. That doesn’t necessarily make the car move, it just sets it.

Each joystick axis returns a value between -100 to +100, and returns a value of zero when centered. This then means that the joystick axes, when pushed, equate to -100% to 100%. The farther towards 100 or -100 the axes are pushed, the faster the motor will spin.

The [Spin] block is then used to actually move the motor. This is the equivalent to pressing the gas on the car once the direction has been set. This allows each motor to be controlled by one of the four Controller axes.

Screenshot of a VEX IQ Blocks tutorial interface, showcasing programming blocks and options for building educational robotics projects, designed for beginners and educators in the VEX IQ Robotics platform.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

The [If then else if then else] block is used to map certain behaviors to buttons being pressed or released on the Controller. In this section of code, the conditions set are if the E Up or E Down buttons are pressed. If so, certain behaviors will happen, such as the arm raising and lowering. There is also the else portion of the condition, if neither button is pressed, the arm is set to stop moving.

Note the following section of code in the project for the Claw follows the same explanation.

Summary of Option 2: Using a Forever Loop

Advantages

Limitations

Example Situation

  • Can accommodate custom builds, especially drivetrains with more than 4 motors
  • Can assign multiple behaviors to a single button
  • Can assign behaviors to different axes on the Controller (as opposed to the only options of Left Arcade, Right Arcade, Split Arcade, and Tank Drive in the Device Configuration)
  • Requires some amount of coding knowledge (conditionals, loops, and knowledge of the buttons/joysticks on the Controller)
  • Potential for slower project execution, or lag in button response time. Since all commands are contained within a single Forever loop, the code execution could run slowly depending on the conditions set and the length of the code.
  • Using the Controller with a custom built robot, especially with a non-standard drivetrain
  • When wanting to assign multiple behaviors to a single button. For example, when the F Up Button is pressed, the Claw can open, drive forward, and then close around an object.

Option 3: Using Events

If you want a lot of customization to your Controller, Using Events is the best option for you. One button press can trigger multiple robot behaviors, like pressing a button to open the claw, raise the arm, and drive forward for a set distance. Trying to code multiple behaviors per button within a Forever loop would cause the project execution to slow dramatically – using Events allows you to do this more effectively.

This option uses Events to break up the project flow. This is similar to using a Forever loop, but allows for the code to be more organized, so that the button execution has a faster response time. Faster response time means you will not see a lag between pressing Controller buttons and seeing the robot behavior. This example shows the same behaviors as the previous project, but done using the Events instead of the Forever Loop.

Screenshot of a VEX IQ Blocks tutorial interface, showcasing programming blocks and options for building and controlling VEX IQ robots, aimed at educators and students learning robotics concepts.

Download the "Option 3" VEXcode IQ (2nd generation) project file >

Note: if using a 1st Gen Clawbot the arm motor will need to be reversed in the Device Configuration to work as intended in the project above.

Explanation of Option 3 Code.

Code Piece

Explanation

Screenshot of a VEXcode IQ Blocks tutorial interface, showcasing programming blocks and options for VEX IQ Robotics, designed to assist educators and students in learning robotics concepts.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

Screenshot of VEXcode IQ Blocks interface, showcasing programming blocks for VEX IQ robotics, used in educational tutorials for building and programming robots. Diagram illustrating the VEX IQ Blocks programming interface, showcasing various programming blocks and their functions for educational robotics projects.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

{When controller axis} Event blocks are used to trigger certain behaviors when one of the four axes on the Controller are changed by using the joysticks.

The [Set motor velocity] blocks are used to set the motor velocity to the current Controller’s position along the A and D axes. This is the equivalent to setting a car in drive. That doesn’t necessarily make the car move, it just sets it.

Each joystick axis returns a value between -100 to +100, and returns a value of zero when centered. This then means that the joystick axes, when pushed, equate to -100% to 100%. The farther towards 100 or -100 the axes are pushed, the faster the motor will spin.

The [Spin] block is then used to actually move the motor. This is the equivalent to pressing the gas on the car once the direction has been set. This allows each motor to be controlled by one of the four Controller axes.

Screenshot of a VEX IQ Blocks tutorial, showcasing programming blocks and instructions for building and programming robots, aimed at educators and students in the VEX IQ robotics platform.

A Clawbot was used for this code example. When the buttons on the Controller are used to raise and lower the arm, as soon as the button is released, due to gravity the arm will fall back down. Setting both the arm and claw to “hold” will ensure that both the arm and claw will remain in place even after the buttons on the Controller have been released.

{When controller axis} Event blocks are used to map certain behaviors to buttons being pressed or released on the Controller. In this section of code, the conditions set are if the E Up or E Down buttons are pressed. If so, certain behaviors will happen, such as the arm raising, lowering, or stopping.

Note the last section of code in the project for the Claw follows the same explanation.

Summary of Option 3: Using Events

Advantages

Limitations

Example Situation

  • Can accommodate custom builds, especially drivetrains with more than 4 motors
  • Can assign multiple behaviors to a single button
  • Can assign behaviors to different axes on the Controller (as opposed to the only options of Left Arcade, Right Arcade, Split Arcade, and Tank Drive in the Device Configuration)
  • Faster code execution and thus, button responsiveness (as each condition is called on separately and not embedded in one single stack of code)
  • Requires the most coding knowledge of the options (conditionals, loops, events, and knowledge of the buttons/joysticks on the Controller)
  • If using a 1st generation VEX IQ Brain, having too many Events in a project may result in the project not running, due to the processing limitations of the Brain.
  • Using the Controller with a custom built robot, especially with a non-standard drivetrain
  • When wanting to assign multiple behaviors to a single button with no lag. For example, when the F Up Button is pressed, the Claw can open, drive forward, and then close around an object.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: