Button#

Introduction#

The IQ (2nd gen) Brain features multiple buttons that can be used to trigger functions or check the status of user inputs. These buttons include the Left, Right, and Check buttons, as well as methods to track when they are pressed, released, or currently pressed.

Below is a list of all available methods:

Methods – Interact with buttons based on input.

  • .pressed – Calls a function when the specified button is pressed.

  • .released – Calls a function when the specified button is released.

  • .pressing – Returns whether the specified button is being pressed.

.pressed#

.pressed registers a function to be called when a specific button on the Brain is pressed. This method must be called on a specific button object, such as buttonCheck – (see full list of button objects below).

Usage:
One of three available button objects can be used with this method, as shown below:

Button

Command

buttonCheck

brain.buttonCheck.pressed(callback, arg) — The Check button

buttonLeft

brain.buttonLeft.pressed(callback, arg) — The Left button

buttonRight

brain.buttonRight.pressed(callback, arg) — The Right button

Parameters

Description

callback

A function that is previously defined to execute when the specified button is being pressed.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# Turn in a circle when the left button is pressed
def button_pressed():
    drivetrain.turn_for(RIGHT, 360, DEGREES)

brain.buttonLeft.pressed(button_pressed)

.released#

.released registers a function to be called when a specific button on the Brain is released. This method must be called on a specific button object, such as buttonCheck – (see full list of button objects below).

Usage:
One of three available button objects can be used with this method, as shown below:

Button

Command

buttonCheck

brain.buttonCheck.released(callback, arg) — The Check button

buttonLeft

brain.buttonLeft.released(callback, arg) — The Left button

buttonRight

brain.buttonRight.released(callback, arg) — The Right button

Parameters

Description

callback

A function that is previously defined to execute when the specified button is released.

arg

Optional. A tuple containing arguments to pass to the callback function. See Using Events with Parameters for more information.

# Turn in a circle when the left button is released
def button_released():
    drivetrain.turn_for(LEFT, 360, DEGREES)

brain.buttonLeft.released(button_released)

pressing()#

.pressing returns a Boolean indicating whether a specific button on the Brain is currently being pressed. This method must be called on a specific button object, such as buttonCheck (see full list of button objects below).

  • True - The specified button is being pressed.

  • False - The specified button is not being pressed.

Usage:
One of three available button objects can be used with this method, as shown below:

Button

Command

buttonCheck

brain.buttonCheck.pressing() — The Check button

buttonLeft

brain.buttonLeft.pressing() — The Left button

buttonRight

brain.buttonRight.pressing() — The Right button

# Turn when the check button is pressed
while True:
    if brain.buttonCheck.pressing():
        drivetrain.turn(RIGHT)
    else:
        drivetrain.stop()