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

Summary

Variable Summary Table

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

Actions Summary Table

ActionsDescription
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns an integer.
Equals(Libraries.Language.Object object)This action determines if two objects are equal based on their hash code values.
GetHashCode()This action gets the hash code for an object.
IsButtonPressed(text button)This action is used to find out if a certain button is currently being pressed.
SetLightPattern(integer pattern)This action displays a light pattern on the LED surrounding the EV3's buttons.
WaitForButtonPress()This action halts the current program execution until a button is pressed on the EV3 brick.

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.

Example Code

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

Parameters

Return

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

Equals(Libraries.Language.Object object)

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

Example Code

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

Parameters

Return

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

GetHashCode()

This action gets the hash code for an object.

Example Code

Object o
        integer hash = o:GetHashCode()

Return

integer: The integer hash code of the object.

IsButtonPressed(text button)

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

Example Code

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

Parameters

Return

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

SetLightPattern(integer pattern)

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

Example Code

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)

Parameters

WaitForButtonPress()

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

Example Code

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")