Libraries.Robots.Lego.UltrasonicSensor Documentation

This class is an object representation of the LEGO EV3 Mindstorms Ultrasonic Sensor. It is used to detect how far away objects are, in meters.

Example Code

class Main
action Main
    use Libraries.Robots.Lego.Button
    use Libraries.Robots.Lego.Screen
    use Libraries.Robots.Lego.UltrasonicSensor

    UltrasonicSensor ultrasonicSensor
    Button buttonController
    Screen screenController

    ultrasonicSensor:SetPort(1)

    repeat until buttonController:IsButtonPressed(buttonController:ESCAPE_BUTTON)
        screenController:ScrollUp(ultrasonicSensor:GetDistance()+"")
    end
end
end

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer PORT_3
integer PORT_1
integer PORT_4This action lets the program know which port on the robot that the ultrasonic sensor being used is plugged into. As such, this action must be called before any other action in the UltrasonicSensor class will work.
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)

GetDistance()

This action measures the distance between the ultrasonic sensor and an object in front of it.

Return

number: the how far an object is away from the ultrasonic sensor, measured in meters. The sensor is effective up to around 1.6 meters, otherwise it will return infinity if an object is too far away.

Example

use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.UltrasonicSensor
use Libraries.Robots.Lego.Utility

Sound soundController
UltrasonicSensor ultrasonicSensor
Utility utility
number distance = 0

ultrasonicSensor:SetPort(1)
repeat 10 times
    distance = ultrasonicSensor:GetDistance()
    if distance < 0.2
        soundController:PlayTone(100, 200)
    elseif distance < 0.4
        soundController:PlayTone(200, 200)
    elseif distance < 0.4
        soundController:PlayTone(400, 200)
    elseif distance < 0.6
        soundController:PlayTone(800, 200)
    elseif distance < 0.8
        soundController:PlayTone(1600, 200)
    elseif distance < 0.4
        soundController:PlayTone(3200, 200)
    else
        soundController:Buzz()
    end
    utility:DelayMilliseconds(500)
end

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

SetPort(integer portNumber)

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

Parameters

  • integer portNumber: specifies the port on the robot that the ultrasonic 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.Motor
use Libraries.Robots.Lego.UltrasonicSensor
use Libraries.Robots.Lego.Utility

UltrasonicSensor ultrasonicSensor
Motor motorController
Utility utility

ultrasonicSensor:SetPort(1)

motor:RotateForward("B")
repeat 5 times
    motor:RotateForward("C")
    repeat while ultrasonicSensor:GetDistance() > 1.0
        utility:DelayMilliseconds(20)   //repeats the check every 20 milliseconds
    end
    motor:Stop("C")
    utility:DelayMilliseconds(1000)     //allows the B motor to rotate alone for 1 second, causing the robot to turn
end