Page 1 of 1
Having problems getting a reading from light sensor in NXC
Posted: 13 Oct 2010, 21:59
by skelly89
Hello, I'm wondering if I could get some help on utilizing the NXT 2.0 Light Sensor using NXC and BricxCC. I am having a lot of issues getting the Light Sensor to work properly in both RobotC and NXC, and have a sneaking suspicion that it's a simple beginner error. I am trying to program a robot to follow a line, but it seems like the light sensor is not picking up anything at all. I have even tried to use the example line program and the robot seems unresponsive. Here is an example of a simple program that seems like it sounds work:
Code: Select all
#define THRESHOLD 40
task main()
{
SetSensor(SENSOR_2, SENSOR_LIGHT);
OnFwd(OUT_AC, 70);
while(true)
{
if(SENSOR_2 > THRESHOLD)
{
OnRev(OUT_C, 70);
Wait(10);
until (SENSOR_2 <= THRESHOLD)
OnFwd(OUT_AC, 70);
}
}
}
I have even messed around with the value of threshold and nothing seems to happen. I also initialized the sensor as SENSOR_TYPE_LIGHT_ACTIVE (which to my knowledge should turn on the red LED for low light situations) but the LED did not turn on.
I programmed a simple RobotC program that output the value the sensor was reading, and every reading it gave 100, and would not change no matter what the sensor was pointed at. Am I not reading or storing the values right? I know my sensor is not working because if the program is written in NXT-G the sensor does not seem to have a problem turning on and giving readings. Any step towards the right direction would be most appreciated.
Re: Having problems getting a reading from light sensor in NXC
Posted: 13 Oct 2010, 22:37
by m-goldberg
I think your problem is that SENSOR_LIGHT refers to the old version NXT 1.0 light sensor. The new sensor 2,0 is defined by names like SENSOR_COLORxxxx, where xxx varies according to sensor mode used.
Re: Having problems getting a reading from light sensor in NXC
Posted: 13 Oct 2010, 23:11
by gloomyandy
I'm no NXC expert but isn't SENSOR_2 just a constant (#define)? In which case don't you need to call a function of some sort to read the sensor value? Unlike RobotC I don't think NXC maps sensor readings into variables behind the scenes...
Andy
Re: Having problems getting a reading from light sensor in NXC
Posted: 14 Oct 2010, 02:09
by gusjansson
The input port constants are S1, S2, S3, and S4. These are just constants identifying the sensor port. You cannot read the sensor by just specifying the constant. To read the sensor use the function Sensor(port) or SensorRaw(port) to read the raw sensor value.
Here is your corrected program:
Code: Select all
#define THRESHOLD 40
task main()
{
SetSensor(S2, SENSOR_LIGHT);
OnFwd(OUT_AC, 70);
while(true)
{
if(Sensor(S2) > THRESHOLD)
{
OnRev(OUT_C, 70);
Wait(10);
until (Sensor(S2) <= THRESHOLD)
OnFwd(OUT_AC, 70);
}
}
}
Gus
Re: Having problems getting a reading from light sensor in NXC
Posted: 14 Oct 2010, 02:49
by afanofosc_99
skelly89 wrote:
Code: Select all
SetSensor(SENSOR_2, SENSOR_LIGHT);
The problem, I believe, is that you are using SENSOR_2, which can only be used in NXC as an alternative to calling Sensor(S2), instead of S2 which is a port number/name constant that must be used in any NXC API function that requires a port number. Hopefully the NXC Help documents and Guide PDF demonstrate and document this correctly. Some people use IN_n for port names. Those constants are intended for use in NBC and not NXC. The Sn constants are designed to be compatible with NQC code. Unlike NQC, however, which allowed you to use SENSOR_n constants for both naming a port and reading a sensor's value, NXC does not support that dual purpose for the SENSOR_n constants.
In summary:
1) use S1, S2, S3, or S4 in NXC code whenever you are required to pass a port value into an API function. Alternatively you can use a variable whose value is equal to one of these 4 constants.
2) Use Sensor(S1) or SENSOR_1 and so forth for any of the 4 ports so long as the attached sensor is an analog device (not an i2c device). If you look in the header file (NXCDefs.h) you will see #define SENSOR_1 Sensor(S1) so these two are equivalent.
3) SetSensor is not often used by people who did not come from the NQC world. It should work the same as SetSensorLight(S1) if you call it with the right parameters. SetSensor(S1, SENSOR_LIGHT);
I encourage you to read the Input module documentation in the NXC Help and NXC_Guide.
John Hansen
Re: Having problems getting a reading from light sensor in NXC
Posted: 14 Oct 2010, 02:53
by afanofosc
And Morton is quite right that SENSOR_LIGHT is for the old RCX light sensor. You should use SENSOR_NXTLIGHT with the NXT light sensor (or, my preference, use SetSensorLight instead).
Or, actually, as Morton mentioned, if you actually are referring to the NXT 2.0 color sensor you should use SENSOR_COLORRED or SENSOR_COLORFULL, depending on what you are really trying to do. If you use SENSOR_COLORRED it should work like the NXT 1.0 light sensor though the values you get from it will not be exactly like those returned by the NXT 1.0 light sensor.
There was at least one really good thread about this on the old forums.
John Hansen
Re: Having problems getting a reading from light sensor in NXC
Posted: 14 Oct 2010, 16:55
by skelly89
Thanks for the help guys. I do understand that you need to call the Sensor function to get the value, it was an accidental typo putting Sensor_S2 instead of Sensor(S2). I had it correct in my test programs but wrote it wrong when typing up this post. I understand that you need to have a function call in order to get that value, but thank you for the detailed explanations as of why.
It looks like my problem was that I was not setting up the correct sensor. I did not know that the first NXT set came with a different light sensor, I thought they were both the same. So if I use
Code: Select all
SetSensorLight(S2);
if(Sensor(S2) > THRESHOLD)
{
foobar;
}
That function call Sensor(S2) will return a value between 1 and 100? Or does the color sensor only give values of 1 through 6 for color values?
edit: Actually, after looking at the post above this more, since I am using the NXT color sensor should I initialize it as
SetSensor(S2, SENSOR_COLORFULL); ? Unfortunately I am away from my Windows tower so I cannot test what you guys are helping me with. Thank you again.
Re: Having problems getting a reading from light sensor in NXC
Posted: 14 Oct 2010, 19:00
by afanofosc
Here's a few quotes from the old forums:
The NXT 2.0 color sensor cannot be used with the NXT 1.0 light sensor type. You need to use one of the new types available in the 1.28 firmwares specifically designed to work with the new color sensor. For full color mode, use SetSensorColorFull(byte port). You can also use it in a single color light sensor mode with SetSensorColorRed(byte port), SetSensorColorGreen(byte port), SetSensorColorBlue(byte port), and SetSensorColorNone(byte port). The "none" option is for ambient light (no lamp). In full color mode the sensor returns a color number via the Sensor(S3) or SENSOR_3 API calls. I am not sure how you would get the color sensor to return a value that is directly analogous to the values returned by the light sensor. There are recent posts on the Nxtasy forums that I encourage you to read that describe the NXC API functions that work with the LEGO color sensor. Just look in the NXT Software forum for threads about the NXT 2.0 color sensor. You might try seeing what values you get when you set the sensor type and mode using SetSensorColorRed(port) and read its values using Sensor(S3). S3 == IN_3 but is meant for use in NXC while IN_3 is meant for use in NBC.
To the best of my knowledge, using the new color sensor with the red lamp (SetSensorColorRed) and reading the raw sensor values using SensorRaw(port) will be as fast and accurate as the light sensor in the NXT 1.0 set.
I would configure the port for a color sensor in RED mode using SetSensorColorRed(port) and then read it using SENSOR_2 or Sensor(S2) (either should work). Or read the raw value using SensorRaw(port).
John Hansen