View unanswered posts | View active topics It is currently Mon Jul 28, 2014 6:06 pm






Reply to topic  [ 5 posts ] 
coding problem. 
Author Message
Rookie

Joined: Sun Feb 26, 2012 12:01 am
Posts: 2
Post coding problem.
hi, i am a beginner in programming. i have a problem and any help will be very much appreciated.
i am trying to program a color sensor where a variable is incremented whenever it detects the same color.
(example is if it detects a green, x will be incremented by one.)

my code is like this:
string sColor;
int x=0;

while(1)
{
switch (SensorValue[color])
{
case GREENCOLOR:
sColor = "Green";
x = x+1;
break;
default:
sColor = "???";
break;
}
nxtDisplayCenteredTextLine(2, sColor);
wait1Msec(50);
}

but i'm not getting the output that i want.
whenever it sees a green, x is always incrementing continuously.
i want it to only increase by 1 one at a time in every detection.
(i apologize for my grammar :] )
can anyone help me with this. thanks a lot. :)

jamekz~


Sun Mar 04, 2012 1:34 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: coding problem.
It is because the CPU is running really fast. It was detecting the same color object over and over again continuously. If you want to count color objects, you need to detect transitions instead. For example, you only count it when you previously don't see that color, then you see the color. Once you see the color and incremented one, you don't count until the color changed to something else before you detect that color again.
Code:
string sColor;
int x=0;
int prevColor = BLACKCOLOR;
int currColor;

while(1)
{
    currColor = SensorValue[color];
    if (currColor != prevColor)
    {
        switch (currColor)
        {
            case GREENCOLOR:
                sColor = "Green";
                x++;
                break;
            default:
                sColor = "???";
                break;
        }
        nxtDisplayCenteredTextLine(2, sColor);
        prevColor = currColor;
    }
    wait1Msec(50);
}


Sun Mar 04, 2012 3:45 am
Profile
Rookie

Joined: Sun Feb 26, 2012 12:01 am
Posts: 2
Post Re: coding problem.
thanks for the reply. it now worked just as expected.
now I am having fun playing/programming with my nxt. :)

I'm kinda curious, is it possible to send the value of the color to a mobile?..
I kinda want to experiment my lego and do a bunch of things.
if it's possible, can I program it using robotc?..
I read some of the topics here but all I get is controlling the nxt from a mobile.
how about doing the opposite which is to send a data to a mobile from the nxt.

thanks for the advice. :)
this will be a great start for my career.

jamekz~


Mon Mar 05, 2012 9:51 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: coding problem.
In theory, you can do this using Bluetooth communication. I've not done any Bluetooth programming so can't help you there but there are a lot of discussing threads on similar topics. You may want to search for them.


Mon Mar 05, 2012 2:10 pm
Profile
Novice
User avatar

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Post Re: coding problem.
jamekz wrote:
I'm kinda curious, is it possible to send the value of the color to a mobile?..
I kinda want to experiment my lego and do a bunch of things.
if it's possible, can I program it using robotc?..
I read some of the topics here but all I get is controlling the nxt from a mobile.
how about doing the opposite which is to send a data to a mobile from the nxt.

Bluetooth would be the way to go. However, you will need to find or create an app for your phone that will display the Bluetooth message.
//Andrew

_________________
Check out my website! www.RoboDesigners.com

VRC Team 2190

Twitter: @RoboDesigners


Tue Mar 06, 2012 11:34 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 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.