ROBOTC.net forums
http://www.robotc.net/forums/

Timing
http://www.robotc.net/forums/viewtopic.php?f=52&t=5452
Page 1 of 1

Author:  KuatYards [ Mon Feb 04, 2013 3:35 pm ]
Post subject:  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

Author:  JohnWatson [ Mon Feb 04, 2013 5:24 pm ]
Post subject:  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).

Author:  KuatYards [ Wed Feb 06, 2013 3:32 pm ]
Post subject:  Re: Timing

Ah, :facepalm: of course

Author:  mightor [ Wed Feb 06, 2013 3:44 pm ]
Post subject:  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

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