Controller#

Introduction#

The VEX IQ (2nd gen) Controller has buttons and two joysticks. Controller methods can be used to check button presses, read joystick movement, enable or disable configured controller actions, or run functions when controller events happen.

The IQ (2nd gen) Brain can connect to either an IQ (2nd gen) or IQ (1st gen) Controller. An IQ (1st gen) Controller must have a blue Smart Radio installed.

Configured controller actions are controller behaviors set in the Devices menu. Use remote_control_code_enabled to temporarily enable or disable those configured actions during a project.

This page uses controller as the example controller name. Replace it with your own configured name as needed.

Below is a list of all available methods:

Action — Turn configured controller actions on or off.

Getters — Read button states and joystick positions.

  • .pressing — Returns whether a specified button is being pressed.

  • .position — Returns the position of a specified joystick axis.

Callbacks — Run functions when controller input changes.

  • .pressed — Runs a function when a specified button is pressed.

  • .released — Runs a function when a specified button is released.

  • .changed — Runs a function when the joystick’s position changes along a specified axis.

Constructor — Manually create a Controller object.

Action#

remote_control_code_enabled#

remote_control_code_enabled is a variable that enables or disables controller actions configured in the Devices menu. Controller configured actions are enabled by default.

Usage:
remote_control_code_enabled = state

Value

Description

True

Enables controller configured actions.

False

Disables controller configured actions.

# Use custom controller code, then re-enable configured actions
remote_control_code_enabled = False

while True:
    if controller.axisA.position() > 0:
        drivetrain.drive(FORWARD)
    elif controller.axisA.position() < 0:
        drivetrain.drive(REVERSE)
    elif controller.buttonEUp.pressing():
        break
    else:
        drivetrain.stop()

    wait(20, MSEC)

remote_control_code_enabled = True

Getters#

.pressing#

.pressing returns whether a specific button on the controller is currently being pressed. This method must be called on a specific button object, such as controller.buttonEUp.

  • True — The specified button is being pressed.

  • False — The specified button is not being pressed.

Usage:
One of the available button objects can be used with this method:

A front and back side view of the IQ 2nd gen Controller with all buttons highlighted yellow. The surface of the controller contains two joystick buttons in the upper left and right corners, with E up and down below the left joystick and F up and down below the right. On the back of the controller are L up and down and R up and down on the left and right sides respectively.

Button

Command

buttonEDown

controller.buttonEDown.pressing() — The E ▼ button

buttonEUp

controller.buttonEUp.pressing() — The E ▲ button

buttonFDown

controller.buttonFDown.pressing() — The F ▼ button

buttonFUp

controller.buttonFUp.pressing() — The F ▲ button

buttonL3

controller.buttonL3.pressing() — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.pressing() — The L ▼ button

buttonLUp

controller.buttonLUp.pressing() — The L ▲ button

buttonR3

controller.buttonR3.pressing() — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.pressing() — The R ▼ button

buttonRUp

controller.buttonRUp.pressing() — The R ▲ button

Parameters

Description

This method has no parameters.

# Turn right while E Up is held
while True:
    if controller.buttonEUp.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

.position#

.position returns the position of a joystick axis as a number from -100 to 100.

This method must be called on a specific axis object, such as controller.axisA.

Usage:
One of the available axis objects can be used with this method:

The IQ 2nd gen Controller with the four joystick axes highlighted. On the left joystick, Axis A is vertical and Axis B is horizontal. On the right joystick, Axis C is horizontal and Axis D is vertical.

Axis

Command

axisA

controller.axisA.position() — The left joystick’s vertical axis

axisB

controller.axisB.position() — The left joystick’s horizontal axis

axisC

controller.axisC.position() — The right joystick’s horizontal axis

axisD

controller.axisD.position() — The right joystick’s vertical axis

Parameters

Description

This method has no parameters.

# Turn with the left joystick
remote_control_code_enabled = False

while True:
    if controller.axisB.position() > 10:
        drivetrain.turn(RIGHT)
    elif controller.axisB.position() < -10:
        drivetrain.turn(LEFT)
    else:
        drivetrain.stop()

    wait(20, MSEC)

Callbacks#

.pressed#

.pressed runs a function when the specified button is pressed. Once it is used, the function will run automatically each time that button is pressed.

Usage:
One of the available button objects can be used with this method:

A front and back side view of the IQ 2nd gen Controller with all buttons highlighted yellow. The surface of the controller contains two joystick buttons in the upper left and right corners, with E up and down below the left joystick and F up and down below the right. On the back of the controller are L up and down and R up and down on the left and right sides respectively.

Button

Command

buttonEDown

controller.buttonEDown.pressed(callback, arg) — The E ▼ button

buttonEUp

controller.buttonEUp.pressed(callback, arg) — The E ▲ button

buttonFDown

controller.buttonFDown.pressed(callback, arg) — The F ▼ button

buttonFUp

controller.buttonFUp.pressed(callback, arg) — The F ▲ button

buttonL3

controller.buttonL3.pressed(callback, arg) — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.pressed(callback, arg) — The L ▼ button

buttonLUp

controller.buttonLUp.pressed(callback, arg) — The L ▲ button

buttonR3

controller.buttonR3.pressed(callback, arg) — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.pressed(callback, arg) — The R ▼ button

buttonRUp

controller.buttonRUp.pressed(callback, arg) — The R ▲ button

Parameter

Description

callback

A previously defined function to run each time the specified button is pressed.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# Drive forward when E Up is pressed
def drive_forward():
    drivetrain.drive_for(FORWARD, 100, MM)

controller.buttonEUp.pressed(drive_forward)

.released#

.released runs a function when the specified button is released. Once it is used, the function will run automatically each time that button is released.

Usage:
One of the available button objects can be used with this method:

A front and back side view of the IQ 2nd gen Controller with all buttons highlighted yellow. The surface of the controller contains two joystick buttons in the upper left and right corners, with E up and down below the left joystick and F up and down below the right. On the back of the controller are L up and down and R up and down on the left and right sides respectively.

Button

Command

buttonEDown

controller.buttonEDown.released(callback, arg) — The E ▼ button

buttonEUp

controller.buttonEUp.released(callback, arg) — The E ▲ button

buttonFDown

controller.buttonFDown.released(callback, arg) — The F ▼ button

buttonFUp

controller.buttonFUp.released(callback, arg) — The F ▲ button

buttonL3

controller.buttonL3.released(callback, arg) — The Left Joystick button, IQ (2nd gen) Controller only

buttonLDown

controller.buttonLDown.released(callback, arg) — The L ▼ button

buttonLUp

controller.buttonLUp.released(callback, arg) — The L ▲ button

buttonR3

controller.buttonR3.released(callback, arg) — The Right Joystick button, IQ (2nd gen) Controller only

buttonRDown

controller.buttonRDown.released(callback, arg) — The R ▼ button

buttonRUp

controller.buttonRUp.released(callback, arg) — The R ▲ button

Parameter

Description

callback

A previously defined function to run each time the specified button is released.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# Stop driving when E Up is released
def stop_driving():
    drivetrain.stop()

controller.buttonEUp.released(stop_driving)

.changed#

.changed runs a function when the joystick’s position changes along the specified axis. Once it is used, the function will run automatically each time the joystick’s position changes along that axis.

Usage:
One of the available axis objects can be used with this method:

The IQ 2nd gen Controller with the four joystick axes highlighted. On the left joystick, Axis A is vertical and Axis B is horizontal. On the right joystick, Axis C is horizontal and Axis D is vertical.

Axis

Command

axisA

controller.axisA.changed(callback, arg) — The left joystick’s vertical axis

axisB

controller.axisB.changed(callback, arg) — The left joystick’s horizontal axis

axisC

controller.axisC.changed(callback, arg) — The right joystick’s horizontal axis

axisD

controller.axisD.changed(callback, arg) — The right joystick’s vertical axis

Parameter

Description

callback

A previously defined function to run each time the joystick’s position changes along the specified axis.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# Play a sound when the right joystick moves
def beep():
    brain.play_sound(SoundType.TADA)

controller.axisD.changed(beep)

Constructors#

Controller#

Controller creates a Controller object. Manually creating a Controller object is only needed when configuring a controller outside of VEXcode.

Usage:
Controller()

Parameters

Description

This constructor has no parameters.

# Create a Controller object
my_controller = Controller()