View unanswered posts | View active topics It is currently Sun Aug 19, 2018 10:54 pm

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
How to stop something after some time no matter what?
Author Message
Rookie

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

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

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

Wed Feb 04, 2015 8:24 pm
Rookie

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

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
Guru

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

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
Display posts from previous:  Sort by
 Page 1 of 1 [ 5 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