NXC: start/stop timer?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: start/stop timer?

Post by HaWe »

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) :(
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: NXC: start/stop timer?

Post by h-g-t »

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.
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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: start/stop timer?

Post by mattallen37 »

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 ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: start/stop timer?

Post by HaWe »

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) ?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: start/stop timer?

Post by HaWe »

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... ) :roll:
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: start/stop timer?

Post by muntoo »

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:

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();
}
Just fix this up. (There may be bugs, though I doubt it.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: start/stop timer?

Post by afanofosc »

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.

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 \
}

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: start/stop timer?

Post by HaWe »

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... ) ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests