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
Variables Table
Variables | Description |
---|---|
integer PORT_3 | |
integer BROWN | public constant integer CYAN = 12 |
integer GRAY | public constant integer PINK = 8 |
integer BLACK | public constant integer PINK = 8 |
integer BLUE | |
integer PORT_1 | public constant integer CYAN = 12 |
integer PORT_2 | |
integer WHITE | public constant integer ORANGE = 5 |
integer GREEN | |
integer NONE | |
integer RED | |
integer YELLOW | public constant integer ORANGE = 5 |
integer PORT_4 | 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. |
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
- Libraries.Language.Object: The object to compare to.
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
- Libraries.Language.Object: The to be compared.
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)
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.
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".
Example
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
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()
GetLightColor()
This action gets the color of light being emitted from the color sensor.
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.
Example
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
GetLightLevel()
This action uses the color sensor to measure the amount of incoming light.
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).
Example
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
GetRedGreenBlueLevels()
This action will provide information on the red, green, and blue levels of a measured object.
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).
Example
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()
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.
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.
Example
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)
end
IsLightOn()
This action is used to determine if the color sensor's light is currently on or off.
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.
Example
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
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.
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.
Example
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
SetLightOn(boolean value)
This action turns the flood light on or off entirely.
Parameters
- boolean value
Example
use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Sound
ColorSensor colorSensor
Sound sound
colorSensor:SetPort(3)
colorSensor:SetLightOn(false)
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.
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.
Example
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