Libraries.Robots.Lego.ColorSensor Documentation

The ColorSensor class is an object representation of the LEGO Mindstorms EV3 Color Sensor.

Example Code

use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility


class Main
    action Main
        ColorSensor colorSensor
        Motor motor
        Utility utility
        
        colorSensor:SetPort(3)                      //tells the robot that the sensor is plugged into port 2
        motor:SetSpeed("B", 540)                    //two motors to move the robot
        motor:SetSpeed("C", 540)
        motor:RotateForward("B")
        motor:RotateForward("C")
        repeat until colorSensor:GetColor() = "red"
            utility:DelayMilliseconds(20)           //interval between color checks
        end
    end
e

Inherits from: Libraries.Language.Object

Summary

Variable Summary Table

VariablesDescription
integer NONE
integer RED
integer WHITEpublic constant integer ORANGE =
integer GREEN
integer YELLOWpublic constant integer ORANGE =
integer PORT_1public constant integer CYAN =
integer PORT_3
integer BLUE
integer BROWNpublic constant integer CYAN =
integer PORT_2
integer BLACKpublic constant integer PINK =
integer PORT_4This action lets the program know which port on the robot that the color sensor being used is plugged into. As such, this action must be called before any other action in the ColorSensor class will work. After calling this action, the color sensor's red light will turn on once it has fully initiated.
integer GRAYpublic constant integer PINK =

Actions Summary Table

ActionsDescription
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns an integer.
Equals(Libraries.Language.Object object)This action determines if two objects are equal based on their hash code values.
GetColor()This action uses the color sensor to read the color in front of it and send back the color as a text variable.
GetHashCode()This action gets the hash code for an object.
GetLightColor()This action gets the color of light being emitted from the color sensor.
GetLightLevel()This action uses the color sensor to measure the amount of incoming light.
GetRedGreenBlueLevels()This action will provide information on the red, green, and blue levels of a measured object.
GetReflectionLevel()This action uses the color sensor to measure the reflection of an object.
IsLightOn()This action is used to determine if the color sensor's light is currently on or off.
SetLightColor(integer color)This action is used to change the color of light projecting from the color sensor.
SetLightOn(boolean value)This action turns the flood light on or off entirely.
SetPort(integer portNumber)This action lets the program know which port on the robot that the color sensor being used is plugged into.

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.

Example Code

Object o
        Object t
        integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smalle

Parameters

Return

integer: 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(

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

GetColor()

This action uses the color sensor to read the color in front of it and send back the color as a text variable. The color sensor is capable of reading 7 different colors: red, green, blue, yellow, brown, white, and black. If the color sensor is too far away from the color it is trying to read, it will return the text "none". During the measurement, the color sensor's white light will be emitted to help determine an object's color.

Example Code

use Libraries.Robots.Lego.ColorSensor

    ColorSensor colorSensor

    colorSensor:SetPort(3)
    text color = ""
    repeat while true
        color = colorSensor:GetColor()
        if color = "none"
            output "No color found."
        else
            output "Color: " + color + "."
        end
    e

Return

text: the color that was read by the sensor in all lower-case letters. Possible colors that can be returned are: red, green, blue, yellow, brown, white, or black. If the sensor cannot read a color, it will return the text "none".

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.

GetLightColor()

This action gets the color of light being emitted from the color sensor.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Sound

    ColorSensor colorSensor
    Sound sound
    
    colorSensor:SetPort(3)
    text lightColor = colorSensor:GetLightColor()
    if lightColor = "red"
        sound:Beep()
    elseif lightColor = "blue"
        sound:BeepTwice()
    elseif lightColor = "white"
        sound:BeepSequenceUp()
    else
        sound:Buzz()    //the light is currently off
    e

Return

text: the color currently being emitted by the color sensor in all lower-case letters. Possible return values are: "red", "white", "blue", or "none", where a value of "none" indicates that the light is currently off.

GetLightLevel()

This action uses the color sensor to measure the amount of incoming light.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Motor

    ColorSensor colorSensor
    Motor motor
    
    colorSensor:SetPort(3)
    motor:RotateForward("B")
    motor:RotateForward("C")      //two motors used to move the robot forward
    repeat until colorSensor:GetLightLevel() > 0.15
       output "Light level: " + colorSensor:GetLightLevel()
    e

Return

number: a number between 0.0 and 1.0 that represents the intensity of light measured by the color sensor. The values range from complete darkness (0.0) to direct sunlight (1.0).

GetRedGreenBlueLevels()

This action will provide information on the red, green, and blue levels of a measured object.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Screen
    use Libraries.Robots.Lego.Button
    use Libraries.Containers.Array

    ColorSensor colorSensor
    Screen screen
    Button button
    
    colorSensor:SetPort(3)
    Array<number> redGreenBlueLevels = colorSensor:GetRedGreenBlueLevels()
    screen:Output("Red: " + redGreenBlueLevels:Get(0), 0)
    screen:Output("Green: " + redGreenBlueLevels:Get(1), 1)
    screen:Output("Blue: " + redGreenBlueLevels:Get(2), 2)
    button:WaitForButtonPress

Return

Libraries.Containers.Array: a number array with three values representing the intensity of red, green, and blue measured, respectively. Values will range between the color not being read at all (0.0) and the color being fully present in the reading (1.0).

GetReflectionLevel()

This action uses the color sensor to measure the reflection of an object. It accomplishes this by projecting a red light and then measuring the intensity of that light reflected back.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Screen
    use Libraries.Robots.Lego.Utility

    ColorSensor colorSensor
    Motor motor
    Screen screen
    Utility utility
    
    colorSensor:SetPort(colorSensor:PORT_3)
    motor:RotateForward("A")  //two motors used to move the robot
    motor:RotateForward("B")
    repeat 8 times
        screen:ScrollUp("Reflectivity: " + colorSensor:GetReflectionLevel())  //displays reflectivity of object
        utility:DelayMilliseconds(500)
    e

Return

number: a number between 0.0 and 1.0 that represents the intensity of light being reflecting. The values range from no reflection (0.0) to complete reflection (1.0). If an object is too far away, no reflection will be picked up by the sensor, resulting in a value of 0.0.

IsLightOn()

This action is used to determine if the color sensor's light is currently on or off.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Sound

    ColorSensor colorSensor
    Sound sound
    
    colorSensor:SetPort(3)
    if colorSensor:IsLightOn()
        colorSensor:SetLightColor(colorSensor:BLUE)
    else
        sound:Buzz()
    e

Return

boolean: a boolean value with a value of true if the color sensor's light is on or false if the color sensor's light is off.

SetLightColor(integer color)

This action is used to change the color of light projecting from the color sensor. By default, the light is off. The color sensor can emit red, white, blue, or no light.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Button
    use Libraries.Robots.Lego.Utility

    ColorSensor colorSensor
    Button button
    Utility utility
    
    colorSensor:SetPort(3)
    repeat until button:IsButtonPressed(button:ESCAPE_BUTTON)
        colorSensor:SetLightColor(colorSensor:BLUE)
        utility:DelayMilliseconds(500)
        colorSensor:SetLightColor(colorSensor:RED)
        utility:DelayMilliseconds(500)
    e

Parameters

SetLightOn(boolean value)

This action turns the flood light on or off entirely.

Example Code

use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Sound

    ColorSensor colorSensor
    Sound sound
    
    colorSensor:SetPort(3)
    colorSensor:SetLightOn(fals

Parameters

SetPort(integer portNumber)

This action lets the program know which port on the robot that the color sensor being used is plugged into. As such, this action must be called before any other action in the ColorSensor class will work. After calling this action, the color sensor's red light will turn on once it has fully initiated.

Example Code

use Libraries.Robots.Lego.ColorSensor

    ColorSensor colorSensor1
    ColorSensor colorSensor2
    ColorSensor colorSensor3
    
    colorSensor1:SetPort(1)                     //we can have multiple color sensors plugged into the robot at once
    colorSensor2:SetPort(2)                     //after setting the ports, we can now use any other action in the ColorSensor class
    colorSensor3:SetPort(colorSensor3:PORT_3)   //we can use a class constant to designate a port, as we

Parameters