Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clock second hand jitters

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
I have an analog clock with a second hand that ticks nicely around but sometimes is erratic.

I have a reference timer set at 1000 ms. (sometimes it is better setting it to 990ms)

Any number from 0 to 59 will position the hand.

Some activies (particularly loading video) makes it sometimes miss a beat.

If I drive the hand position every second directly from Second(Time) it sometimes hesitates and catches up two seconds in the middle of the minute after the disturbance has passed.

If instead I have a counter to set the position of the hand that is reset to 0 every minute, it appears to tick evenly but if it has missed two during the minute, it jumps from 57 to 60, missing 58 and 59!

I considered having an AVI or animated GIF of a sweep hand repeating itself every minute but this is overkill and they sometimes jitter anyway!

Is there another way like using DirectX or something?
 
I'd set the timer interval to 500 (or even 250). WM_TIMER messages are special, low-priority messages (and they don't queue, so there can only ever be one WM_TIMER message pending for any application). This means that
a) the Timer event will be triggered at best at the interval time you have requested
b) if the system is busy, some events may be lost altogether

Reducing the interval time to a factor of the actual period you want helps offset this.

However, if you really want to mess about with this stuff I'd suggest the multimedia timers. Here's some example code. You'll need a form and a module. The form needs a text box and a couple of command buttons. Note that we are playing with a callbacks here, which we can't safely debug in the IDE (it'll crash VB), so be careful ...
Ok, theis goes in the module:
Code:
[blue]Option Explicit

Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public Const TIME_ONESHOT = 0  '  program timer for single event
Public Const TIME_PERIODIC = 1  '  program for continuous periodic event

Public Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    Form1.Text1.Text = Second(Now)
End Sub[/blue]
And the following goes in the form:
Code:
[blue]Option Explicit

Private TimerEventID As Long

Private Sub Command1_Click()
    TimerEventID = timeSetEvent(1000, 500, AddressOf TimeProc, 0, TIME_PERIODIC) ' every second, with half-second timer resolution
End Sub

Private Sub Command2_Click()
    timeKillEvent TimerEventID
    TimerEventID = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If TimerEventID Then timeKillEvent TimerEventID
End Sub[/blue]

Note that the multimedia timer event can be place a relatively heavy demand on system overheads depending on the resolution selected, which is why I opted for 500ms resolution.

There's also CreateTimerQueueTimer, but I'll leave you to investigate that for yourself
 
Thanks.
I had found that 490ms seems to give the best results.
I'm using this clock display in the same app. that I monitor a Webcam once per second and send high res frames out on the net so I want to put as low a load on everything as possible.
I notice when the frame speed of the camera slows to less that 1/15 sec with low light (on automatic exposure) the timer misses quite a few beats, even though the processor usage only peaks at 20% and all other network communications doesn't seem to be affected.

I have seen a clock that has a nice continuous sweep hand rather than ticking in seconds. I wonder if this really is a movie re-starting every minute? In which case how would I superimpose the minute and hour hands over a movie?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top