Libraries.Robots.Lego.Button Documentation

The Button class is an object representation of the LEGO Mindstorms EV3 button section located below the screen on the brick. It includes functionality for the buttons as well as the LED that surrounds them. The buttons and LED patterns available are represented as class constants.

Example Code

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Motor

class Main
action Main
    Button button
    Motor motor
    
    motor:SetSpeed("A", 720)    //two motors to move the robot
    motor:SetSpeed("B", 720)
    repeat while true
        button:WaitForButtonPress()
        if button:IsButtonPressed(button:UP_BUTTON)
            motor:RotateForward("A")
            motor:RotateForward("B")
            button:SetLightPattern(button:SOLID_GREEN_LIGHT)
        elseif button:IsButtonPressed(button:DOWN_BUTTON)
            motor:RotateBackward("A")
            motor:RotateBackward("B")
            button:SetLightPattern(button:FLASHING_RED_LIGHT)
        else
            motor:Stop("A")
            motor:Stop("B")
            button:SetLightPattern(button:NO_LIGHT)
        end
    end
end
end

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer PULSING_RED_LIGHT
integer SOLID_GREEN_LIGHT
integer PULSING_ORANGE_LIGHTThis action is used to find out if a certain button is currently being pressed.
text DOWN_BUTTON
integer SOLID_ORANGE_LIGHT
text CENTER_BUTTON
integer SOLID_RED_LIGHT
integer NO_LIGHT
integer FLASHING_ORANGE_LIGHT
text UP_BUTTON
text ESCAPE_BUTTON
text LEFT_BUTTON
integer FLASHING_GREEN_LIGHT
integer PULSING_GREEN_LIGHT
integer FLASHING_RED_LIGHT
text RIGHT_BUTTON

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

Example

use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)

GetHashCode()

This action gets the hash code for an object.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

IsButtonPressed(text button)

This action is used to find out if a certain button is currently being pressed.

Parameters

  • text button: is used to specify the button to be checked. The Button class has text constants that can be used for this parameter.

Return

boolean: a boolean value denoting whether the button specified in the parameter is being pressed in or not.

Example

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Sound

Button button
Sound sound

repeat until button:IsButtonPressed(button:ESCAPE_BUTTON)
    if button:IsButtonPressed(button:LEFT_BUTTON)
        sound:PlayTone(1000, 20)
    elseif button:IsButtonPressed(button:RIGHT_BUTTON)
        sound:PlayTone(2000, 20)
    end
end

SetLightPattern(integer pattern)

This action displays a light pattern on the LED surrounding the EV3's buttons.

Parameters

  • integer pattern: specifies the light pattern to be displayed on the EV3's brick. The Button class has integer constants that can be used for this parameter.

Example

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Utility

Button button
Utility utility

button:SetLightPattern(button:SOLID_GREEN_LIGHT)
utility:DelayMilliseconds(2000)
button:SetLightPattern(button:FLASHING_RED_LIGHT)
utility:DelayMilliseconds(2000)
button:SetLightPattern(button:PULSING_ORANGE_LIGHT)
utility:DelayMilliseconds(2000)

WaitForButtonPress()

This action halts the current program execution until a button is pressed on the EV3 brick.

Example

use Libraries.Robots.Lego.Button
use Libraries.Robots.Lego.Motor

Button button
Motor motor

motor:SetSpeed("B", 1080)
motor:RotateForward("B")
button:WaitForButtonPress()
motor:Stop("B")