Bumper Switch#

Introduction#

The bumper class is used to detect when the Bumper Switch is pressed and released.

Class Constructor#

bumper( 
  int32_t index );

Class Destructor#

Destroys the bumper object and releases associated resources.

virtual ~bumper();

Parameters#

Parameter

Type

Description

index

int32_t

The Smart Port that the Bumper Switch is connected to, written as PORTx, where x is the port number (for example, PORT1).

Example#

// Create a bumper instance in Port 1
bumper Bumper1 = bumper(PORT1);

Member Functions#

The bumper class includes the following member functions:

  • pressing — Returns if the Bumper Switch is being pressed.

  • pressed — Registers a function to be called when the Bumper Switch is pressed.

  • released — Registers a function to be called when the Bumper Switch is released.

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

/* This constructor is required when using VS Code.
Bumper Switch configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a bumper instance in Port 1
bumper Bumper1 = bumper(PORT1);

pressing#

Returns if the Bumper Switch is currently being pressed.

Available Functions
int32_t pressing();

Parameters

This function does not accept any parameters.

Return Values

Returns an int32_t indicating whether the Bumper Switch is being pressed.

  • 1 — The Bumper Switch is being pressed.

  • 0 — The Bumper Switch is not being pressed.

pressed#

Registers a function to be called when the Bumper Switch is pressed.

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

Parameters

Parameter

Type

Description

callback

void (*)(void)

A pointer to a function that will be called when the bumper is pressed. The function must take no parameters and return void.

Return Values

This function does not return a value.

Examples

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

// Display a message when bumper is pressed
void BumperPressed() {
  Brain.Screen.print("Bumper pressed");
}

Register the callback inside int main()

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

  // Run BumperPressed when the value of the Bumper Switch is pressed.
  Bumper1.pressed(BumperPressed);
}

released#

Registers a function to be called when the Bumper Switch is released.

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

Parameters

Parameter

Type

Description

callback

void (*)(void)

A pointer to a function that will be called when the bumper is released. The function must take no parameters and return void.

Return Values

This function does not return a value.

Examples

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

// Display a message when bumper is released
void BumperReleased() {
  Brain.Screen.print("Bumper released");
}

Register the callback inside int main()

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

  // Run BumperReleased when the value of the Bumper Switch is released.
  Bumper1.released(BumperReleased);
}