Results 1 to 12 of 12

Thread: Stop Watch

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2000
    Location
    Johannesburg,South Africa
    Posts
    1

    Question

    Hy there people of the world.Please help me with ,What I thought was a going to be simple piece of code but I have become just a little STUCK!!!
    I am trying to make just a simple "Stop Watch" program where you can start a timer then stop it,and start the timer from where it was stoped.

    I have got the Stop Watch sort of working,it starts counting from 00:00:00,but when i stop the Stop Watch and restart it,it starts counting from the time a stoped it plus the time it was stoped for.

    Any ideas on how to fix it

    PS.I am new to Programing so i am a bit Stupid

    thanks
    Shaun Mannion

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Can you post your code here?
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752

    Cool May be this work

    Private Sub Command1_Click()
    If Command1.Caption = "Start" Then
    Command1.Caption = "Stop"
    Timer1.Enabled = True
    Else
    Command1.Caption = "Start"
    Timer1.Enabled = False
    End If
    End Sub

    Private Sub Timer1_Timer()
    Static hh As Integer, mm As Integer, ss As Integer, nn As Integer
    nn = nn + 1
    If nn = 100 Then
    ss = ss + 1
    nn = 0
    If ss = 60 Then
    ss = 0
    mm = mm + 1
    If mm = 60 Then
    mm = 0
    hh = hh + 1
    End If
    End If
    End If
    Label1.Caption = hh & ":" & mm & ":" & ss & ":" & nn
    End Sub

    Faisal
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  4. #4
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    I haven't really read your code but may be this may work?

    Make the variable that tracks the time global so that you can initialise it when you click start. This way it's available to the timer sub procedure and the command event procedure.

  5. #5
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Also the 'nn' should be microseconds not nanoseconds as the timer's intervals are in microseconds!!!

  6. #6
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    After reading about your thread I thought I'll have a go at writting a stop watch as I have never done one before and thought It could be intresting, hers my code:

    However it doesn't seem to be counting at the right pace if you know what I mean the seconds are to slow...any ideas?

    Code:
    Dim stopWatchHours As Integer
    Dim stopWatchMinutes As Integer
    Dim stopWatchSeconds As Integer
    Dim stopWatchMicroSecs As Integer
    
    Private Sub cmdStartStop_Click()
        If cmdStartStop.Caption = "Start" Then
            cmdStartStop.Caption = "Stop"
            'initialise variables
            stopWatchHours = 0
            stopWatchMinutes = 0
            stopWatchSeconds = 0
            stopWatchMicroSecs = 0
            'start the timer
            Timer1.Enabled = True
        Else
            cmdStartStop.Caption = "Start"
            'stop the timer
            Timer1.Enabled = False
        End If
    End Sub
    
    Private Sub Timer1_Timer()
        stopWatchMicroSecs = stopWatchMicroSecs + 1
        
        If stopWatchMicroSecs = 100 Then
            stopWatchSeconds = stopWatchSeconds + 1
            stopWatchMicroSecs = 0
        End If
        
        If stopWatchSeconds = 60 Then
            stopWatchMinutes = stopWatchMinutes + 1
            stopWatchSeconds = 0
        End If
        
        If stopWatchMinutes = 60 Then
            stopWatchHours = stopWatchHours + 1
            stopWatchMinutes = 0
        End If
        
        Label1.Caption = stopWatchHours & ":" & stopWatchMinutes & ":" & stopWatchSeconds & ":" & stopWatchMicroSecs
    End Sub

  7. #7
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752

    Cool

    The intilization of variables should be done in some other place.eg in Form Load
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    How abt mine?

    Code:
    'Put this code under a Form
    
    Private Sub CmdAction_Click(Index As Integer)
    Dim i As Integer
    Select Case Index
    Case 0 'Start
        sTime = Now
        lblStopWatch.Caption = "00:00:00"
        SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
        CmdAction(0).Enabled = False
        For i = 1 To 3: CmdAction(i).Enabled = True: Next
    Case 1 'Stop
        KillTimer Me.hwnd, 0
        CmdAction(0).Enabled = True
        For i = 1 To 3: CmdAction(i).Enabled = False: Next
    Case 2 'Reset
        sTime = Now
        lblStopWatch.Caption = "00:00:00"
    Case 3 'Pause
        If CmdAction(3).Caption = "Pause" Then
            pTime = Now
            KillTimer Me.hwnd, 0
            CmdAction(3).Caption = "Resume"
        Else
            SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
            sTime = sTime + (Now - pTime)
            CmdAction(3).Caption = "Pause"
        End If
    End Select
    End Sub
    
    'Put this code under the basic Module fiile
    Option Explicit
    Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    
    Public sTime As Date
    Public eTime As Date
    Public pTime As Date
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        eTime = Now
        Ctime
    End Sub
    Private Sub Ctime()
    Dim h As Long
    Dim m As Long
    Dim s As Long
    
    s = DateDiff("s", sTime, eTime, 0, 0)
    
    h = s \ 3600
    m = (s - h * 3600) \ 60
    s = (s Mod 60)
    frmMain.lblStopWatch.Caption = h & ":" & Format(m, "00") & ":" & Format(s, "00")
    
    End Sub

  9. #9
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    I initialised the variables in the button's event procedure because every time a user clicks the start button it should reset first.

    I was trying to write it very basic without using the system time in any way or APIs. Plus I wrote it in like 2 minutes while replying to this thread. Any ideas why the seconds are not right?

    [Edited by Ramandeep on 12-05-2000 at 05:11 PM]

  10. #10
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi! Ramandeep
    I juz run your code & everything is fine. Perhaps you set a wrong interval for your timer? I've set the timer interval to 1.

  11. #11
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Chris I have set my interval to 1 but the seconds don't seem to be changing at the right speed, another thing I've noticed is my PinPong Game (another recent thing I wrote/still edditing it)is that when I run it at my UNI it runs faster, asny ideas why this may be?

    It can be the computer spec because my machine out runs the uni's tosters by mile

    My Spec
    PIII 800MHz
    Dual Proccessor Mother Boards (only installed 1 CPU at the moment)
    256Mb Ram
    Geforce 2 graphics card
    Live Platinum 5.1 sound card
    20.4Gb harddrive (7200rpm)
    Win98 Second Eddition
    Visual Basic 6.0 Enterprise Eddition
    +more

    I'm not trying to bost about my machine's spec!!!

    Any ideas? Chris what sort off machine did u run it on and did the seconds change at the right speed? If so it could be that somthing isn't configured right?

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi! Ramandeep, I don't think this is due to the hardware configuration isuue. Well, my PC is Juz the Dell Dimension L550r [*]PIII 550MHz,[*]128MB Ram

    Perhaps, you're running others application that take too much resource and cause your stopwatch running into inaccuratecy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width