Data AcQuisition And Real-Time AnalysisScope - Spectrum - Spectrogram - Signal Generator
Software for Windows
Science with your Sound Card!
Contact us about
HMS Timer / Stopwatch / Alarm Mini-App
The HMS_Timer_Stopwatch macro is one of the Macro Examples and Mini-Apps included with Daqarta. It offers 3 different modes of operation:
Stopwatch mode counts up in hours, minutes, seconds, and milliseconds until you hit a key to stop.
Time-of-day mode allows you to set alarm time, then it counts down hours, minutes, and seconds remaining, and gives an audio alarm.
Duration mode behaves like a kitchen timer: You enter the amount of time to wait, then it counts down and gives the alarm.
To run this macro, hit the F8 key followed by the 'H' key.
Simple usage instructions are shown in a message. You can right-click the message to open this Help topic for more details.
This macro demonstrates how to wait for key entry, use time functions, display a time countdown, and create a simple audible alarm. It also shows how to get user data from a Field instead of the more elaborate Custom Controls dialog used in many of the other macro mini-apps.
When HMS_Timer_Stopwatch starts it sets Label1 (under the main Daqarta Notes area) to 'Time / Duration', indicating that Field1 directly below it is where you will enter the stop time or the duration. (You can ignore this field for stopwatch use.)
A message is displayed with brief usage instructions, and mentions that you can right-click the message to open this Help topic:
STOPWATCH: Hit 's' to start. Any character or space to stop. ALARM TIMER: Enter stop Time or Duration in seconds or HH:MM:SS into designated field below Notes. Time may be HH:MMa or HH:MMp. To start, hit: 't' = stop time 'd' = duration Right-click this message for Help.
You must hit lowercase letters, as indicated.
If you hit 's' to start the Stopwatch, the above message will be replaced by a large-font Custom Meter which will start counting up in HH:MM:SS.nnn format. As a reminder, its title is STOPWATCH: Hit any character or space to stop.
After you stop the stopwatch, the macro will exit but the meter will continue to show the total time until you cancel it with the [x] in the title bar.
If you hit 't' to start Time mode, the display counts down the total seconds remaining, using HH:MM:SS format. The title is TIME TO ALARM at 12:34:56.000 (or whatever time you entered into Field1 under the Time / Duration label). This time is always shown in 24 hour mode, regardless of how you entered it. (See below.)
Time entries are assumed to be local time, not UTC.
Note that if you enter time using a single colon, the entry will be treated as MM:SS unless you follow it with an 'a' or 'p', as in 12:34p. (You can use 'A' or 'P' instead, if you wish.) Since the value will then be treated as HH:MM, you can't include a "fractional minutes" decimal.
To use 24-hour time, you must enter in HH:MM:SS mode, with decimal seconds if desired.
Entered stop times are assumed to be for the same day, unless that time has already passed; in that case, the next day is assumed. If you enter 24:00:00 or 00:00:00 (midnight) and the current local time is 9:50 AM, the countdown will start at 14:10:00 (14 hours and 10 minutes until midnight). If you enter 9:00a (which has already passed) the countdown will start at 23:10:00 since the target is 9 hours past midnight.
You can force times for the following day (or days) by adding 24 hours per day to the hours entry (it can go beyond the 2 digits implied by "HH"). For example, if it is 9:50 AM and you want the alarm at 10:00 AM tomorrow, you would enter 34:00:00 instead of 10:00:00 or 10:00a.
When the countdown reaches zero, the meter display changes to Done! and the alarm sounds for 2 seconds, after which the meter is removed and the macro exits.
If you hit 'd' to start Duration mode, the display likewise counts down the total seconds remaining, using HH:MM:SS format just as for Time mode. The title, however, shows the requested number of seconds as a single integer and fraction: TIME TO ALARM after 123.456 secs (or whatever value you entered into Field1 under the Time / Duration label). This is true even if the entry was in HH:MM:SS format.
As for Time mode, 12:34 will be treated as 12 minutes and 34 seconds. If you want 12 hours and 34 minutes, you can use 12:34a or 12:34:00. The 'HH:MMa' form works because any Daqarta time entry with that format is automatically returned as seconds since midnight.
Also as for Time mode, when the countdown reaches zero the meter display changes to Done! and the alarm sounds for 2 seconds, after which the meter is removed and the macro exits.
The alarm uses Daqarta's Synth functions. It sets the Synth to Burst mode before toggling it on, but it otherwise uses the existing instrument Voice and Note, as well as the Burst On and Off times. These default to Ocarina (a fairly clean sine wave) at 1975.533 Hz (Note 95 = B6), On for 110 msec and Off for 550 msec.
You can experiment with these to get the sound you want. Hit CTRL+N to open the Synth Control dialog. You can toggle the Synth on manually and leave it on while you adjust the controls. The default values are rather piercing; you can reduce the volume with the Master Volume slider (F9 key or toolbar speaker icon), or with the Synth Velocity control.
The default Note frequency is around that of maximum sensitivity of the typical human ear. It also reproduces well on small laptop speakers. If you move this too much lower (or higher), it may be more pleasing but less audible.
If you will always be running the timer macro on the same system, and you don't use these Synth controls otherwise, you can just manually set them as desired and they will remain that way across sessions. Alternatively, you can explicitly set these in the timer macro just before the SynthOn=1 command.
In fact, you can make the alarm much more elaborate, as well as changing its overall duration. For example, instead of just shutting it off after WaitSecs=2, you can modify the settings and give another WaitSecs command... for as many changes as you want.
After setting the Time / Duration label for Field1 and displaying the instruction message, the macro enters a WHILE loop to wait for an 's', 't', or 'd' key. Other keys (and any keys used during value entry or editing) are ignored.
The loop uses WaitKey=255 in the WHILE loop to halt macro execution until any key is hit, which is then read via UK=Key?# and tested. The WaitKey isn't absolutely needed, but it is a good idea. Without it, the WHILE loop is constantly (and wastefully) testing the key value whether it has changed or not. That "wheel spinning" probably won't be noticed on most systems, under most conditions, but WaitKey is much more efficient.
When any of the three key options is found a LoopBreak=2 command exits the WHILE loop
Next, an IF.UK="s" tests for Stopwatch mode. If so, the start time is read via T=Time?s, and another WHILE loop is entered that waits for any keypress. On every pass through the loop, the elapsed time is computed via M=Time?s - T (current time minus start time) and displayed via Mtr0=M(T.3). The current key value, if any is then read via UK=Key?#, after which WEND. returns to the WHILE.UK=0 to see if any key has been hit to stop the Stopwatch.
Alternatively, if the initial IF.UK="s" fails then it must be a 't' or 'd' key. The ELSE. branch uses D=Field1 to get the Time / Duration value (in seconds), then uses IF.UK="t" to see if it is Time mode. If so, it sets the Mtr0 title to show the specified time via Mtr0="<<TIME TO ALARM at "+D(T), where D(T) shows the value of D as HH:MM:SS.nnn. Then D=D - Time?s subtracts the current time from the alarm time to get the time remaining, which will be counted down. However, if this is negative it means that time is already passed so 24 hours (in seconds) is added back to it.
Otherwise, if it was not a 't' key it must have been a 'd' for Duration mode, so the initial D=Field1 seconds count is used as-is. The display title shows this via Mtr0="<<TIME TO ALARM after "+D(0.3)+" secs"
In either case, the actual countdown begins with variable D holding the remaining seconds. T=D + Time?s find the stop time by adding the current time to the remaining seconds.
Then WHILE.(T-Time?s)=>0 starts a loop that runs until the current time is equal to or greater than the stop time. The remaining time is shown as HH:MM:SS with Mtr0=D(T.0). Then the new time remaining is computed with Q=T-Time?s and the integer portion is found with D=int(Q). Then WaitSecs=Q-D waits for the next integer second. This scheme avoids cumulative errors that might arise by simply using WaitSecs=1 in a loop for the original D seconds remaining.
When the WHILE finally fails, the display changes to Done! and the alarm is sounded. As noted under Alarm Settings, above, you can edit this as desired. You can easily change the WaitSecs=2 line to get a longer or shorter alarm.
;<Help=H490F Label1="Time / Duration" ;Label for Field1 Msg="<<Stopwatch / Alarm Timer" ;Title for Msg Msg="STOPWATCH:" _ ;Instruction message +n +"Hit 's' to start." _ +n +"Any character or space to stop." _ +n +n+ "ALARM TIMER:" _ +n +"Enter stop Time or Duration" _ +n +"in seconds or HH:MM:SS" _ +n +"into designated field below Notes." _ +n +"Time may be HH:MMa or HH:MMp." _ +n +"To start, hit:" _ +n +"'t' = stop time" _ +n +"'d' = duration" _ +n +n +"Right-click this message for Help." Mtr0="<<STOPWATCH:" _ ;Meter title for Stopwatch +" Hit any character or space to stop." UK=0 ;Default key code WHILE.UK=0 ;Loop until key found WaitKey=255 ;Wait for key press UK=Key?# ;Read the key IF.UK="s" ;If this is 's' LoopBreak=2 ; then exit the loop ENDIF. IF.UK="t" ;Same for 't' or 'd' LoopBreak=2 ENDIF. IF.UK="d" LoopBreak=2 ENDIF. UK=0 ;If any other, keep waiting WEND. IF.UK="s" ;If 's', start Stopwatch mode Msg= ;Clear instruction Msg T=Time?s ;T = current local time UK=0 WHILE.UK=0 ;Loop until any key hit M=Time?s - T ;Elapsed time = current - start Mtr0=M(T.3) ;Display as HH:MM:SS.nnn UK=Key?# ;Key hit, if any WEND. ELSE. ;Else alarm timer 't' or 'd' D=Field1 ;D = requested Time or Duration IF.UK="t" ;Is this Time mode? Mtr0="<<TIME TO ALARM at "+D(T) ;Mtr0 title D=D - Time?s ;Less current = time left IF.D=<0 ;If negative, assume tomorrow D=D + 24 * 3600 ;Add 24 hours ENDIF. ELSE. ;Else show Duration meter title Mtr0="<<TIME TO ALARM after "+D(0.3)+" secs" ENDIF. Msg= ;Clear instruction Msg T=D + Time?s ;Stop time = current + duration WHILE.(T-Time?s)=>0 ;While time remaining Mtr0=D(T.0) ;Display as HH:MM:SS Q=T-Time?s ;Time remaining D=int(Q) ;Integer secs remaining WaitSecs=Q-D ;Wait for next second WEND. Mtr0="Done!" ;Visual confirmation SynthBurst=1 ;Set synth burst mode SynthOn=1 ;Toggle synth on WaitSecs=2 ;Wait for 2 seconds SynthOn=0 ;Toggle synth off Mtr0= ;Remove Mtr0 after alarm ENDIF. Mtr0="<<" ;Clear Mtr0 title Msg="<<" ;Clear Msg title
See also Macro Examples and Mini-Apps
Questions? Comments? Contact us!We respond to ALL inquiries, typically within 24 hrs.
Over 30 Years of Innovative Instrumentation
© Copyright 2007 - 2017 by Interstellar Research
All rights reserved