Libraries.Robots.Lego.TouchSensor Documentation

This class is an object representation of the LEGO EV3 Mindstorms Touch Sensor. It is used to detect when the sensor is pressed in. Generally, this button is pressed in by the user to tell the robot to perform some action.

Example Code

use Libraries.Robots.Lego.TouchSensor
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility

class Main
    action Main
        TouchSensor touchSensor
        Sound sound
        Motor motor
        Utility utility

        touchSensor:SetPort(touchSensor:PORT_2) //touch sensor connected to port 2 on the robot
        motor:SetSpeed(motor:MOTOR_B, 480)
        motor:SetSpeed(motor:MOTOR_C, 480)      //two motors used to move the robot, connected to ports B and C
        repeat 5 times
            if touchSensor:IsPressed()
                motor:RotateForward(motor:MOTOR_B)
                motor:RotateForward(motor:MOTOR_C)    //robot will move forward if button was pressed
            else
                sound:Beep()    //robot will beep if the button was not pressed in
            end
            utility:DelayMilliseconds(1000)  //check if the button is pressed in every second
            motor:Stop(motor:MOTOR_B)
            motor:Stop(motor:MOTOR_C)
        end
    end
end

Inherits from: Libraries.Language.Object

Summary

Variable Summary Table

VariablesDescription
integer PORT_2
integer PORT_4
integer PORT_3
integer PORT_1

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.
IsPressed()This action checks if the touch sensor is currently pressed in or not.
SetPort(integer portNumber)This action lets the program know which port on the robot that the touch sensor being used is plugged into.

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.

IsPressed()

This action checks if the touch sensor is currently pressed in or not.

Example Code

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

    TouchSensor touchSensor
    Button button
    Utility utility

    touchSensor:SetPort(touchSensor:PORT_1)         //touch sensor connected to port 1 of the robot
    integer lightColor = 0
    repeat 20 times
        if touchSensor:IsPressed()
            lightColor = lightColor + 1             //update the light color
            button:SetLightPattern(lightColor mod 3 + 1)    //sets the pattern to either 1, 2, or 3 which corresponds to a solid green, red, or orange light, respectively
        else
            button:SetLightPattern(button:NO_LIGHT)         //turn off the light when the button is not pressed in
        end
        utility:DelayMilliseconds(500)              //check if the button is pressed in every 100 milliseconds
    end

Return

boolean: a boolean value indicating whether the button is pressed in (true) or not pressed in (false).

SetPort(integer portNumber)

This action lets the program know which port on the robot that the touch sensor being used is plugged into. As such, this action must be called before any other action in the TouchSensor class will work.

Example Code

use Libraries.Robots.Lego.TouchSensor
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Utility


    TouchSensor touchSensor
    Motor motor
    Utility utility

    touchSensor:SetPort(touchSensor:PORT_3) //touch sensor connected to port 3 of the robot
    motor:SetSpeed(motor:MOTOR_B, 720)
    motor:SetSpeed(motor:MOTOR_C, 720)      //two motors used to move the robot, connected to ports B and C
    repeat 10 times
        if touchSensor:IsPressed()
           motor:RotateForward(motor:MOTOR_B)
           motor:RotateBackward(motor:MOTOR_C)    //spin in a certain direction
        else
           motor:RotateBackward(motor:MOTOR_B)
           motor:RotateForward(motor:MOTOR_C)     //spin in the opposite direction
        end
        utility:DelayMilliseconds(1000)  //check if the button is pressed in every second
    end

Parameters