View unanswered posts | View active topics It is currently Sun Sep 21, 2014 9:12 pm






Reply to topic  [ 6 posts ] 
using the color sensor 
Author Message
Rookie

Joined: Fri Feb 24, 2012 12:36 pm
Posts: 4
Post using the color sensor
Hi,

I'm a college student using Mindstorms 3.05 for an engineering class, and I'm trying to get my NXT to distinguish colors. I just picked up the 6-color sensor but have no idea how to configure it. I've been looking through forums but can't find the basic sample program on ROBOTC that people seem to keep mentioning. I just need a basic way for the sensor to distinguish between red, blue, green, yellow, black, and white. Any starting points or any other form of help is greatly appreciated :)


Fri Feb 24, 2012 12:39 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: using the color sensor
Is this what your looking for?

Code:
#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);
  }
}

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Fri Feb 24, 2012 12:43 pm
Profile WWW
Rookie

Joined: Fri Feb 24, 2012 12:36 pm
Posts: 4
Post Re: using the color sensor
That looks about right, where did you find this? And how do I get the top line starting with #pragma? When I go to Motors and Sensors setup, I don't have any color options. I just have No Sensor, Touch, Light Active, Light Inactive, Sound DB, Sound DBA, and SONAR. Is there a firmware problem since I don't have those color options or is there something I'm missing? When I run your code I run into a bunch of errors, too


Fri Feb 24, 2012 1:07 pm
Profile
Rookie

Joined: Fri Feb 24, 2012 12:36 pm
Posts: 4
Post Re: using the color sensor
I figured out how to get the top line, turns out I just had to switch to expert mode and my Lego color sensor showed up. Now I'm running into errors regarding the 'case statement not having a constant value' and another saying that the various COLOR variables are undefined and that 'short' is assumed. I'm a fairly new C user, I have some experience in Java and Matlab. Where should I be creating these variables?


Fri Feb 24, 2012 1:24 pm
Profile
Senior Roboticist
User avatar

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
Post Re: using the color sensor
It's in the sample programs. go to "file", then "open sample program", and go to the folder "LEGO Color Sensor". It's the only program in it if your using 3.05. Did you modify the program? You might want to try replacing the "COLOR" things with the numbers 1-6. Like this:
Code:
#pragma config(Sensor, S1,     colorPort,           sensorCOLORFULL)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  string sColor;

  while (true) {
    switch (SensorValue[colorPort])
    {
      case 1:    sColor = "Black";     break;
      case 2:     sColor = "Blue";      break;
      case 3:    sColor = "Green";     break;
      case 4:   sColor = "Yellow";    break;
      case 5:      sColor = "Red";       break;
      case 6:    sColor = "White";     break;
      default:            sColor = "???";       break;
    }
    nxtDisplayCenteredTextLine(2, sColor);
    wait1Msec(50);
  }
}


Both this and the original code work fine for me.

_________________
A.K.A. inxt-generation
Self-proclaimed genius, and future world dominator.
My Brickshelf Folder
"Don't they teach recreational mathematics anymore?" - The Tenth Doctor
Bow down to Nikola Tesla, King of the Geek Gods.


Fri Feb 24, 2012 1:37 pm
Profile WWW
Rookie

Joined: Fri Feb 24, 2012 12:36 pm
Posts: 4
Post Re: using the color sensor
Sounds good, I'll give that a try and see if I have any more issues. Thank you very much!


Thu Mar 01, 2012 1:20 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.