NXC Color Sensor Help

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
pesso
Posts: 8
Joined: 29 Sep 2010, 23:51
Location: Under your Bed

NXC Color Sensor Help

Post by pesso »

How do I read an NXT 2.0 color sensor in NXC?

PS: This is the 1999th post in the Software Forum!
51st Member Of MindBOARDS! (well, that's that the member's list says)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Color Sensor Help

Post by mattallen37 »

This is one way

Code: Select all

task main()
{
  while (true)
  {
    SetSensorColorFull(S1);
    ColorSensorReadType csr;
    csr.Port = S1;
    SysColorSensorRead(csr);
    if (csr.Result == NO_ERR)
    {
      NumOut(0, LCD_LINE1, csr.ColorValue);
    }
  }
}
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Color Sensor Help

Post by afanofosc »

If you are wanting to just read the color number in full color mode then use SetSensorColorFull(port) to configure the port to work with the color sensor in full color mode and read the color value with SENSOR_n where n is S1..S4. If you want to use the color sensor as a regular light sensor then use SetSensorColorRed(port) and read the scaled value (0..100%) using SENSOR_n (i.e., SENSOR_1, SENSOR_2, SENSOR_3, or SENSOR_4). You could also use SetSensorColorGreen or SetSensorColorBlue if you want to experiment with other light colors instead of red. To read the raw values in these non-full-color modes you can use SensorRaw(port) or SensorValueRaw(port) to read the raw value. The NXC Programmer's Guide contains documentation for additional color sensor API functions that you can use in NXC.

http://bricxcc.sourceforge.net/nbc/nxcd ... index.html

Drill down in the navigation tree to Modules\NXT Firmware Modules\Input module\Input module functions\.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
pesso
Posts: 8
Joined: 29 Sep 2010, 23:51
Location: Under your Bed

Re: NXC Color Sensor Help

Post by pesso »

I have tried SetSensorColorFull(S#), but I keep getting errors. For example, I get the following errors with mattallen37's code:

Code: Select all

Line 5: Undefined Indentifer SetSensorColorFull
Line 5: "=" expected
Line 5: ";" expected
Line 6: Undefined Identifier ColorSensorReadType
Line 6: "=" expected
Line 7: Math Factor expected
Line 7: Undefined Identifier ColorSensorReadType
Line 8: Undefined Identifier SysColorSensorRead
Line 8: "=" expected
Line 8: Undefined Identifier csr
Line 8: ";" expected
Line 9: Undefined Identifier csr.result
FYI, In BricxCC if I go to Preferences/Compiler/NBC&NBC and click "Version" I get 1.0.1.35. (I think that's my problem) I cannot upgrade at the moment. All I need to do is tell the difference between black and white. Is there still anything I can do?
51st Member Of MindBOARDS! (well, that's that the member's list says)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC Color Sensor Help

Post by mattallen37 »

Yes, try upgrading. Also, you may need the enhanced firmware for the NXT.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

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