Hour, minute, second timer

Recommended Posts


I am new to Daqfactory and fairly new to labjack (T7) which is being used.  I am struggling to set up a visual timer that can be started and stopped with a button and will visually display time lapsed time in hours, minutes and seconds.  Ultimately I also want data logging to be stopped and started at the same time.

Any guidance or examples on how this is best achieved would be amazing!



Link to comment
Share on other sites

There are a number of ways to do this.  Here is one:

1) create a start button.  

2) in the Start Button, select a Quick Sequence Action, with code like this:

global expStartTime = systime()
global expStopTime = 0
beginLogging(myLog) // to start logging.  Replace myLog with the name of your logging set.

3) create a Stop button, select a Quick Sequence Action with code like this:
expStopTime = systime()
endLogging(myLog)  // again, replace myLog with the name of your logging set

4) create a Variable Value component.  The expression would be:

formatDateTime("%H:%M:%S", iif (expStopTime, expStopTime - expStartTime, systime() - expStartTime))

That is it.  The script in the start button initializes two variables holding the start and stop time of the experiment.  Stop time is set to 0 while the experiment is running since it isn't over yet.  The stop button then sets the stop time to the current system time.

The expression in the variable value component has two parts.  The first, formatDateTime() takes a time stamp and displays it in human readable format based on specifiers.  In this case %H is hours, %M is minutes, and %S is seconds.  Note that this will only work for up to 24 hours.  If you need longer you'll need the specifier for days, which is %j.  That will work up to 1 year.

The second part is iif() which stands for inline if.  It has three parameters.  If the first parameter is true (i.e. not 0), then the function returns the second parameter, otherwise it returns the third parameter.  In this case, we are looking to see if the stop time is 0 or not.  If it is not 0 then the experiment is done and we return the difference between the stop and start times, which is the final elapsed time.  If it is 0, then the experiment is running, so we display the difference between the current system time and the start time.  This updates each time the screen is redrawn.

Link to comment
Share on other sites

Legend! that's bang on.

+ Good work on DAQ Factory... Great product :)


FYI - For reference for anyone in the forum looking to do a similar thing - Have added some bits to name the data logging file + added an extra timer to display total test time:



In a button quick sequence:

// Starts Timer and starts Data logging

if (Timer2Running != 1)  
  global expStartTime2 = systime() // Total Test Timer
  global expStopTime2 = 0
  global Timer2Running = 1

global expStartTime1 = systime() // Test Phase Timer
global expStopTime1 = 0

if (logging.Data1.running) 
   logging.Data1.strFileName = "file path" + formatdatetime("%y_%m_%d@%H_%M",systime()) + ".csv" //add folder address into file path where ya want it saved.

 logging.Data1.strFileName = "C:\Users\rs\Desktop\Calirometer Room\Test Data\CM1_" + formatdatetime("%y_%m_%d@%H_%M",systime()) + ".csv" 


Stop timer button quick sequence:

//Stops Timer & Data Logging Data1

expStopTime1 = systime()


Second stop timer button quick sequence:

//Stops Timer & Data Logging Data1

expStopTime2 = systime()

Timer2Running = 0 // resets bit to allowed timer to be started

Link to comment
Share on other sites

  • 5 months later...

Hello Guru,

This is very useful and works fine.

Suppose that I want to use a test timer that can show the test time in hours, minutes and seconds, but with the possibility to show the hours up to a large number (e.g. 1000 hours).

How could this be achieved? 

Link to comment
Share on other sites

Given a variable deltaT in seconds, to format it as H:M:S where H could be > 24 you would do:

doubleToStr(floor(deltaT / 3600)) + formatDateTime(":%M:%S",deltat)

If H was always < 24 you could just use the FormatTime() function, but that function rolls over at the day.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.