The NXT 2.0 retail set comes with a new color sensor. It is an analog sensor rather than I2C like the HiTechnic color sensors. The LEGO color sensor requires the firmware that comes with the retail NXT 2.0 set (version 1.28). The firmware that comes with the recently released NXT 2.0 education software (version 1.26) does not include support for the LEGO color sensor.
NXC and BricxCC now support the new color sensor in the pre-release (aka test release) build that you can download via the following URLs:
http://bricxcc.sourceforge.net/test_release.zip
http://bricxcc.sourceforge.net/nbc/beta/nbc-1.0.1.b36.tgz
http://bricxcc.sourceforge.net/nbc/beta/nbc-1.0.1.b36.osx.tgz
Before you try to control the new color sensor using NXC or BricxCC you will need to make sure you tell the compiler that you are targetting the NXT 2.0 firmware rather than the old 1.0 firmware. With the command line compiler you tell the compiler this using the -v switch.
nbc -v=128 myprogram.nxc
The value 128 means version 1.28 which is the firmware version number of the NXT 2.0 retail firmware. The NXT 1.0 firmware version is 1.05 so you would use 105 with the -v switch if you want to target the 1.0 firmware.
In BricxCC all you need to do is check the “NXT 2.0 compatible firmware” option on the Compiler|NBC/NXC tab in the Preferences dialog.
There are 5 new sensor types that you use with the color sensor: SENSOR_TYPE_COLORFULL, SENSOR_TYPE_COLORRED, SENSOR_TYPE_COLORGREEN, SENSOR_TYPE_COLORBLUE, and SENSOR_TYPE_COLORNONE. These can be used with the SetSensorType API function in order to configure the sensor port for the color sensor. For detecting colors you normally will use the full color sensor type. You can use the four other types to detect ambient light levels (with the COLORNONE type) or as a color lamp in red, green or blue. If you want to use the color sensor as if it were a light sensor that came with the NXT 1.0 set you can use the COLORRED type to turn on the red lamp.
The NXC API also has sensor port configuration functions specifically for each of these sensor type: SetSensorColorFull, SetSensorColorRed, SetSensorColorGreen, SetSensorColorBlue, and SetSensorColorNone. Each of these functions calls SetSensorType with the appropriate sensor type constant, then calls SetSensorMode with SENSOR_MODE_RAW, and then finally calls ResetSensor to wait for the firmware to finish initializing the sensor based on the new type and mode. Your best bet is to use one of these 5 functions whenever you need to configure the NXT to use the color sensor.
Once you’ve properly configured the port for use with the new color sensor you can read data from the sensor in a variety of ways. The simplest way to read from the color sensor is to use the SENSOR_1, SENSOR_2, SENSOR_3, and SENSOR_4 values. These return the color number when the sensor is configured in full color mode. You can also read raw sensor values similar to the values you get from the light sensor using SensorRaw when you have the sensor configured as one of the non-full color types.
Direct access to the color sensor fields in the Input module IOMAP structure is provided by a few low level NXC API functions:
unsigned long ColorCalibration(const byte port, const byte pointNum, const byte colorNum);
unsigned int ColorCalLimits(const byte port, const byte pointNum);
unsigned int ColorADRaw(const byte port, const byte colorNum);
unsigned int ColorSensorRaw(const byte port, const byte colorNum);
int ColorSensorValue(const byte port, const byte colorNum);
byte ColorBoolean(const byte port, const byte colorNum);
byte ColorCalibrationState(const byte port);
There is also a low level system call function called SysColorSensorRead which takes a structure argument of type ColorSensorReadType. The definition of this structure is shown below:
struct ColorSensorReadType {
char Result;
byte Port;
int ColorValue;
unsigned int RawArray[4];
unsigned int NormalizedArray[4];
int ScaledArray[4];
bool Invalid;
};
On input to the SysColorSensorRead function call you set the Port field to S1, S2, S3, or S4. After the call you can read the Result field to check for errors and if no errors ocurred you can access the ColorValue field or read data from the 3 arrays. Each array is indexed using INPUT_RED (0), INPUT_GREEN (1), INPUT_BLUE (2), or INPUT_BLANK (3).
At a higher level you can read the raw color sensor values using ReadSensorColorRaw(byte port, unsigned int & rawValues[]). You can also read all the color sensor data using ReadSensorColorEx(byte port, int & colorValue, unsigned int & rawValues[], unsigned int & normValues[], int & scaledValues[]).
The ColorValue value returned by these functions will be one of these constants: INPUT_BLACKCOLOR, INPUT_BLUECOLOR, INPUT_GREENCOLOR, INPUT_YELLOWCOLOR, INPUT_REDCOLOR, and INPUT_WHITECOLOR (1..6, respectively).
Hopefully this information helps you get the most out of your new LEGO color sensor with NXC and BricxCC.