View unanswered posts | View active topics It is currently Sun Jan 21, 2018 2:49 pm






Reply to topic  [ 5 posts ] 
How to stop something after some time no matter what? 
Author Message
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
Post 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! :)


Wed Feb 04, 2015 12:29 am
Profile
Guru
User avatar

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


Wed Feb 04, 2015 1:51 am
Profile
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
Post 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.

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


Wed Feb 04, 2015 8:24 pm
Profile
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 16
Post 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;


Wed Feb 04, 2015 8:36 pm
Profile
Guru
User avatar

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


Wed Feb 04, 2015 10:12 pm
Profile
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.