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
end
Inherits from: Libraries.Language.Object
Summary
Variable Summary Table
Variables | Description |
---|---|
integer GRAY | public constant integer PINK = |
integer BROWN | public constant integer CYAN = |
integer RED | |
integer PORT_2 | |
integer BLUE | |
integer BLACK | |
integer GREEN | |
integer PORT_4 | |
integer NONE | |
integer YELLOW | |
integer WHITE | public constant integer ORANGE = |
integer PORT_3 | |
integer PORT_1 |
Actions Summary Table
Actions | Description |
---|---|
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. |
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. |
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 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
- Libraries.Language.Object: The object to compare to.
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
- Libraries.Language.Object: The to be compared.
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
end
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
end
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()
end
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
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)
end
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 relection 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()
end
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)
end
Parameters
- integer color: is an integer code that represents the color of light to be set. The ColorSensor class has constants that can be used for this. Valid colors for the light are: red, white, blue, or none.
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 well
Parameters
- integer portNumber: specifies the port on the robot that the color 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.