how to use light sensor in NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
shaoxiaojun
Posts: 13
Joined: 30 May 2012, 06:53

how to use light sensor in NXC

Post by shaoxiaojun »

I'm a new man in NXT2.0 world.
I nearly read all the docs in http://bricxcc.sourceforge.net/nbc/nxcdoc/index.html, but still didn't get a clear clue how to use the NXT2.0 light sensor, I ran the 'view' in brick with light sensor connected in IN_3, the ambient always show 100, same in my code:

Code: Select all

task main(){
     //Precedes(SnifferLight);
		 SetSensorLight(IN_3);
     int ambient =0;

     while(true){
		 ambient = Sensor(IN_3);
		 NumOut(10, LCD_LINE7, ambient);
		 Wait(1000);
		 }
}
also, could you guys show me a link or post how to use this sensor and understand it? I'm not a lazy man, but i have to say these docs are more like a API list but no explanation at all, it a bit hard for me...
thanks.
this is shawn.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: how to use light sensor in NXC

Post by mattallen37 »

The "light sensor" wasn't included in the 2.0 set, so you probably have the Lego color sensor.

I'm fairly sure all of these are specifically for the Lego color sensor. These setup the type of light you want to use:

Code: Select all

SetSensorColorFull("port");
SetSensorColorRed("port");
SetSensorColorGreen("port");
SetSensorColorBlue("port");
SetSensorColorNone("port");
SetSensorType("port", SENSOR_TYPE_COLORFULL);
SetSensorType("port", SENSOR_TYPE_COLORRED);
SetSensorType("port", SENSOR_TYPE_COLORGREEN);
SetSensorType("port", SENSOR_TYPE_COLORBLUE);
SetSensorType("port", SENSOR_TYPE_COLORNONE);
These are for reading the values:

Code: Select all

ReadSensorColorRaw("port", "rawValues");
ReadSensorColorEx("port", "colorVal", "raw", "norm", "scaled");
And, I'm fairly sure that if you just want the default values (like the color number), you can use >Sensor("port")<.

I haven't personally done much with the Lego color sensor, so I'm not 100% sure.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: how to use light sensor in NXC

Post by mattallen37 »

Try this:

Code: Select all

unsigned int rawValues[4];

task main(){
  SetSensorColorFull(S1);
  while(true){
    ReadSensorColorRaw(S1, rawValues);
    ClearScreen();
    NumOut(0, LCD_LINE1, Sensor(S1));
    NumOut(0, LCD_LINE2, rawValues[0]);
    NumOut(0, LCD_LINE3, rawValues[1]);
    NumOut(0, LCD_LINE4, rawValues[2]);
    NumOut(0, LCD_LINE5, rawValues[3]);
    Wait(50);
  }
}
That displays the current color (top line) the red, green, and blue components, as well as the blank value (value when the LED is off). All the values (except obviously the color number) are raw values, so they should be in the range of like 150-800 or so.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: how to use light sensor in NXC

Post by spillerrec »

I don't quite remember either, but think mattallen is correct, you cannot use SetSensorLight for the color sensor. The SetSensorRed() should be the most similar to the light sensor when you want to read a light value from a surface, use SetSensorNone() if you want to turn the lamp off and use it to read ambient light. The red, green and blue version controls which color light it emits.

Use the tutorial document to get an introduction to NXC, it explains how to use BCC and NXC from the very basics. It is on the page you linked to, but you can also find it in BCC by clicking Help->Tutorials PDFs->NXC. (It is based on NXT 1.0 though.)

There is a online version of the API here which is much easier to read than the current PDF: http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/
It is as well documented as most other APIs in my opinion. As with any other API, it becomes much easier to understand when you get a bit more used to that particular API. Try to look at examples and tutorials first, then slowly start using the API more and more.

EDIT: It makes me wonder, could you use the raw values to get a better grayscale reading by using a proper RGB to grayscale conversion?
EDIT2: Now that I think about it, why would you read the grayscale value of a colored object if you have RGB information available? : P
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
shaoxiaojun
Posts: 13
Joined: 30 May 2012, 06:53

Re: how to use light sensor in NXC

Post by shaoxiaojun »

so appreciate for the qucik response.
now the color can be saw in my program, but one more question is about the
That displays the current color (top line) the red, green, and blue components, as well as the blank value (value when the LED is off).
from mattallen37. what's mean of the blank value? I understand it as the ambient light power, the stronger light should have the bigger value, so i wrote the code:

Code: Select all

unsigned int rawData[], normData[];
int scaledData[];
int cval;
SetSensorColorNone(S3);
ReadSensorColorEx(S3, cval, rawData, normData, scaledData);
when i try to show the rawData[3](skip the RGB), it always a constant 312, also, i run the view/Reflected light and veiw/Ambient light in brick, it always show 100%, could you help to clarify?
thanks lot.
this is shawn.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: how to use light sensor in NXC

Post by mattallen37 »

My program did work for you, right?

And about the values compared to the light brightness, that may be true for scaled values. However, typically the RAW value is the opposite, where the brighter the light, the lower the number. This seemingly backwards behavior is due to the HW, and is corrected by the FW for the scaled values (but obviously not for the RAW values).

Can you post your entire (current) program?

About "ambient" and "reflected", I think you are mixing functions from two sensors. The Lego light sensor (included in the 1.0 and the 9797 sets) is analog. The only light source of it's own, is the red LED, which you can turn on and off.

You are using the Lego color sensor. This is a totally different sensor, with practically no similarities to the Lego light sensor. None of the APIs are interchangeable between the two sensors, because they function in very different ways.

As I said before, I don't have much experience with this sensor.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
shaoxiaojun
Posts: 13
Joined: 30 May 2012, 06:53

Re: how to use light sensor in NXC

Post by shaoxiaojun »

I can't remebmer where i saw a post before say the 'Color sensor' in NXT 2.0 is a superset for 1.0's light sensor, means the 2.0 sensor can detect the light power and the colors, and acts as the simple lamp. and I believe that becasue there's 3 'LED' in the sensor, please correct me if i got wrong...

the code is simple, I just want to implenment the function of enviroment light power and color detection.

Code: Select all


task main(){
     //SetSensorColorNone(S3);
		 SetSensorColorFull(S3);

		 while(true){
		 ReadSensorColorEx(S3, cval, rawData, normData, scaledData);
		 ClearScreen();
		 NumOut(0, LCD_LINE2, scaledData[0]);
		 NumOut(0, LCD_LINE3, scaledData[1]);
		 NumOut(0, LCD_LINE4, scaledData[2]);
		 switch (cval)
		 {
		 	case 1 :
			                TextOut(0, LCD_LINE1, "black");
      			                break;
                                                case 2 :
				TextOut(0, LCD_LINE1, "blue");
      					 break;
		                case 3 :
				TextOut(0, LCD_LINE1, "green");
      					 break;
			case 4 :
				TextOut(0, LCD_LINE1, "yellow");
      					 break;
      			case 5 :
				TextOut(0, LCD_LINE1, "red");
      					 break;
      			case 6 :
				TextOut(0, LCD_LINE1, "white");
      					 break;
 			default :
				TextOut(0, LCD_LINE1, "Unknow value:");
				NumOut(80, LCD_LINE1, cval);

      }
    Wait(50);
  }
}
I'm new here, so very confused about the parameter ' normData, scaledData' here, and when and why need to turn on the different LEDs(Red, Green, Blue), I know the 'rawData' is a value(rgb) to unique desribe one color, and 'cval' is a simplified and pre-defined by LEGO base on the rgb value.
for this code, if i use 'SetSensorColorNone(S3);', the

Code: Select all

NumOut(0, LCD_LINE2, scaledData[0]);
		 NumOut(0, LCD_LINE3, scaledData[1]);
		 NumOut(0, LCD_LINE4, scaledData[2]);
always show a constant. what's the different with the ColorFull and ColorFull?
please help to clarify.
thanks lot.
this is shawn.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: how to use light sensor in NXC

Post by mattallen37 »

Here is how the color sensor works, according to my limited understanding. There is only one light sensitive "sensor" (a photo-transistor), and it cannot detect color at all (it only measures overall brightness). There is also an RGB LED. In this case, the RGB LED has only 4 states; Off, Red, Green, and Blue. When you set the color to "Full", you are actually telling it to cycle between all 4 of the LED states. While it is cycling, it is measuring the light level from the photo-transistor. When the LED is off, the sensor value is stored in the "Blank" value. When the LED is red, the sensor value is stored in the "Red" value, and the same is true for green and blue.

The sensor itself can not detect color! It would have no way of distinguishing an ambient red light, from an ambient blue light. By turning the LED to a certain state (1 of 4), and then reading the photo-transistor value, it can measure the reflected light for each color component, but it also will pick up any ambient light. The "blank" value is the photo-transistor value when the LED is off (so it only measures ambient light). By subtracting the blank value from the other three values, the NXT is effectively "canceling out" any ambient light from the three color readings.

As far as the NXC API, I am not sure what you want to use.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: how to use light sensor in NXC

Post by spillerrec »

I will try to set things a bit more clear.

The new color sensor and the old light sensor are not compatible. While the color sensor can act as a light sensor, it is not the same as the old light sensor and requires a different set of commands.

The view programs Reflected light and Ambient light therefore only works with the light sensor and not with the color sensor. The NXT firmware only provides a Color view program for the color sensor and not for the color sensor in light mode.

The color sensor is only supposed to detect 6 colors: black, blue, green, yellow, red and white. The raw RGB readings are intended for advanced users and isn't available in NXT-G either. (Third-party blocks might exists, but nothing official.)

To access the readings from the color sensor in the normal fashion, just use Sensor(S3) as with the other sensors:

Code: Select all

task main(){
  //SetSensorColorFull(S3);
  //SetSensorColorRed(S3);
  SetSensorColorNone(S3);
  while(true){
    ClearScreen();
    NumOut( 0, LCD_LINE1, Sensor(S3) );
    Wait(20);
  }
}
With the SetSensorColorFull() you get a value in the range 1- 6.
With SetSensorColorRed() you get a value in the range 0-100, with the sensor detecting reflected light. (You could use Green or Blue as well, but the old light sensor used a red LED.)
With SetSensorColorNone() you get a value in the range 0-100, with the sensor detecting ambient light.


@matt,
I tried taking some photos with high and low shutter speeds, and while I could clearly see the LED turn on and off, I couldn't see the individual colors in the LED turn on and off, they appeared all to be on at the same time. At a shutter speed of 1/800 (which is 1.25 msec), I couldn't spot the difference for the on state compared to a shutter speed of 1/8 (125 msec).
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
shaoxiaojun
Posts: 13
Joined: 30 May 2012, 06:53

Re: how to use light sensor in NXC

Post by shaoxiaojun »

I think i tried

Code: Select all

SetSensorColorNone(S3);
yesterday and seems it always return a constant, whatever, I'll confirmed it by today and post the testing result here, hope this can help some starters...

So the conclusion of the difference between the old light sensor and NXT 2.0 color sensor are:
1. old one can acts as a lamp(white light?) and detect the ambient light.
2. new one can also acts as a lamp(but only for red, green or blue) and detect the ambient light use SetSensorColorNone.
3. new one can detect 6 colors in simple mode and theoriodically all colors in raw mode(your can set a mapping table base on Rgb value).
4. use

Code: Select all

SetSensorColorRed
, we can read actually the reflected red light on object's surface, as well as blue and green, and this is the theory of color sensor.


not very understand your 'shutter' testing here, I'll keep tracking you post if want to share sth.

thanks lot.
this is shawn.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 7 guests