Page 1 of 1

NXC Color Sensor Help

Posted: 23 Feb 2011, 04:06
by pesso
How do I read an NXT 2.0 color sensor in NXC?

PS: This is the 1999th post in the Software Forum!

Re: NXC Color Sensor Help

Posted: 23 Feb 2011, 04:33
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);
    }
  }
}

Re: NXC Color Sensor Help

Posted: 23 Feb 2011, 04:48
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

Re: NXC Color Sensor Help

Posted: 24 Feb 2011, 00:46
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?

Re: NXC Color Sensor Help

Posted: 24 Feb 2011, 00:52
by mattallen37
Yes, try upgrading. Also, you may need the enhanced firmware for the NXT.