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
text CENTER_BUTTON
integer NO_LIGHT
integer FLASHING_ORANGE_LIGHT
integer FLASHING_GREEN_LIGHT
integer FLASHING_RED_LIGHT
integer SOLID_RED_LIGHT
integer SOLID_ORANGE_LIGHT
integer SOLID_GREEN_LIGHT
text UP_BUTTON
integer PULSING_RED_LIGHT
integer PULSING_ORANGE_LIGHT
text ESCAPE_BUTTON
text RIGHT_BUTTON
text LEFT_BUTTON
text DOWN_BUTTON
integer PULSING_GREEN_LIGHT

Actions Summary Table

ActionsDescription
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns a CompareResult.
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 a CompareResult. The compare result is either larger if this hash code is larger than the object passed as a parameter, smaller, or equal.

Example Code

use Libraries.Language.Support.CompareResult
        Object o
        Object t
        CompareResult result = o:Compare(t)

Parameters

Return

Libraries.Language.Support.CompareResult: 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")