View unanswered posts | View active topics It is currently Thu Sep 18, 2014 10:17 pm






Reply to topic  [ 4 posts ] 
Timing 
Author Message
Rookie

Joined: Mon Feb 04, 2013 3:23 pm
Posts: 2
Post Timing
I need help coding an autonomous program that runs for a set time. I think I need to use the Time1 but it's not working, the loop is infinate, here's the code:

int runTime = 1000;

int curRunTime = Time1[T1];
clearTimer(T1);

while (curRunTime < runTime) {
/*do code here/*
}

the problem is that the loop never ends


Mon Feb 04, 2013 3:35 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 578
Post Re: Timing
KuatYards wrote:
I need help coding an autonomous program that runs for a set time. I think I need to use the Time1 but it's not working, the loop is infinate, here's the code:

int runTime = 1000;

int curRunTime = Time1[T1];
clearTimer(T1);

while (curRunTime < runTime) {
/*do code here/*
}

the problem is that the loop never ends


There are some programming logic errors that we can take a look at here. Let's walk through the program line by line:

Code:
int runTime = 1000;


Creates an integer variable called 'runTime' and sets its value to 1000. Everything looks good here.

Code:
int curRunTime = Time1[T1];


Creates an integer variable called curRunTime and sets its value the current value of the T1 timer. The problem is; what is the value of the T1 timer at this point? Whatever value T1 holds, that's that will be assigned to curRunTime.

Code:
clearTimer(T1);


Clears the T1 timer. Note, however, that curRunTime has already been assigned the value that timer T1 had in the previous line of code; unless you specifically set the value of the timer T1 to curRunTime again, it will hold whatever value is already assigned to it.

Code:
while (curRunTime < runTime) {
       /*do code here/*
}


Runs the loop while curRunTime is less than runTime (which is 1000). Since curRunTime is the original value of timer T1 (and is never assigned a new value), if the original value of timer T1 is less than 1000 the loop will run forever. Since the curRunTime variable is assigned timer T1's value early in the program (and the individual timers are reset each time the program is run), the value is most likely very small.

To solve this, you will need to first clear the timer value, then assign its value to curRunTime, and then make sure that you update curRunTime with timer T1's value inside of the while loop. We actually have a Video Trainer Curriculum video on this very topic you may want to look at (http://www.education.rec.ri.cmu.edu/pre ... index.html, under the Sensing -> Line Tracking -> Line Tracking (Timer) video). There is even a simpler way to do this program, which will become clear to you once you watch the vidao (the video will show you exactly how to program it).

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Feb 04, 2013 5:24 pm
Profile
Rookie

Joined: Mon Feb 04, 2013 3:23 pm
Posts: 2
Post Re: Timing
Ah, :facepalm: of course


Wed Feb 06, 2013 3:32 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Timing
If you're using the Driver Suite in your robot, you might want to take a look at the timer-test1.c program. It's an example for the timer library that I wrote a while back. It allows you to have an arbitrary number of timers without the need to use the T1-T4 timers. It's quite flexible.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Wed Feb 06, 2013 3:44 pm
Profile WWW
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.