Lego Mindstorms chameleon

For my first article I’ll be doing a chameleon Lego Mindstorms LED blink. I will use the color sensor for both the RGB sensor and RGB LED. My goal is to reproduce the color sensed on the RGB LED. The Lego model is really simple, just the color sensor connected to port 1 of the brick.

I flashed my Lego Mindstorms brick (I’ll called it NXT later on) with the LeJOS firmware which allows you to program the NXT using Java. I’ve been trying the visual programming system proposed by Lego for 1 hour and decided it was too complicated for me … I might be too old school. The installation of the LeJOS firmware and SDK has been really easy on Ubuntu 12.04 thanks to the official LeJOS tutorial. I also setup Eclipse Indigo with the LeJOS Eclipse plugin. I was very surprised how easy it was to install and use LeJOS.

As for many of the source codes I will be providing here, this one is self explanatory and I won’t comment it line by line.

package org.ithasu.chameleon;

import lejos.nxt.Button;
import lejos.nxt.ColorSensor;
import lejos.nxt.SensorPort;
import lejos.robotics.Color;

public class Chameleon {
	public static void main(String[] args) {
		System.out.println("== Chameleon ==");
		System.out.println("Enter to sense,");
		System.out.println("any other exit");

		ColorSensor sensor = new ColorSensor(SensorPort.S1);
		while (true) {
			Color c = sensor.getColor();
			if (!sensor.setFloodlight(c.getColor())) {
				// Color is not recognized
				try {
					for (int i = 0; i < 6; i++) {
						sensor.setFloodlight(!sensor.isFloodlightOn());
						Thread.sleep(500);
					}
					sensor.setFloodlight(false);
				} catch (Exception e) {
				}
			}
			if (Button.waitForAnyPress() != Button.ID_ENTER) {
				break;
			}
		}
	}
}

The only problem is that the ColorSensor seems unable to reproduce others colors than red, green and blue (by mixing the RGB for example). I did not investigated this part yet, but has the ColorSensor returns false if color cannot be reproduced, I did some red LED blinking to notify me. Here is a short video where we can see that red, green and blue work well, while white gives a no light and yellow is reported as an error.

Leave a Reply

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