Bumper Switch#
Introduction#
The Bumper Switch is a mechanical switch that can detect when it is pressed and released.
This page uses bumper_1 as the example Bumper Switch name. Replace it with your own configured name as needed.
Below is a list of all methods:
Getters — Check if the Bumper Switch is currently pressed.
pressing— Returns whether the Bumper Switch is being pressed.
Callback — Run code when the Bumper Switch is pressed or released.
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.
Constructors — Manually initialize and configure the Bumper Switch.
Bumper— Create a Bumper Switch.
Getters#
pressing#
pressing returns an integer indicating whether the Bumper Switch is currently being pressed.
1— The Bumper Switch is being pressed.0— The Bumper Switch is not being pressed.
Usage:
bumper_1.pressing()
Parameters |
Description |
|---|---|
This method has no parameters. |
# Back up and turn if bumper switch
# is pressed
while True:
drivetrain.drive(FORWARD)
if bumper_1.pressing():
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)
Callback#
pressed#
pressed registers a function to be called when the Bumper Switch is pressed.
Usage:
bumper_1.pressed(callback, arg)
Parameters |
Description |
|---|---|
|
A function that is previously defined to execute when the Bumper Switch is being pressed. |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
# Drive forward when bumper switch
# is pressed
def bumper_pressed():
drivetrain.drive_for(FORWARD, 100, MM)
bumper_1.pressed(bumper_pressed)
released#
released registers a function to be called when the Bumper Switch is released.
Usage:
bumper_1.released(callback, arg)
Parameters |
Description |
|---|---|
|
A function that is previously defined to execute when the Bumper Switch is released. |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
# Drive reverse when bumper switch
# is released
def bumper_released():
drivetrain.drive_for(REVERSE, 100, MM)
bumper_1.released(bumper_released)
Constructors#
Constructors are used to manually create Bumper objects, which are necessary for configuring a Bumper Switch outside of VEXcode.
Bumper Switch#
Bumper creates a Bumper Switch.
Usage:
Bumper(smartport)
Parameter |
Description |
|---|---|
|
The Smart Port that the Bumper Switch is connected to, written as PORTx where x is the number of the port. |
bumper_1 = Bumper(PORT1)
# Back up and turn if bumper switch pressed
while True:
drivetrain.drive(FORWARD)
if bumper_1.pressing():
drivetrain.drive_for(REVERSE, 100, MM)
drivetrain.turn_for(RIGHT, 90, DEGREES)