# Robotc color sensor error

I am trying to write a simple program where the robot(Lego NXT2) will follow a blue line.

#pragma config(Sensor, S1,     ,               sensorCOLORBLUE)
//!!Code automatically generated by 'ROBOTC' configuration wizard               !!//

{
while(true)
{
if(SensorValue[S1] == sensorCOLORBLUE)
{
motor[motorB] = 0;
motor[motorC] = -50;
}
else
{
motor[motorB] = -50;
motor[motorC] = 0;
}
}
wait1Msec(1);
}


I am using an nxt color sensor and the problem is that only 1 motor is moving. I know that none of the motors are broken either because I tested them out. Can somebody help me diagnose my problem?

• Aren't you effectively starting one motor and stopping one motor in these code lines? Which seems to be the behaviour you're seeing. What is it that you're actually intending to do based on the color sensor value? – RaGe Sep 24 '14 at 16:13
• I interpreted “the problem is that only 1 motor is moving” as only one motor (eg, motor 1) ever moves, vs. only one motor at any given time (in accord with code). Lacking any response from OP we will never know – James Waldby - jwpat7 Sep 25 '14 at 17:00
• I notice that your wait1Msec(1) is outside your while loop, which is probably unrelated to your problem but still a red flag. – Ian Dec 1 '14 at 19:33
• Just to echo part of jwpat7's answer below, you should be troubleshooting this in pieces -- the sensor and the motor system separately -- instead of all together. As you'll find out, a program that runs on a robot (interacting with the real world) is far less reliable than the same program running on a computer in simulation. You'll spend a lot more time handling unexpected conditions with a robot. – Ian Dec 1 '14 at 19:39

Someone who owns a Lego NXT2 system may be able to check your code and provide a good answer.

Meanwhile, load some of the color sensor programs from the following links, run them with the sensor pointed at known colors, and edit your question to report whether your color sensor is working properly.

Color Range Block v1.0 from teamhassenplug.org

G1-FindColor, G2-SumoColor, etc from nxtprograms.com

Also see philohome.com's “Color Sensors Showdown” page.

According to this page the sensor basically gives you three integers corresponding to the intensity of red/green/blue light. The larger the number, the brighter the incoming light.

The error I suspect is that SensorValue[S1] actually is a value from 0 to 255 and the chance that you get exactly the number sensorCOLORBLUE is very small.

However, such an output means, that you can adjust the thresholds you want to be recognized as blue very nicely in software: for example by saying if the blue value is the highest of those 3, you have a blue line and act accordingly. Example (pseudo) code would be something like this:

if ( (sensor_output_blue > sensor_output_red) && (sensor_output_blue > sensor_output_green) {
//react to blue
//for example by turning left
} else {
//react to red or green
//for example by turning right (ie. in opposite direction)
}


If using all three colors at the same time is inconvenient, you can do something similar using only the brighness of blue:

if (sensor_output_blue > threshold_blue) {
//react to blue
} else {
//react to the other colors
}

• im hoping this isnt off topic but does anyone know some good resources i can use to learn RobotC? i am sort of new to this language and need help in learning it.i am actually part of a First Tech Challenge team and need some help in programming. – Rayyaan Mustafa Dec 1 '14 at 23:48
• @Rayyaan At least I cannot help you with that. I'd suggest googling for a tutorial and if you can't find one asking another question (and indicating in this question what you searched for (keywords) and why that was unsatisfactory). Also, are there good reasons to use this exact language/tool? Perhaps something with a big, helpful userbase would be better for a beginner. On a side note, did you solve the problem with the light sensors? If not we might be able to suggest other troubleshooting steps. If you have solved it, that would be nice to know for people looking up this question. – caconyrn Dec 9 '14 at 19:48
• Yea actually im not sure if there are any other languages that we can use on legomindstorms. i use RobotC on it. before ive used the drag and drop one but i think thats all. i think that you can use labview but im not sure. anywasy i use robotC cuz it seems to be the most common language in FTC thanks for all the help guys – Rayyaan Mustafa Dec 10 '14 at 22:24

Your comparison is wrong - sensorColorBLUE is a sensor setting, not a color. To detect a color, you need your sensor in RGB mode, and your comparison needs to be against a TColors type. Change the code as follows:

#pragma config(Sensor, S1,     colorSensor,    sensorColorNxtFULL)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

{
while(true)
{
if((TColors)SensorValue[colorSensor] == BLUECOLOR)
{
motor[motorB] = 0;
motor[motorC] = -50;
}
else
{
motor[motorB] = -50;
motor[motorC] = 0;
}
}
wait1Msec(1);
}


I kept the rest of your code [almost] as-is, however I suggest naming the sensor, as shown in my code. I also suggest naming your motors in "Motors and Sensors setup"; if you're applying -50 power because the motor is reversed, you should reverse the motors in setup.

Finally, you don't need the wait1Msec(1) command - that doesn't do anything at all here - the NXT color sensor will take about 3ms to scan, this additional delay is not at all necessary.

• Wow, I just realized this question is almost 2 and a half years old! Well, hopefully someone will find this info useful. :) – CoolBots Mar 9 '17 at 15:51