Results 1 to 31 of 31

Thread: [RESOLVED] Start/Stop Clock Wierdness

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Resolved [RESOLVED] Start/Stop Clock Wierdness

    I was provided some code for a countdown timer that would tick upon starting. What the code failed to provide was a way to stop the clock from ticking down and a way to restart it via a command button.

    Here is the code I have.

    Code:
    Dim StartTime As Long, Mincount As Integer
    Dim Minutes As String, Seconds As String
    Dim TimeA As Long, TimeB As Long
    Dim GameLength As Integer ' Playing Time in Seconds
    
    Private Sub timeClock_Timer()
    '    Dim lTemp As Long
    '    Dim lTemp2 As Long
    '    intCounter = intCounter - 1000
    '    lTemp = intCounter
    '    lTemp = lTemp / 1000 'convert to seconds
    '    lTemp2 = lTemp
    '    lTemp = (lTemp / 60) Mod 60 'Minutes
    '    lTemp2 = (lTemp2 Mod 60) 'Seconds
    '    If lTemp2 < 10 Then
    '        lblTime.Caption = lTemp & ":0" & lTemp2
    '    Else
    '        lblTime.Caption = lTemp & ":" & lTemp2
    '    End If
    '    If intCounter = 0 Then
    '        timeClock.Enabled = False
    '        MsgBox "Times up"
    '    End If
    
    If (Timer - StartTime) / (Mincount + 1) > 60 Then Mincount = Mincount + 1
    Seconds = Format$(60 * (Mincount + 1) - (Timer - StartTime), "00")
    If Seconds = "60" Then Seconds = "00"
    If GameLength - (Timer - StartTime) > 60 Then
        Minutes = Format$((GameLength - (Timer - StartTime)) \ 60, "00:")
        lblTime.Caption = Replace(Minutes & Seconds, "01:", "1:")
    ElseIf GameLength - (Timer - StartTime) > 0 Then
       lblTime.Caption = Replace(Format$(60 * (Mincount + 1) - (Timer - StartTime), "00:00.0"), "00:", "")
    Else: 'MsgBox "Game Over!"
    End If
    End Sub
    
    Private Sub cmdClock_Click()
        If lblTime.Caption = "0:00" Then
            'intCounter = 150000 '20 minutes
        End If
        If timeClock.Enabled = True Then
            TimeA = Timer
            'StartTime = StartTime - Timer
            StartTime = StartTime + (TimeA - TimeB)
            timeClock.Enabled = False
            cmdClock.Caption = "Start Clock"
        Else
            timeClock.Enabled = True
            cmdClock.Caption = "Stop Clock"
            TimeB = Timer
        End If
    End Sub
    
    Private Sub Form_Load()
        GameLength = 120
        StartTime = Timer
    End Sub
    Any help with getting the final piece to the puzzle finished would be greatly appreciated.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    Code:
    Option Explicit
    
    Dim StartTime As Long, Mincount As Integer
    Dim Minutes As String, Seconds As String
    Dim TimeA As Long, TimeB As Long
    Dim GameLength As Integer ' Playing Time in Seconds
    
    Private Sub timeClock_Timer()
    '    Dim lTemp As Long
    '    Dim lTemp2 As Long
    '    intCounter = intCounter - 1000
    '    lTemp = intCounter
    '    lTemp = lTemp / 1000 'convert to seconds
    '    lTemp2 = lTemp
    '    lTemp = (lTemp / 60) Mod 60 'Minutes
    '    lTemp2 = (lTemp2 Mod 60) 'Seconds
    '    If lTemp2 < 10 Then
    '        lblTime.Caption = lTemp & ":0" & lTemp2
    '    Else
    '        lblTime.Caption = lTemp & ":" & lTemp2
    '    End If
    '    If intCounter = 0 Then
    '        timeClock.Enabled = False
    '        MsgBox "Times up"
    '    End If
    
    If (Timer - StartTime) / (Mincount + 1) > 60 Then Mincount = Mincount + 1
    Seconds = Format$(60 * (Mincount + 1) - (Timer - StartTime), "00")
    If Seconds = "60" Then Seconds = "00"
    If GameLength - (Timer - StartTime) > 60 Then
        Minutes = Format$((GameLength - (Timer - StartTime)) \ 60, "00:")
        lblTime.Caption = Replace(Minutes & Seconds, "01:", "1:")
    ElseIf GameLength - (Timer - StartTime) > 0 Then
       lblTime.Caption = Replace(Format$(60 * (Mincount + 1) - (Timer - StartTime), "00:00.0"), "00:", "")
    Else: 'MsgBox "Game Over!"
    End If
    End Sub
    
    Private Sub cmdClock_Click()
        If lblTime.Caption = "0:00" Then
            'intCounter = 150000 '20 minutes
        End If
        If cmdClock.Caption = "Start Clock" Then
            timeClock.Interval = 500
            TimeA = Timer
            StartTime = StartTime - Timer
            StartTime = StartTime + (TimeA - TimeB)
    '        timeClock.Enabled = False
    '        cmdClock.Caption = "Start Clock"
    '    Else
    '        timeClock.Enabled = True
            cmdClock.Caption = "Stop Clock"
            TimeB = Timer
        Else
            timeClock.Enabled = False
        End If
    End Sub
    
    Private Sub Form_Load()
    
        cmdClock.Caption = "Start Clock"
        GameLength = 120
        StartTime = Timer
    End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    code is not quite right Martin, am I not being clear with my code that I posted... I should have taken the comments out more than likely.

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Logic was not right... I modified it to this from your example. I run the script and the just stagnates, does not update the "clock"

    Code:
    Private Sub cmdClock_Click()
        If lblTime.Caption = "0:00" Then
            'intCounter = 150000 '20 minutes
        End If
        If cmdClock.Caption = "Start Clock" Then
            timeClock.Interval = 500
            TimeA = Timer
            StartTime = StartTime - Timer
            StartTime = StartTime + (TimeA - TimeB)
            cmdClock.Caption = "Stop Clock"
            timeClock.Enabled = True
            TimeB = Timer
        Else
            timeClock.Enabled = False
            cmdClock.Caption = "Start Clock"
        End If
    End Sub

  6. #6

  7. #7

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Okay I see what you did, and it is reflected in your project.

    your clock starts and stops but does not resume after being stopped. This is being used in a sports application albeit basketball.

    Not so important, but a down the line feature consideration... how to change the clock time in the event it is changed etc. Expecially since this clock does tenth second accuracy.

  9. #9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Alright, how can I make the clock restart gracefully after being stopped, that was my original problem.

  11. #11

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    Okay. I added a new Long global variable called "SaveTimer". Maybe I could have used TimeA and/or TimeB but this works.

    Code:
    Private Sub cmdClock_Click()
        Static bRestart As Boolean
        If lblTime.Caption = "0:00" Then
            'intCounter = 150000 '20 minutes
        End If
        If cmdClock.Caption = "Start Clock" Then
            timeClock.Enabled = True
            If Not bRestart Then
                timeClock.Interval = 500
                TimeA = Timer
                StartTime = StartTime - Timer
                StartTime = StartTime + (TimeA - TimeB)
                bRestart = True
            Else
                StartTime = SaveTimer
            End If
            cmdClock.Caption = "Stop Clock"
            TimeB = Timer
        Else
            SaveTimer = Timer
            timeClock.Enabled = False
            cmdClock.Caption = "Start Clock"
    End If
    
    ' Also add this line at the bottom of timeClock_Timer()
    
    SaveTimer = Timer

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    That's very strange it is working for you Martin,

    Everytime I stop then start the clock again it starts from 1:58 as if the SaveTimer is not "saving"

    Could you post your project? I have not compiled this yet, could that be an issue?

  14. #14

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Appreciate it! I'm tinkering with it!

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    Okay, no more TmeA and TimeB.

    Code:
    Private Sub cmdClock_Click()
        Static bRestart As Boolean
        If lblTime.Caption = "0:00" Then
            'intCounter = 150000 '20 minutes
        End If
        If cmdClock.Caption = "Start Clock" Then
            timeClock.Enabled = True
            If Not bRestart Then
                timeClock.Interval = 500
                StartTime = Timer
                bRestart = True
            Else
                StartTime = StartTime + (Timer - SaveTimer)
            End If
            cmdClock.Caption = "Stop Clock"
        Else
            SaveTimer = Timer
            timeClock.Enabled = False
            cmdClock.Caption = "Start Clock"
        End If
    End Sub

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Sir, that did it, Thank you very much for the help! I know this will get asked again, good thing it will show up in the search now!

  18. #18

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Again I come to you guys with another buggish issue.

    I also mentioned above that if I wanted to increase or decrease the time that it could be arranged. Martin told me add to start time. While it makes logical sense. My Timer code seems to not want to recognize it and spit out odd numbers on the seconds field. I feel that is where my code is flawed, or isn't bulletproof. It will eventually correct itself at the next minute mark, aka a if/then loop is passed?

    I have it setup so that two prompt boxes are shown when you press adjust clock. One says minute and another second. negative integers number work here. I might make another one that says miliseconds if that is possible.

    Any suggestions or am I just being too dang picky. Again guy's you are the BEST that's why I only visit here for vb stuff.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Anyone?

  21. #21
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    We don't want people bumping their threads so please don't do it again.

    Having said that, please zip up your project and attach it and provide step by step instructions on exactly how to reproduce the error.

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    I apologize Martin, won't happen again.

    Here is my project.

    Steps to reproduce: Run the Project, click on Adjust Clock. Enter 0 for the Minutes and type in 25 or so seconds for the other prompt. Click Start Clock. The time does not add to the minutes. I have a feeling my math logic is flawed?
    Attached Files Attached Files

  23. #23
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    The problem is in this code.

    Code:
    Private Sub cmdClock_Click()
        Static bRestart As Boolean
        If lblTime.Caption = "0:00" Then
            ' What to do when Clock Resets
            
            ' Prompt Asking to increase QTR & SetClock to Default
        End If
        If cmdClock.Caption = "Start Clock" Then
            timeClock.Enabled = True
            If Not bRestart Then
                timeClock.Interval = 100
                StartTime = Timer
                bRestart = True
            Else            StartTime = StartTime - Modify + (Timer - SaveTimer)
            End If
            cmdClock.Caption = "Stop Clock"
        Else
            SaveTimer = Timer
            timeClock.Enabled = False
            cmdClock.Caption = "Start Clock"
        End If
    End Sub
    The main problem is that you set StartTime at Form_Load, you adjust it, and then you reset StartTime with the highlighted code, overwriting the adjustment. The second problem is that bRestart is local to this sub so the If Not bRestart is always True. I think you want to use If Not Adjusted_Clock instead but when you do there is an overflow problem. I'll work on that in a while but maybe you can fix it first yourself.

  24. #24

  25. #25
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Re: Start/Stop Clock Wierdness

    Quote Originally Posted by DJHotIce
    I apologize Martin, won't happen again.

    Here is my project.

    Steps to reproduce: Run the Project, click on Adjust Clock. Enter 0 for the Minutes and type in 25 or so seconds for the other prompt. Click Start Clock. The time does not add to the minutes. I have a feeling my math logic is flawed?
    Hi,

    Not sure you still need it, but I've got a sample project for you (or anybody else strugling with timers) that has a resumable timer and resumable countdown.

    Edit: Forgot to stop the countdown when it reached 0, fixed now.

    HTH
    Attached Files Attached Files
    Last edited by Jottum; Feb 22nd, 2008 at 08:25 AM.
    Jottum™
    XpVistaControls , (transparent) usercontrols for XP, Vista and 7 with W2K legacy support.

  26. #26
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    I made two more changes. The first immediately shows the adjusted time in the clock and the second is the addition of a form to do the adjustment rather than input boxes.
    Attached Files Attached Files

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Not a big big issue, but the clock lost the tenth second precision when the clock is under a minute.

    Other than that I think it is good. I'm not gonna complain, way smarter than I!

    (BTW I am aware of split second buffering issues)

  28. #28

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Posts
    192

    Re: Start/Stop Clock Wierdness

    Code:
    ElseIf GameLength - (Timer - StartTime) > 0 Then
       lblTime.Caption = Replace(Format$(60 * (Mincount + 1) - (Timer - StartTime), "00:00.0"), "00:", "")
    This code was in earlier. When the clock reached 1 minute it would tick down 59.9, 59.8 59.7 so on so on till it reached 00:00

    Right now it goes 59, 58, 57, 56 no decimal precision.

  30. #30

  31. #31
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Start/Stop Clock Wierdness

    I believe I've fixed that problem. You didn't say anything about frmAdjust so I'll assume you are using it and noticed that you needed to click Exit twice to get out. I fixed that problem (by eliminating the button) and added code to adjust the tenths. Finally I changed frmMain so that you can't adjust the clock while it's running.
    Attached Files Attached Files

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