NXC: start/stop timer?
NXC: start/stop timer?
hi,
I can't find anything about the keyword "timer":
how to start / stop / reset timers?
how many different timers can I handle in 1 program?
(the somewhat strange-sounding keyword CurrentTick I could find, but nothing else, unfortunately) :(
I can't find anything about the keyword "timer":
how to start / stop / reset timers?
how many different timers can I handle in 1 program?
(the somewhat strange-sounding keyword CurrentTick I could find, but nothing else, unfortunately) :(
Re: NXC: start/stop timer?
As far as I can see you only get FirstTick, which is the value when the program began running and CurrentTick which is the present value. I think you have no control over the system timer and can only read it.
The only 'timer' you get is the wait statement.
The only other one I have seen so far is the sleep timer.
The only 'timer' you get is the wait statement.
The only other one I have seen so far is the sleep timer.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC: start/stop timer?
Because you can know the Tick of when the program starts, as well as the current Tick, you can calculate almost any time/timer value from that. You could even "reset" it, by simply subtracting all future readings by the current tick (as it is at that one point).
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC: start/stop timer?
hgt, matt,
thanks! I think I falsly recalled some old stuff of the old RCX times...
@afanofosc:
Why didn't you call the Timer Timer but Tick instead? Wouldn't it have been better to use the C keywords, e.g. Timer(0) ?
thanks! I think I falsly recalled some old stuff of the old RCX times...
@afanofosc:
Why didn't you call the Timer Timer but Tick instead? Wouldn't it have been better to use the C keywords, e.g. Timer(0) ?
Re: NXC: start/stop timer?
ps
any smart proposals how to easily handle 8 different timers in a program?
Sth like 8 independent different stop watchs:
StartTimer[n]
SetTimer[n]
HoldTimer[n]
ResetTimer[n]
(it's been hard day... )
any smart proposals how to easily handle 8 different timers in a program?
Sth like 8 independent different stop watchs:
StartTimer[n]
SetTimer[n]
HoldTimer[n]
ResetTimer[n]
(it's been hard day... )
Re: NXC: start/stop timer?
For Doc, master of calculating FFTs and Gamma Loophole Phi Uber Functions in his feet (his head is not worthy of such pathetically easy stuff), but is too busy finding THE Question to bother himself with such a simple program:
Just fix this up. (There may be bugs, though I doubt it.)
Code: Select all
struct T
{
bool stopped;
unsigned long strt;
unsigned long lastStop;
unsigned long total;
} t;
unsigned long T_get()
{
if(t.stopped)
return(t.total);
return(CurrentTick() - t.lastStop + t.total);
}
void T_stop()
{
if(!(t.stopped))
{
t.total = T_get();
t.stopped = true;
}
}
void T_start()
{
if(t.stopped)
{
t.stopped = false;
t.lastStop = CurrentTick();
}
}
void T_set(unsigned long time)
{
t.total = time;
t.lastStop = CurrentTick();
}
void T_init()
{
t.stopped = false;
t.strt = CurrentTick();
t.lastStop = CurrentTick();
t.total = 0;
}
void T_reset()
{
T_init();
}
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXC: start/stop timer?
Muntoo offers some great code. Alternatively you can simply use 8 global unsigned long variables. Store CurrentTick into which ever one you want to "clear" and then its value becomes the difference between CurrentTick() and its current value. You can wrap that in functions if you like or just do the math inline. Macros could also be used to hide the trivial amount of code that clears or reads a timer value.
John Hansen
Code: Select all
unsigned long __timer_global_0, __timer_global_1, __timer_global_2, __timer_global_3;
unsigned long __timer_global_4, __timer_global_5, __timer_global_6, __timer_global_7;
#define CLEAR_TIMER(_n) asm { \
compchk EQ, isconst(_n), TRUE \
compchk LT, _n, 8 \
compchk GTEQ, _n, 0 \
gettick __timer_global_##_n \
}
#define READ_TIMER(_n) asm { \
compchk EQ, isconst(_n), TRUE \
compchk LT, _n, 8 \
compchk GTEQ, _n, 0 \
gettick __URETVAL__ \
sub __URETVAL__, __URETVAL__, __timer_global_##_n \
}
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC: start/stop timer?
muntoo, John, thank you very much! Exactly what I needed! :)
And muntoo: yes, you're oh so right (making fun of me)
(it's really been a hard day's night... ) ;)
And muntoo: yes, you're oh so right (making fun of me)
(it's really been a hard day's night... ) ;)
Who is online
Users browsing this forum: No registered users and 3 guests