View unanswered posts | View active topics It is currently Thu Jul 24, 2014 10:28 pm






Reply to topic  [ 11 posts ] 
Mindstorms color sensor using RobotC 
Author Message
Rookie

Joined: Tue May 22, 2012 7:47 am
Posts: 5
Post Mindstorms color sensor using RobotC
I used the below code to detect whether or not a ball is red blue green etc. But everytime i run it on the nxt block it continues to show this "???". I need help, i dont see were the code could have gone wrong.

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

task main()
{
string sColor;

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


Tue May 22, 2012 7:52 am
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: Mindstorms color sensor using RobotC
You're using the LEGO Color Sensor, not HiTechnic, right? If so, then your values are all wrong. Here:
Code:
#pragma config(Sensor, S1,     colorport,      sensorCOLORFULL)

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);
  }
}


Also, you never defined "colorport". The line at the top does that. Replace the "S1" in it with whichever port your sensor is plugged into. i.e. S2 for port two, S3 for port three, etc.

_________________
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.


Tue May 22, 2012 8:45 am
Profile WWW
Rookie

Joined: Tue May 22, 2012 7:47 am
Posts: 5
Post Re: Mindstorms color sensor using RobotC
Thanks, i am currently in a differend class right now but i will try it out tomorrow. My teacher did get the sensor from the hitechnic website.
I got this code from a moderator on a website called bot bench. heres the link to it.
http://botbench.com/blog/2011/02/16/rob ... ur-sensor/

-Kelso


Tue May 22, 2012 11:48 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3162
Location: Rotterdam, The Netherlands
Post Re: Mindstorms color sensor using RobotC
That code is for the LEGO Colour Sensor, not the HiTechnic Colour Sensor. You're going to want to check out HTCS2-test*.c examples that come with the driver suite. More info on the suite itself and how to get it working, etc, can be found on the same site you got this example from: http://botbench.com/blog/robotc-driver-suite/

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue May 22, 2012 12:06 pm
Profile WWW
Rookie

Joined: Tue May 22, 2012 7:47 am
Posts: 5
Post Re: Mindstorms color sensor using RobotC
Ok thanks, ill check it out.
-Kelso


Tue May 22, 2012 12:10 pm
Profile
Rookie

Joined: Tue May 22, 2012 7:47 am
Posts: 5
Post Re: Mindstorms color sensor using RobotC
Ok, the driver tests work great, but now i need to add motor commands. So when it senses a green it does one thing and when it senses a red it does another. how would i go about doing that with the sample code? A fellow classmate used code similar to the above code posted and included his motor commands inbetween the color and break section for each color, but since that is not in the sample code i dont know how id go about doing that.


Wed May 23, 2012 7:49 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3162
Location: Rotterdam, The Netherlands
Post Re: Mindstorms color sensor using RobotC
Looks like you have some learning to do! Go check out the curriculum on the robotc website and also check out the tutorial you can find here: http://botbench.com/blog/2009/05/14/robotc-tutorial/

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Wed May 23, 2012 8:51 am
Profile WWW
Rookie

Joined: Tue May 22, 2012 7:47 am
Posts: 5
Post Re: Mindstorms color sensor using RobotC
Ok, ill check that out, thanks for the help.


Thu May 24, 2012 7:20 am
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post Re: Mindstorms color sensor using RobotC
Hey Guys,
Sorry for interrupting the fun, but I have a burning question that has to be answered.
Can RobotC use the "Lamp" feature of the lego color sensor like NXT - G?

Thanks,
Neil

P.S.

I am new to the site
posting.php?mode=reply&f=1&t=4571#
posting.php?mode=reply&f=1&t=4571#

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sat Nov 10, 2012 12:27 am
Profile WWW
Rookie

Joined: Sat Nov 10, 2012 3:55 pm
Posts: 1
Post Re: Mindstorms color sensor using RobotC
roboRed wrote:
Hey Guys,
Sorry for interrupting the fun, but I have a burning question that has to be answered.
Can RobotC use the "Lamp" feature of the lego color sensor like NXT - G?

Thanks,
Neil


Check out the following blog at bot bench.com
At the very end of the comments Xander describes the code required to use lamp mode with the colour sensor.

http://botbench.com/blog/2011/02/16/robotc-using-the-nxt-2-0-colour-sensor/

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


BManCan


Sun Nov 11, 2012 3:42 pm
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post Re: Mindstorms color sensor using RobotC
Thanks, Man

_________________
string Robored = "Awesome"
~~Neil Balch~~


Sun Nov 11, 2012 6:12 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 11 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:  
cron



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