Bumper#

Initializing the Bumper Class#

The Bumper constructor creates a Bumper object in the specified Smart Port:

Parameter

Description

port

A valid Smart Port that the Bumper Sensor is connected to.

# Create the Brain.
brain = Brain()
# Construct a Bumper Sensor "bumper" with the
# Bumper class.
bumper = Bumper(Ports.PORT1)

This bumper object will be used in all subsequent examples throughout this API documentation when referring to Bumper class methods.

Class Methods#

pressed()#

The pressed(callback, arg) method registers a function to be called when the Bumper Sensor is pressed.

Parameters

Description

callback

A function that will be called when the Bumper Sensor is pressed.

arg

Optional. A tuple that is used to pass arguments to the callback function.

Returns: An instance of the Event class.

# Define a function bumper_pressed().
def bumper_pressed():
    # The Brain will print that the Bumper Sensor was pressed
    # on the Brain's screen.
    brain.screen.print("Bumper Sensor pressed")
# Run bumper_pressed when the Bumper Sensor is pressed.
bumper.pressed(bumper_pressed)

bumper.released()#

The released(callback, arg) method registers a function to be called when the Bumper Sensor is released.

Parameters

Description

callback

A function that will be called when the Bumper Sensor is released.

arg

Optional. A tuple that is used to pass arguments to the callback function.

Returns: An instance of the Event class.

# Define a function bumper_released.
def bumper_released():
    # The Brain will print that the Bumper Sensor was released
    # on the Brain's screen.
    brain.screen.print("Bumper Sensor released")
# Run bumper_released when the Bumper Sensor is released.
bumper.released(bumper_released)

pressing()#

The pressing() method checks if the Bumper Sensor is currently being pressed.

Returns: True if the Bumper Sensor is currently being pressed. False if it is not being pressed.