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

Summary

Variable Summary Table

VariablesDescription
integer PORT_4
integer PORT_3
integer PORT_2
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.
GetDistance()This action measures the distance between the ultrasonic sensor and an object in front of it.
GetHashCode()This action gets the hash code for an object.
SetPort(integer portNumber)This action lets the program know which port on the robot that the ultrasonic 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.

GetDistance()

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

Example Code

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

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.

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.

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.

Example Code

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

Parameters