Bumper#

Initializing the Bumper Class#

A Bumper Sensor is created by using the following constructor:

Bumper(port)

This constructor uses one parameter:

Parameter

Description

port

The 3-Wire Port that the Bumper Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the Bumper Class constructor.

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

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)

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.