ROBOTC.net forumshttp://robotc.net/forums/ How to stop something after some time no matter what?http://robotc.net/forums/viewtopic.php?f=52&t=10456 Page 1 of 1

 Author: Cyks [ Wed Feb 04, 2015 12:29 am ] Post subject: How to stop something after some time no matter what? Hello, We need to stop some code after three seconds. We think using this time while loop will allow this to happen: clearTimer(T1); while(time1(T1) < 3000) { }Anything we put inside this while loop will stop after three seconds right? It doesn't seem to work for us. Here is our code: clearTimer(T1); while(time1(T1) < 3000) { while ((SensorValue(SonarSensor1) == 22) && (SensorValue(SonarSensor2) == 22)) { motor[motorC] = 0; motor[motorE] = 0; motor[motorD] = 0; motor[motorF] = 0; } while ((SensorValue(SonarSensor1) == 23) && (SensorValue(SonarSensor2) == 23)) { motor[motorC] = 0; motor[motorE] = 0; motor[motorD] = 0; motor[motorF] = 0; } while ((SensorValue(SonarSensor1) == 24) && (SensorValue(SonarSensor2) == 24)) { motor[motorC] = 0; motor[motorE] = 0; motor[motorD] = 0; motor[motorF] = 0; } if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) > 23)) { motor[motorC] = 20; motor[motorD] = 20; motor[motorE] = -20; motor[motorF] = -20; } else if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) == 23)) { motor[motorC] = 20; motor[motorD] = 20; motor[motorE] = -20; motor[motorF] = -20; } else if ((SensorValue(SonarSensor2) > 23) && (SensorValue(SonarSensor1) == 23)) { motor[motorC] = -20; motor[motorD] = -20; motor[motorE] = 20; motor[motorF] = 20; } } motor[motorC] = 0; motor[motorD] = 0; motor[motorE] = 0; motor[motorF] = 0;Are the sensor while loops inside of the time while loop making the time while loop not work? Like does it get stuck on the while ((SensorValue(SonarSensor1) == 22) && (SensorValue(SonarSensor2) == 22)) { motor[motorC] = 0; motor[motorE] = 0; motor[motorD] = 0; motor[motorF] = 0; }and kinda doesn't read the while(time1(T1) < 3000) while it is inside of the second loop? If not, how can we make the robot stop something after a time (three seconds)? Thank you for any help! Author: MHTS [ Wed Feb 04, 2015 1:51 am ] Post subject: Re: How to stop something after some time no matter what? Yep, while loops inside a while-loop usually doesn't do what you intend to do. Unfortunately, I couldn't figure out what you are trying to do. So I have no suggestion on how to fix it. It seems you are trying to use two ultrasonic sensors (left and right) to approach some sort of obstacle evenly. If so, the logic is completely wrong.

Author:  Cyks [ Wed Feb 04, 2015 8:24 pm ]
Post subject:  Re: How to stop something after some time no matter what?    MHTS wrote:Yep, while loops inside a while-loop usually doesn't do what you intend to do. Unfortunately, I couldn't figure out what you are trying to do. So I have no suggestion on how to fix it. It seems you are trying to use two ultrasonic sensors (left and right) to approach some sort of obstacle evenly. If so, the logic is completely wrong.    Well we have two ultrasonic sensors both on the side of our robot and it is used to align with a rolling goal more accurately. What we want to do is when the one closer to the front has a higher value then the robot moves back. And if the one closer to the back has a higher value then the robot moves forward. And if they are both equal then the robot should stop. We can't really figure out how to do this.>_< Do you have any suggestions? lol ;P

Author:  Cyks [ Wed Feb 04, 2015 8:36 pm ]
Post subject:  Re: How to stop something after some time no matter what?    Cyks wrote:    MHTS wrote:Yep, while loops inside a while-loop usually doesn't do what you intend to do. Unfortunately, I couldn't figure out what you are trying to do. So I have no suggestion on how to fix it. It seems you are trying to use two ultrasonic sensors (left and right) to approach some sort of obstacle evenly. If so, the logic is completely wrong.    Thanks for your help! Well we have two ultrasonic sensors both on the side of our robot and it is used to align with a rolling goal more accurately. What we want to do is when the one closer to the front has a higher value then the robot moves back. And if the one closer to the back has a higher value then the robot moves forward. And if they are both equal then the robot should stop. We can't really figure out how to do this.>_< Do you have any suggestions? lol ;P    Would something like this work?

clearTimer(T1);

while(time1(T1) < 3000)
{
if ((SensorValue(SonarSensor1)) == (SensorValue(SonarSensor2)))//if both sensors are equal to each other then stop
{
motor[motorC] = 0;
motor[motorE] = 0;
motor[motorD] = 0;
motor[motorF] = 0;
}

if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) > 23))//if both sensors are greater than 23 move back
{
motor[motorC] = 20;
motor[motorD] = 20;
motor[motorE] = -20;
motor[motorF] = -20;
}
else if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) == 23))//if sonar one is greater than 23 and sonar two is equal to 23 move back
{
motor[motorC] = 20;
motor[motorD] = 20;
motor[motorE] = -20;
motor[motorF] = -20;
}
else if ((SensorValue(SonarSensor2) > 23) && (SensorValue(SonarSensor1) == 23))//if sonar two is greater than 23 and sonar one is equal to 23 move forward
{
motor[motorC] = -20;
motor[motorD] = -20;
motor[motorE] = 20;
motor[motorF] = 20;
}
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;

Author:  MHTS [ Wed Feb 04, 2015 10:12 pm ]
Post subject:  Re: How to stop something after some time no matter what?    Cyks wrote:    Cyks wrote:    MHTS wrote:Yep, while loops inside a while-loop usually doesn't do what you intend to do. Unfortunately, I couldn't figure out what you are trying to do. So I have no suggestion on how to fix it. It seems you are trying to use two ultrasonic sensors (left and right) to approach some sort of obstacle evenly. If so, the logic is completely wrong.    Thanks for your help! Well we have two ultrasonic sensors both on the side of our robot and it is used to align with a rolling goal more accurately. What we want to do is when the one closer to the front has a higher value then the robot moves back. And if the one closer to the back has a higher value then the robot moves forward. And if they are both equal then the robot should stop. We can't really figure out how to do this.>_< Do you have any suggestions? lol ;P    Would something like this work?

clearTimer(T1);

while(time1(T1) < 3000)
{
if ((SensorValue(SonarSensor1)) == (SensorValue(SonarSensor2)))//if both sensors are equal to each other then stop
{
motor[motorC] = 0;
motor[motorE] = 0;
motor[motorD] = 0;
motor[motorF] = 0;
}

else if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) > 23))//if both sensors are greater than 23 move back
{
motor[motorC] = 20;
motor[motorD] = 20;
motor[motorE] = -20;
motor[motorF] = -20;
}
else if ((SensorValue(SonarSensor1) > 23) && (SensorValue(SonarSensor2) == 23))//if sonar one is greater than 23 and sonar two is equal to 23 move back
{
motor[motorC] = 20;
motor[motorD] = 20;
motor[motorE] = -20;
motor[motorF] = -20;
}
else if ((SensorValue(SonarSensor2) > 23) && (SensorValue(SonarSensor1) == 23))//if sonar two is greater than 23 and sonar one is equal to 23 move forward
{
motor[motorC] = -20;
motor[motorD] = -20;
motor[motorE] = 20;
motor[motorF] = 20;
}
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;    I would add an else to the second if (like corrected above). However, since your logic specifically compares for equality to the number 23, in real life, it is almost always not true. So I would not use this kind of logic. The range comparison you posted on the other thread may work better.

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/