Hi.
I'm trying to use the Color Sensor like a Light Sensor with nxc. I tried the line follower wrote by Benedettelli on his tutorial Programming LEGO NXT Robots using NXC but it doesn't work. The robot (classic Quick Start of NXT 2.0 kit) go always straight and the light of the Light Sensor is always off.
This is the code I used:
#define LUMINOSITA_SOGLIA 35
task main()
{
SetSensorLight(IN_2); // Definisce il sensore 2 come sensore della luce
OnFwd(OUT_AC, 75);
while(true)
{
if (Sensor(IN_3) > LUMINOSITA_SOGLIA)
{
OnRev(OUT_C, 75);
until(Sensor(IN_3) <= LUMINOSITA_SOGLIA);
OnFwd(OUT_AC, 75);
}
}
}
I read also Hansen's book LEGO MINDSTORMS NXT Power Programming - Robotics in C but the Light Sensor set the sensor in a difficult way, that i don't understand.
What is the problem? How I can set the Color Sensor working like the old Light Sensor?
Thank you for replies.
Rocco.
The color sensor cannot be used like the light sensor without some code changes. It isn't a light sensor so you can't simply configure the port to which it is connected for use with a light sensor. If you do that then the firmware treats the color sensor like it is the NXT 1.0/1.1 light sensor and that won't work. You have to tell the firmware the correct sensor type if you want it to communicate with the sensor correctly. To use a color sensor as a light sensor you need to use SetSensorColorRed(port). And as muntoo pointed out, make sure you use the right port number throughout your program. Once you have correctly configured the port to work with the color sensor then you can use Sensor(port) or SensorValue(port) or SENSOR_n (where n is 1..4) to read the light sensor value from the color sensor.
afanofosc wrote:The color sensor cannot be used like the light sensor without some code changes. It isn't a light sensor so you can't simply configure the port to which it is connected for use with a light sensor. If you do that then the firmware treats the color sensor like it is the NXT 1.0/1.1 light sensor and that won't work. You have to tell the firmware the correct sensor type if you want it to communicate with the sensor correctly. To use a color sensor as a light sensor you need to use SetSensorColorRed(port). And as muntoo pointed out, make sure you use the right port number throughout your program. Once you have correctly configured the port to work with the color sensor then you can use Sensor(port) or SensorValue(port) or SENSOR_n (where n is 1..4) to read the light sensor value from the color sensor.
John Hansen
Ok.It's clear. But why we need SetSensorColorRed() with the new Color Sensor to work like the old Light Sensor?
Another question. In your book, for the line follower, you first calibrate the sensor. It this really necessary? Or we can make a simple line follower without calibration (like on Benedettelli's tutorial)?
#define LUMINOSITA_SOGLIA 35
task main()
{
SetSensorColorRed(IN_3); // Definisce il sensore 3 come sensore della luce
OnFwd(OUT_AC, 75);
while(true)
{
if (Sensor(IN_3) > LUMINOSITA_SOGLIA)
{
OnRev(OUT_C, 75);
until(Sensor(IN_3) <= LUMINOSITA_SOGLIA);
OnFwd(OUT_AC, 75);
}
}
}
Thank you very much!
Maybe I also understand why the ColorRed. Red is the light emitted by the sensor to measure the light reflected (there isn't a withe light like on the old sensor). Right? So it works also with ColorBlue and ColorGreen?
1. The older light sensor has a red LED, not a white one. Red LEDs are the cheapest kind.
2. Calibration definitely helps a line follower if it's going to used in more than one ambient light situation. In low light the threshold values will quite a bit lower than in bright light.
As Morton mentions, the reason for calibrating a line follower is so that it works well in the current ambient lighting situation. If you will only run it in exactly the same lighting conditions as you test it in then you don't need calibration but usually that is not how things work in real life. The values you get back from the light sensor will vary in different lighting and differing line following surfaces.
Using the green or blue LED in the color sensor will return different values than what you get using the red LED and probably will not allow you to distinguish between black and white as well as the red LED does. You should experiment with the color sensor to see what values you get from it over white and black colors with the 3 different LEDs so that you are familiar with its capabilities.
afanofosc wrote:As Morton mentions, the reason for calibrating a line follower is so that it works well in the current ambient lighting situation. If you will only run it in exactly the same lighting conditions as you test it in then you don't need calibration but usually that is not how things work in real life. The values you get back from the light sensor will vary in different lighting and differing line following surfaces.
Using the green or blue LED in the color sensor will return different values than what you get using the red LED and probably will not allow you to distinguish between black and white as well as the red LED does. You should experiment with the color sensor to see what values you get from it over white and black colors with the 3 different LEDs so that you are familiar with its capabilities.
John Hansen
Yes, I understand the reasons of calibration but I have to proceed step by step! And calibration is a high step, but I will try calibration sooner or later.
I will also try if the line follower work well with the blue LED or the green LED and I will post my result.