-
[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.
-
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
-
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.
-
Re: Start/Stop Clock Wierdness
Tell me what's not right and maybe I can help.
-
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
-
Re: Start/Stop Clock Wierdness
Quote:
Originally Posted by DJHotIce
Logic was not right...
Then you didn't copy and/or use it all because it works for me. For example did you see the changes in Form_Load?
-
1 Attachment(s)
Re: Start/Stop Clock Wierdness
-
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.
-
Re: Start/Stop Clock Wierdness
Add 5 seconds to the clock.
StartTime = StartTime + 5
-
Re: Start/Stop Clock Wierdness
Alright, how can I make the clock restart gracefully after being stopped, that was my original problem.
-
Re: Start/Stop Clock Wierdness
-
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
-
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?
-
Re: Start/Stop Clock Wierdness
You are right it's not working properly. I'm going out for a couple of hours and I'll look at it when I get back unless you tell me you have it figured out by then.
-
Re: Start/Stop Clock Wierdness
Appreciate it! I'm tinkering with it!
-
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
-
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!
-
Re: Start/Stop Clock Wierdness
Don't forget to pull down the Thread Tools menu and mark the thread resolved.
-
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.
-
Re: Start/Stop Clock Wierdness
-
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.
-
1 Attachment(s)
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?
-
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.
-
1 Attachment(s)
Re: Start/Stop Clock Wierdness
I made several changes and marked them all with 'Marty
-
1 Attachment(s)
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
-
1 Attachment(s)
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.
-
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)
-
Re: Start/Stop Clock Wierdness
Quote:
Originally Posted by DJHotIce
Not a big big issue, but the clock lost the tenth second precision when the clock is under a minute....
I'm not sure what you mean.
-
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.
-
Re: Start/Stop Clock Wierdness
-
1 Attachment(s)
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.