Color Sensor
The LEGO Color Sensor (45605) names the color in front of it, measures its red, green and blue, and measures light. It shines a light of its own, so it can read a brick held close to it.
BuildHat comes from a library, not from the language, so this code won't build without it. In
IntelliJ, choose Tools | Nylon | Add Nylon Library… and pick build-hat. It brings the Raspberry
Pi library with it, and adds one line to your project's build.markup.code:
(+dependencies : RepositoryDependency coordinate={"com.grandtheorem:build-hat:0.3.0"} /)
Wait for a color
Plug the sensor into port B. Any port works, as long as the program names the same one.
Pi is the board, BuildHat is the HAT on top of it, Port names the HAT's ports, and Color names LEGO's colors.
# Wait for red, then print what it sees.
namespace colors
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import grandtheorem.buildhat.Color
import namespace nylon.time
BuildHat.connect starts the HAT, and colorSensor gives the sensor on a port, with its light on.
# The Color Sensor in port B.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let eye = hat.colorSensor(Port.B)
waitUntilColor waits until the sensor sees a color, for as long as you give it, and answers whether it did.
# Wait up to 15 seconds for something red.
if (eye.waitUntilColor(Color.Red, 15.seconds())) {
println("red")
} else {
println("no red in 15 seconds")
}
rgb() is what it sees as red, green and blue, each 0 to 255. reflected() is how much of its own light comes back,
from 0 to 100 %.
# What it sees now, as numbers.
if (let rgb? = eye.rgb()) {
println("red ${rgb.red}")
println("green ${rgb.green}")
println("blue ${rgb.blue}")
}
if (let light? = eye.reflected()) {
println("reflected ${light} %")
}
light(false) turns the sensor's light off. Without it, the light stays on after the program ends.
# Turn its light off before the program ends.
eye.light(false)
}
Hold a red brick in front of the sensor, close to it. The program prints red, then the brick's red, green and blue,
and how much light it reflects. Then it turns the sensor's light off.
Everything the sensor can do
| Call | What it does |
|---|---|
hat.colorSensor(port) | The Color Sensor on a port, with its light on |
eye.color() | The color in front of it, as a Color?: nothing when it sees none of them |
eye.rgb() | What it sees as red, green, blue and intensity, each 0 to 255, as an Rgb? |
eye.reflected() | How much of its own light comes back, 0 to 100 %, as an i32? |
eye.ambient() | How much light there is around it, 0 to 100 %, as an i32? |
eye.waitUntilColor(color, timeout) | Waits until it sees color, and answers whether it did in time |
eye.light(on) | Turns its own light on or off |
Color is one of LEGO's colors: Color.Black, Pink, Lilac, Blue, Cyan, Turquoise, Green, Yellow,
Orange, Red and White. The color color() gives is the one the sensor itself names.
Good to know
A reading is taken when you ask for it. rgb(), reflected() and ambient() each average four readings. Any of
them gives nothing when the sensor doesn't answer within half a second.
A wait checks every 50 milliseconds. A color that passes in front of it faster than that can be missed.
The wrong part on the port ends the run. If port B has a motor instead, the run ends and says what's there.
Boards: every board on Boards.