Button#

Introduction#

The button class is derived from the brain base class and provides access to the IQ (2nd gen) Brain’s buttons, allowing your robot to monitor presses to the brain’s buttons.

Access#

The brain class provides twelve button objects. Each object is an instance of the button class and can be accessed through a brain instance.

Object

Example Usage

Description

buttonCheck

Brain.buttonCheck.pressing()

Check button

buttonLeft

Brain.buttonLeft.pressing()

Left button

buttonRight

Brain.buttonRight.pressing()

Right button

Notes#

  • The button object is provided by the brain. It is not constructed directly.

Member Functions#

The Button class includes the following member functions:

  • pressed — Registers a function to be called when the specified button is pressed.

  • released — Registers a function to be called when the specified button is released.

  • pressing — Returns whether or not the specified button is being pressed.

Before calling any button member functions, a brain instance must be created, as shown below:

// Create a brain instance
brain();

pressed#

Registers a callback function that runs when a specific brain button is pressed.

Available Functions
void pressed( 
  void (* callback)(void) ) const;

Parameters

Parameter

Type

Description

callback

void (*)(void)

A function that is called when the specified button is pressed.

Return Values

This function does not return a value.

Examples

This function can be called on any brain button object, for example:

  • Brain.buttonCheck
  • Brain.buttonLeft
  • Brain.buttonRight

Define the callback function (outside of int main())

// Drive forward when check is pressed
void driveForward() {
    Drivetrain.driveFor(forward, 100, mm);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call driveForward when the check button is pressed
  Brain.buttonCheck.pressed(driveForward);
}

released#

Registers a callback function that runs when a specific brain button is released.

Available Functions
void released( 
  void (* callback)(void) ) const;

Parameters#

Parameter

Type

Description

callback

void (*)(void)

A function that is called when the specified button is released.

Return Values

This function does not return a value.

Examples

This function can be called on any brain button object, for example:

  • Brain.buttonCheck
  • Brain.buttonLeft
  • Brain.buttonRight

Define the callback function (outside of int main())

// Drive backward when the left button is released
void backUp() {
    Drivetrain.driveFor(reverse, 100, mm);
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call backUp when the left button is released
  Brain.buttonLeft.released(backUp);
}

pressing#

Returns whether a specific brain button is currently being pressed.

Available Functions
bool pressing() const;

Parameters

This function does not accept any parameters.

Return Values

Returns an integer indicating whether the specified button is being pressed:

  • 1 — The button is being pressed.
  • 0 — The button is not being pressed.
Examples

This function can be called on any brain button object, for example:

  • Brain.buttonCheck
  • Brain.buttonLeft
  • Brain.buttonRight
// Turn right while right is pressed
while (true) {
    if (Brain.buttonRight.pressing()) {
        Drivetrain.turn(right);
    } else {
        Drivetrain.stop();
    }
    wait(5, msec);
}