So you just bought yourself one of the NXT 2.0 Colour Sensors and you’re keen to get started with it. You browse through the example programs that are shipped with ROBOTC and open the “ColorSensor.c” program. “300 lines of code to read a simple colour?” you ask yourself.
Fear not, it’s a lot easier than that. In order to use the NXT 2.0 colour sensor you’ll need to make sure of one thing: you absolutely need at least ROBOTC 2.26.1 for NXT, anything less simply won’t work. Don’t worry, though, it’s a very stable beta. Don’t let that word scare you off, I’ve been using it for months now and it’s never failed me.
The NXT 2.0 Colour Sensor is super easy to use. Here’s a very simple program that will display the currently detected colour on the screen. Please note that this is basically a stripped down version of the one shipped with ROBOTC; I just removed all of the diagnostics crud that the developers added so they could debug their drivers.
#pragma config(Sensor, S1, colorPort, sensorCOLORFULL) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { string sColor; while (true) { switch (SensorValue[colorPort]) { case BLACKCOLOR: sColor = "Black"; break; case BLUECOLOR: sColor = "Blue"; break; case GREENCOLOR: sColor = "Green"; break; case YELLOWCOLOR: sColor = "Yellow"; break; case REDCOLOR: sColor = "Red"; break; case WHITECOLOR: sColor = "White"; break; default: sColor = "???"; break; } nxtDisplayCenteredTextLine(2, sColor); wait1Msec(50); } }
As you can see, not a whole lot to it. Have fun and enjoy your new sensor!
Um, I was just wondering what colors go to what numbers. I mean that in the program, it uses numbers for the colors (1-6 I think). I wish to know what numbers directly relate to what colors? Thanks…
This is from the ROBOTC includes:
typedef enum
{
BLACKCOLOR = 1,
BLUECOLOR = 2,
GREENCOLOR = 3,
YELLOWCOLOR = 4,
REDCOLOR = 5,
WHITECOLOR = 6
} TColors;
Hi, I was wondering if it is possible to use the color sensor value as a condition for a while loop.. I’ve tried it and the == and the != statements give me errors.. thanks in advance
It would help if you gave me a little more to go on. Can you paste a short snippet of the code?
Ok, so i tried this code in robotc and when i ran it the screen showed three question marks ??? like that. My color sensor is put in the right port but when i put a colored ball in front of it nothing happens, the screen stays the same. Why might this be happening, and how might i be able to fix it?
Did you try it with other coloured objects? The best to use is a non-shiny surface. Also, which version of ROBOTC are you using?
I,ve tried colored lego blocks as well as painted balls, the balls are not shiny. I am using ROBOTC 3.08.
It could be that the sensor is flooded by ambient light. Do you have fluorescent lights or bright sunlight in the room?
Im in a classroom and the lights are high up, they are tube lights. I’m not sure what they are called.
Y you no help
There is not an awful lot I can do for you, unfortunately. The sensor either works or it does not. I have mixed results with this sensor. It is most likely not the code you have, it’s just a flaky sensor. Did you try it in a different room with different lights?
yes, i ruduced the amount of light allowed to get to the light.
hi
i have lego mindstorms nxt with color sensor
i want the color sensor to detect the black line inside the maze
i tried a lot but i can’t do it
can u help me,please.
I’m using robotC
Hi Fahd,
Did you try asking this question on the ROBOTC forums? It would be much better than asking here. Just go here: http://www.robotc.net/forums/index.php and sign up. There are lots of people there who can help you, including me 🙂
– Xander
ok MR. XANDER
thanks alot
Hi, I know this post has some time, but does anybody knows if it is possible to obtain the RGB values of each of the colors.
Thanks.
Hi again.
I am using the lego color sensor, when I call the getColorSensorData(color, colorRaw, &out); method, every position on the array out[0], out[1] …, out[3]. Every value is 0. can you guys please help?
Thanks.
Can robotc use the “LAMP” feature of the lego color sensor?
Sure it can! In my case the sensor is connected to the S3 port, so you may need to change that for your NXT.
#pragma config(Sensor, S3, LCOLOUR, sensorCOLORFULL)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
// Blinky
task main()
{
while (true)
{
SetSensorType(LCOLOUR, sensorCOLORBLUE);
wait1Msec(300);
SetSensorType(LCOLOUR, sensorCOLORRED);
wait1Msec(300);
SetSensorType(LCOLOUR, sensorCOLORGREEN);
wait1Msec(300);
SetSensorType(LCOLOUR, sensorCOLORFULL);
wait1Msec(300);
}
}
Thanks!
Hey…..We got a RGB light sensor that isnt working and we dont know why. We tried to run it in a darker room like u said but the light to read the colors wont even come on and we dont know if it is just a bad sensor or if we just have the wrong program.
For are class we are trying to make the Bot follow a blue line through a maze. Can u help us figure out why it isnt working???
Thanks.
Hi there,
Could you repost your question on the Mindboard forums (https://sourceforge.net/apps/phpbb/mindboards) and attach your program to the post so I (and others) can see the program?
Thanks!
= Xander
Hi there, how to add two programs, one for detecting black color object and white objects, then for white objects it remove them and for black color objects it move away from them……help!!
You should consider posting that question on the Mindboards forums, which you can find here: https://sourceforge.net/apps/phpbb/mindboards/index.php