Reading Colors

Lego EV3 Mindstorms includes a Color Sensor that we can use to read colors. Let’s learn how we can read colors using Kotlin.

color sensor

First of all, we need to connect the color sensor to the brick. Bear in mind that in your brick, ports with numbers are used for sensors, and the ones with letters are for motors. Now, connect your color sensor to port 1 using any cable you want. Create a LeJOS project,add Kotlin, and create a file with this code:

package com.kotlinrobots.colors

import lejos.hardware.BrickFinder
import lejos.hardware.port.SensorPort
import lejos.hardware.sensor.EV3ColorSensor
import lejos.robotics.Color

fun main(args: Array<String>) {

    var colorSensor = EV3ColorSensor(SensorPort.S1)

    val colorIDMode = colorSensor.colorIDMode
    var sample = FloatArray(colorIDMode.sampleSize())
    
    BrickFinder.getLocal().keys.waitForAnyPress()

    colorIDMode.fetchSample(sample, 0)
    var colorId = sample.get(0).toInt()
    var colorName =
            when (colorId) {
                Color.GREEN -> "GREEN"
                Color.BLUE -> "BLUE"
                Color.YELLOW -> "YELLOW"
                Color.MAGENTA -> "MAGENTA"
                Color.ORANGE -> "ORANGE"
                Color.WHITE -> "WHITE"
                Color.BLACK -> "BLACK"
                Color.PINK -> "PINK"
                Color.GRAY -> "GRAY"
                Color.LIGHT_GRAY -> "LIGHT_GRAY"
                Color.DARK_GRAY -> "DARK_GRAY"
                Color.CYAN -> "CYAN"
                Color.BROWN -> "BROWN"
                Color.NONE -> "NONE"
                else -> "OTHER"
            }

    println("Color: ${colorName}")
 
    BrickFinder.getLocal().keys.waitForAnyPress()
}

In this piece of code, we are connecting the color sensor to Port 1 and printing on the screen the color name. Note how useful is when because it can return a String value that is assigned to colorName variable.

When we want to read a color using leJOS, we have to write many lines of code that can confuse beginners. This is what we do: 1) get color Mode , 2) create a float array to store values, 3) invoke fetchSample(), 4) get color id and convert it to int so that later we can retrieve its name. That’s a lot of ceremony for just one color!

Let’s see how we can improve this code using Kotlin features and abstract that logic:

package com.kotlinrobots.colors

import lejos.hardware.BrickFinder
import lejos.hardware.port.SensorPort
import lejos.hardware.sensor.EV3ColorSensor
import lejos.robotics.Color

fun EV3ColorSensor.getColorName(): String {
    val colorIDMode = this.colorIDMode
    var sample = FloatArray(colorIDMode.sampleSize())
    colorIDMode.fetchSample(sample, 0)

    return when (sample.get(0).toInt()) {
        Color.GREEN -> "GREEN"
        Color.BLUE -> "BLUE"
        Color.YELLOW -> "YELLOW"
        Color.MAGENTA -> "MAGENTA"
        Color.ORANGE -> "ORANGE"
        Color.WHITE -> "WHITE"
        Color.BLACK -> "BLACK"
        Color.PINK -> "PINK"
        Color.GRAY -> "GRAY"
        Color.LIGHT_GRAY -> "LIGHT_GRAY"
        Color.DARK_GRAY -> "DARK_GRAY"
        Color.CYAN -> "CYAN"
        Color.BROWN -> "BROWN"
        Color.NONE -> "NONE"
        else -> "OTHER"
    }
}

fun main(args: Array<String>) {
    var colorSensor = EV3ColorSensor(SensorPort.S1)
    BrickFinder.getLocal().keys.waitForAnyPress()

    println("Color: ${colorSensor.getColorName()}")

    BrickFinder.getLocal().keys.waitForAnyPress()
}

In this code, we are creating an extension function for the color sensor that reads the color and returns the name. Then in the main function we can simply call that function and print the color name.

Source Code

Miguel

Leave a Reply

Your email address will not be published. Required fields are marked *