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

Variables Table

VariablesDescription
integer PORT_3
integer PORT_4This 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.
integer PORT_1
integer PORT_2

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

IsPressed()

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

Return

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

Example

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

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.

Parameters

  • integer portNumber: specifies the port on the robot that the touch sensor is plugged into. The port number corresponds to the actual number printed above the port on the EV3 brick. Valid port numbers are 1, 2, 3 or 4. There are class variables that correspond to the valid ports.

Example

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