View unanswered posts | View active topics It is currently Thu Oct 23, 2014 3:14 pm






Reply to topic  [ 4 posts ] 
Looping problem 
Author Message
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 12
Post 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!

task main()
{
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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 12
Post 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
Profile
Rookie

Joined: Fri Apr 26, 2013 11:05 am
Posts: 12
Post 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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.