Lego RGB Color Sensor
Page 1 of 1

Author:  Boco [ Thu Aug 12, 2010 1:51 pm ]
Post subject:  Lego RGB Color Sensor

I have the version of RobotC that supports the Lego color sensor. Can anyone give me the code for a simple line following program that uses the color sensor instead of the light sensor?
Also, with NXT-G, you could use the color sensor as a light sensor (get numbers from 1 to 100 from the color sensor). Can you do something similar to that in RobotC.

Author:  Boco [ Mon Sep 06, 2010 10:11 am ]
Post subject:  Re: Lego RGB Color Sensor

Never mind... I got it

Author:  njkelton [ Fri Jul 15, 2011 1:10 am ]
Post subject:  Re: Lego RGB Color Sensor

Could you please share with us how?

Author:  mightor [ Fri Jul 15, 2011 1:36 am ]
Post subject:  Re: Lego RGB Color Sensor

#pragma config(Sensor, S1,     colorPort,           sensorCOLORFULL)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
  string sColor;

  while (true) {
    switch (SensorValue[colorPort])
      case BLACKCOLOR:    sColor = "Black";     break;
      case BLUECOLOR:     sColor = "Blue";      break;
      case GREENCOLOR:    sColor = "Green";     break;
      case YELLOWCOLOR:   sColor = "Yellow";    break;
      case REDCOLOR:      sColor = "Red";       break;
      case WHITECOLOR:    sColor = "White";     break;
      default:            sColor = "???";       break;
    nxtDisplayCenteredTextLine(2, sColor);

You will need ROBOTC version 2.26 beta.

- Xander

Author:  verynewguy [ Mon Aug 22, 2011 12:40 am ]
Post subject:  Re: Lego RGB Color Sensor

Hello every one! I'm new to this board and I have a question. I just updated to 2.26 to be able to use my RGB Lego sensor. All I need to do is figure out how to asign to three individual variables the raw value of each color. I used your snip above and it works great but the colors are predetermined.
In pseudo:

int RedRaw = (Something meaning Raw Red Value)
int GreenRaw = (Something meaning Raw Green Value)
int BlueRaw = (Something meaning Raw Blue Value)


Author:  mightor [ Mon Aug 22, 2011 1:06 am ]
Post subject:  Re: Lego RGB Color Sensor

Hiya Very New Guy!

You're in luck, I wasn't sure it was possible but if you open the ColorSensor.c sample program that came with your ROBOTC 2.26 installation, take a look around line 215:

void displayRGBColorValues()
   short nAtoDValues[4];
   short nRawValues[4];
   short nColorValues[4];
   string sTemp;

  getColorSensorData(colorPort, colorAtoD,   &nAtoDValues);
  getColorSensorData(colorPort, colorRaw,    &nRawValues);
  getColorSensorData(colorPort, colorValue,  &nColorValues);


  nxtDisplayTextLine(3, "   AtoD  Raw   %%");
  StringFormat(sTemp, "R  %4i%5i", nAtoDValues[0], nRawValues[0]);      nxtDisplayTextLine(4, "%s%4i", sTemp, nColorValues[0]);
  StringFormat(sTemp, "G  %4i%5i", nAtoDValues[1], nRawValues[1]);      nxtDisplayTextLine(5, "%s%4i", sTemp, nColorValues[1]);
  StringFormat(sTemp, "B  %4i%5i", nAtoDValues[2], nRawValues[2]);      nxtDisplayTextLine(6, "%s%4i", sTemp, nColorValues[2]);
  StringFormat(sTemp, "Amb%4i%5i", nAtoDValues[3], nRawValues[3]);      nxtDisplayTextLine(7, "%s%4i", sTemp, nColorValues[3]);

I think this is pretty much what you were looking for, no?

For your colour comparing pleasure, I also have for you on special offer an RGB to HSV conversion function, courtesy of one of my driver suite users:
 * Convert RGB colors to HSV
 * @param red the red input value
 * @param green the green input value
 * @param blue the blue input value
 * @param hue the hue output value (from 0 to 365, or -1 if n/a)
 * @param sat the saruration output value (from 0 to 100, or -1 if n/a)
 * @param value the value output value (from 0 to 100)
 * @return void
void RGBtoHSV(float red, float green, float blue, float &hue, float &sat, float &value)
   hue = 0;
   sat = 0;
   value = 0;

  //   Value
  float rgb_max = max3(red, green, blue);
  float rgb_min;
  value = rgb_max / 2.56;
  if (value == 0){
    hue = -1;
    sat = -1;

  //   Saturation
  red /= rgb_max;
  green /= rgb_max;
  blue /= rgb_max;

  rgb_max = max3(red, green, blue);
  rgb_min = min3(red, green, blue);
  sat = (rgb_max - rgb_min) * 100;
  if (sat == 0){
    hue = -1;

  //   Hue

  red = (red - rgb_min) / (rgb_max - rgb_min);
  green = (green - rgb_min) / (rgb_max - rgb_min);
  blue = (blue - rgb_min) / (rgb_max - rgb_min);

  rgb_max = max3(red, green,blue);
  rgb_min = min3(red, green,blue);

  if (rgb_max == red){
    hue = 0.0 + 60.0*(green-blue);
    if (hue < 0.0){
      hue += 360.0;
  } else if (rgb_max == green){
    hue = 120.0 + 60.0 * (blue-red);
  } else {
    hue = 240.0 + 60.0 * (red-green);

This function can also be found in light-common.h in my driver suite which you can download from my signature below.

- Xander

Author:  verynewguy [ Mon Sep 05, 2011 3:41 am ]
Post subject:  Re: Lego RGB Color Sensor

Ok, this is the bare-bones version. All I've done is erased all that wasn't raw-color code. There's no print to display just simply a way to access the raw data from each color short-variables in what I would call brightness levels. Hope this helps someone else out there. I surely learned a lot. Thanks Xander!

#pragma config(Sensor, S1,     colorPort,           sensorCOLORFULL)

short nAtoDValues[3]={0,0,0};
short red = 0;
short green = 0;
short blue = 0;

task main()
   wait1Msec(1000);  // Give sensor time to initialize
   while (true)
        getColorSensorData(colorPort, colorAtoD, &nAtoDValues );
        red = nAtoDValues[0];
        green = nAtoDValues[1];
        blue = nAtoDValues[2];

Author:  mightor [ Mon Sep 05, 2011 4:24 am ]
Post subject:  Re: Lego RGB Color Sensor

No problems. I edited your post to put the code inside [ code] tags. Next time just use them to preserve the spacing and all that.
[code]your code goes here[/code]

- Xander

Author:  Henry Dorsett Case [ Wed Nov 02, 2011 5:47 pm ]
Post subject:  Re: Lego RGB Color Sensor

Hello everybody,

I'm new to this board and to the whole Mindstorms world and it seems that I'm also a bit late for the party but anyway, I'll give it a try and post my question to this thread instead creating a new one. Apologies if that is an unwanted behavior here. And apologies for my clumsy english, it's obviously not my native language.
I want to use the Lego RGB Sensor coming with the 8547 Set as a simple light detector. I'm using ROBOTC 3.04 with FW 9.04. I played around with the different possibilities to use the RGB Sensor but when I try to use the measured numbers (I monitor them via the Debugging window) e.g. with the Lego Color Ambient settings, it seems that there is no way to read the correct data and use it e.g. in a loop. I can see data in the "Raw" and in the "Value" fields but it seems that I can't reference these values correctly. When using the sonar sensor, everything works like a charm if I use the raw values. Maybe this is a typical newbie nobrainer question but I searched the threads in this forum but up to now to no avail. I would be very thankful for a hint...


Author:  elizabethmartinez11 [ Thu Jan 24, 2013 5:20 pm ]
Post subject:  Re: Lego RGB Color Sensor

Hi everyone...well me and my group have to find out a way to make our robot read the colors,we are using RGB lights and we cant find a way to match the number for the color sensor since we get a reading from the light sensor when we go to the view section of the NXT. Can anyone help?

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group