View unanswered posts | View active topics It is currently Fri Jan 24, 2020 12:44 am

 Page 1 of 1 [ 1 post ]
 Print view Previous topic | Next topic
Problems with while statements for turning?
Author Message
Rookie

Joined: Thu Aug 20, 2009 5:12 pm
Posts: 1
Problems with while statements for turning?
Hey guys,

Currently building a robot that can navigate around a course to rescue a can (this is for a robotics competition). I have chosen RobotC over NXT-G, and I must say it is better software. However, I'm having a few issues...

1) I want the robot to follow a black line (it does this fine) however when it reaches an intersection, there will be a green square. I wish to have the robot turn the direction it sees the square. The one exception of this is where it sees black on both sensors, which means I want it to go straight forward.

I have come up with the following code*:

*Note: S1 is the left light sensor, S2 the right light sensor, motorA the left motor, motorB the right motor.
greenleft/greenrightmax are the maximum readings for green. As black is darker, I use if less than or equal to statements.

 Code:while(SensorValue(S1) <= greenleftmax && SensorValue(S2) <= greenrightmax)    // Both are seeing green/black      {         turning = 1; // Prevent normal line following         if(turning == 1)         {         leftdifference = greenleftmax - SensorValue(S1);         rightdifference = greenrightmax - SensorValue(S2);         turning = 2;        }         if(leftdifference > rightdifference  && turning != 0) // Green is right, turn right         {         nxtDisplayTextLine(1, "Leftd: %d", turning);         nxtDisplayTextLine(2, "Leftd: %d", leftdifference);         nxtDisplayTextLine(3, "Rightd: %d", rightdifference);        motor[motorA] = 25;        motor[motorB] = -20;        wait1Msec(1000);        motor[motorA] = 20;        motor[motorB] = 20;        turning = 0;        }         if(rightdifference > leftdifference && turning != 0) // Green is left, turn left         {         nxtDisplayTextLine(2, "Leftd: %d", leftdifference);         nxtDisplayTextLine(3, "Rightd: %d", rightdifference);         motor[motorA] = -20;        motor[motorB] = 25;        wait1Msec(1000);        motor[motorA] = 20;        motor[motorB] = 20;         turning = 0;        }         if(rightdifference >= 5 && leftdifference >= 5 && turning != 0)// Both are black, go straight         {         motor[motorA] = 20;        motor[motorB] = 20;        turning = 0;        }      }

Normal line following looks like this:
 Code:   while(SensorValue(S1) > greenleftmax && SensorValue(S2) > greenrightmax && turning == 0) //if both see white      {    nxtDisplayCenteredTextLine(2, "Both WHITE");    nxtDisplayCenteredTextLine(2, "Both WHITE");    motor[motorA] = 20;    motor[motorB] = 20;      }   while(SensorValue(S1) <= greenleftmin && SensorValue(S2) > greenrightmax && turning == 0)  //if left sees black         {         nxtDisplayCenteredTextLine(2, "Left BLACK");        nxtDisplayCenteredTextLine(4, "Right WHITE");        motor[motorA] = -25;        motor[motorB] = 20;        }  while(SensorValue(S2) <= greenrightmin && SensorValue(S1) > greenleftmax && turning == 0)  //if right sees black      {    nxtDisplayCenteredTextLine(2, "Left WHITE");    nxtDisplayCenteredTextLine(4, "Right BLACK");     motor[motorA] = 20;     motor[motorB] = -25;      }

The problem is when the robot comes up to an intersection, it just starts spinning around (as though executing motor[motorA] = -20; motor[motorB] = 25;) forever. It also doesn't go straight when hitting two black lines; it spins around in a circle. Hopefully this logic will complete the course (can't find how to do it properly anywhere, and have no mentor for this). I would love to do well in this competition and get others to use RobotC rather than NXT-G (which everyone seems to use).

Here is what I am working with:

Example video:

Any help with these issues would be greatly appreciated.

Thank you.

Thu Aug 20, 2009 5:33 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 1 post ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning