Using the NXT 2.0 Colour Sensor as an old fashioned NXT 1.0 Light Sensor as very simple in NXT-G but a little more involved in ROBOTC. The old Light Sensor uses a small red LED to illuminate the target and a small sensor to see how much was reflected. The new colour sensor can be used in a very similar way. The only thing is you will need to do some work yourself to turn it into a normalised value (0-100).
Below is the code for a simple program that allows you to read both the raw value from the Colour Sensor and a normalised one. Make sure you change the blackValue and whiteValue variables to match your own environment.
#pragma config(Sensor, S1, COLOUR, sensorCOLORRED)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* Program to use the NXT 2.0 colour sensor as a standard Lego Light sensor
* Written by Xander Soldaat 18-Feb-2011
*/
// Calibration values, these may be different for you
const int blackValue = 220;
const int whiteValue = 580;
// Normalise a raw value, returns a value from 0 to 100
int normaliseReading(int rawValue) {
// Anything less than the black value should return 0
if (rawValue <= blackValue)
return 0;
// Anything brighter than the white value should return 100
else if (rawValue >= whiteValue)
return 100;
// Anything else should be calculated in a scale from 0-100%
else
return ((long)(rawValue - blackValue) * 100) / (whiteValue - blackValue);
}
task main () {
int rawValue = 0;
int normalisedValue = 0;
while (true) {
// Get the raw value from the sensor.
rawValue = SensorRaw[COLOUR];
// Calculate the normalised value
normalisedValue = normaliseReading(rawValue);
nxtDisplayTextLine(4, "Raw: %3d", rawValue);
nxtDisplayTextLine(5, "Norm: %3d", normalisedValue);
wait1Msec(100);
}
}
You can download the program here: [LINK].
Bot Bench I'd Rather Be Building Robots

“const int”? Can you make a future version of this possible to calibrate from the robot?
Sure, but this was just a simple example. Didn’t want to clutter it with too much stuff 🙂
Hello,
Are there sample program(s) for the Hitechnic rotation sensor.
Thanks
Jan Kromhout
Heelevoetsluis-NL
Jan, I am sure there are. Did you have a look here: http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NAA1030 ?
I just want to program the first version of a colour sensor to read black and white, but can’t work out how to do this. I have read that the sensor needs to be on an angle and not close to the line. For black is the ‘trigger point’ around 50? I have been using a low value.
Your trigger point might be set to 50 but without knowing what your “0” and “100” are, it’s a bit of an empty statement. Instead of “how long is a piece of string?” you’re now asking “how long is half a piece of string?”. It would depend on the length of the string, wouldn’t it?
I made an automatically calibrating, PID-controlled, dual-sensor line follower in ROBOTC that uses the NXT color sensor and the old NXT light sensor in a method similar to the above:
http://paste2.org/p/1565396
Code looks nice, do you have a video of this thing in action?
I am new to the color sensor and i would like some help on how to initialize the color sensor on it s color mode. A simple code on how the color sensor is used in the color mode would be really helpful. I am using the Bricx Command Center in C language. – Thank you
You would be better off asking this question on the Mindboards forums. There is a pretty big NXC community there 🙂 I don’t use that programming language enough to be able to tell you definitively.