View unanswered posts | View active topics It is currently Sat Oct 31, 2020 6:29 pm

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Looping problem
Author Message
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 39
Looping problem
I am having our students use three different sensors to navigate an obstacle course. They can do it anyway they wish, as long as they use all three sensors at least once. One pair of students are using the touch sensor first, followed by the light sensor (to track a curved line). Everything works fine until the end of the line, as the robot circles around looking for the line. So, I know it is stuck in the loop. I tried to have them insert the "break;" command in the "if-else" loop, but it doesn't seem to work. I also had them try adding a motor stop command set, but all this did was slow the line tracking down to a snails pace. I am pasting their code, with hopes that someone can help us out. Thank you!

{
while(SensorValue(touch) < 1)
{
motor(motorB) = -40;
motor(motorC) = -40;
}

while(SensorValue(sonar) > 60)
{
motor(motorB) = 40;
motor(motorC) = 40;
}

motor(motorB) = -40;
motor(motorC) = -40;
wait1Msec(700);

motor(motorB) = 40;
motor(motorC) = -40;
wait1Msec(700);

while(true)
{
if(SensorValue(light) > 61)
{
motor(motorB) = 0;
motor(motorC) = 40;
}

else
{
motor(motorB) = 40;
motor(motorC) = 0;
}

}

while(SensorValue(sonar) > 25)
{
motor(motorB) = 40;
motor(motorC) = 40;
}

}

Tue Apr 22, 2014 8:14 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Looping problem
Since you didn't describe the details of the behavior you are expecting from that code, I can only comment on my understanding on what the code is trying to do:
 Code:task main(){    //    // Keep backing up until the touch sensor hits something.    //    while(SensorValue(touch) < 1)    {        motor(motorB) = -40;        motor(motorC) = -40;    }    //    // Keep going forward until an obstacle is less than 60cm ahead    //    while(SensorValue(sonar) > 60)    {        motor(motorB) = 40;        motor(motorC) = 40;    }    //    // Go into reverse for 700msec.    //    motor(motorB) = -40;    motor(motorC) = -40;    wait1Msec(700);    //    // Turn for 700msec.    //    motor(motorB) = 40;    motor(motorC) = -40;    wait1Msec(700);    while(true)    {        //        // Try following the line.        //        if (SensorValue(light) > 61)        {            motor(motorB) = 0;            motor(motorC) = 40;        }        else        {            motor(motorB) = 40;            motor(motorC) = 0;        }    }    //    // Since the previous while-loop is an infinite loop, the following code is unreacheable.    //    while(SensorValue(sonar) > 25)    {        motor(motorB) = 40;        motor(motorC) = 40;    }}

Please describe what you expect the robot to do so we can help you to figure out how to correct it.

Wed Apr 23, 2014 2:18 am
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 39
Re: Looping problem
Sorry for not providing enough information.

After the robot reaches the end of the line, using the light sensor, we want the robot to go on to the next piece of code. There are more obstacles on this challenge, after the line and these students are planning on using their sonar sensor next.

Wed Apr 23, 2014 8:48 am
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 39
Re: Looping problem
I think I have figured out why we couldn't get out of the loop with the light sensor. We didn't have another sensor (sonar or touch) for the if-else statement to check against. Once I put a second sensor at the beginning of the if-else loop, which means it would only run if this condition was true, the program would stop and move on to the next command set.
Here is how this statement now looks:

while(SensorValue(sonar) > 20) This was the new line of code that we added.
{
if(SensorValue(light) > 61)
{
motor(motorB) = 0;
motor(motorC) = 40;
}

else
{
motor(motorB) = 40;
motor(motorC) = 0;
}

}

Wed Apr 23, 2014 4:38 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 4 posts ]

#### 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