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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a Digital stopwatch... 1

Status
Not open for further replies.

uprocker2

Programmer
Dec 26, 2004
34
0
0
BE
1.i've got a label1 object that needs the time on its caption:
label1.caption =
i've got a commandbutton so when u click on it the time start in label1.caption at 00.00 (no seconds needed, onli minits and hours)
and a timer object offcourse
2. is it also possible to have 1 second be 12 seconds on the label1.caption and when it goes after 23.59 then it goes back to 00.00 so you'll have a day cicle that takes 2 hours in real life.

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
Here is one for seconds...

You need a timer (Timer1), a label (Label1), and 2 buttons (Command1, Command2)

Code:
Private StartTime As Double

Private Sub Command1_Click()
  If Command1.Caption = "Start" Then
    Command1.Caption = "Pause"
    StartTime = Timer - Val(Label1.Caption)
    Timer1.Enabled = True
  Else
    Timer1.Enabled = False
    Command1.Caption = "Start"
  End If
End Sub

Private Sub Command2_Click()
  Label1.Caption = "0.00"
  StartTime = Timer - Val(Label1.Caption)
End Sub

Private Sub Form_Load()
  Command1.Caption = "Start"
  Command2.Caption = "Reset"
  Label1.Caption = "0.00"
  Timer1.Interval = 10
End Sub

Private Sub Timer1_Timer()
  Label1 = Round(Timer - StartTime, 2)
End Sub

You can figure hours and minutes from there...

hours = seconds / 3600
minutes = seconds / 60 - (hours * 60)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
uptill now evrything is ok but how do I do the following
1second = 12 seconds so the clock counts like this:
0
12
24
36
48
60(1minit)
...ect


sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
see Label2 for the Hour/Minute Example...
Code:
Private Sub Timer1_Timer()
  Dim t As Double
  t = Timer - StartTime
  Label1 = Round(t, 2)
  Label2 = Right("00" & (t \ 3600), 2) & ":" & Right("00" & t \ 60 - ((t \ 3600) * 60), 2)
End Sub

you can adjust the 3600's and 60's as you please...

minutes = 60 seconds
hour = 3600 seconds = 60 * minutes

So... if you want 1 second to = 12 seconds...
Minute = 5 seconds
hour = 60 * 5 seconds = 300

so...
Code:
Private Sub Timer1_Timer()
  Dim t As Double
  t = Timer - StartTime
  Label1 = Round(t, 2)
  Label2 = Right("00" & (t \ 300), 2) & ":" & Right("00" & t \ 5 - ((t \ 300) * 5), 2)
End Sub

But that would make 5 days pass in 1 real day...

As far as the 23:56 -> 00:00 use a MOD on the Hour...
Right("00" & ((t \ 300) MOD 24), 2)


Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
so... Is this what you were looking for?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
almost but I still don't understand can u explane evry line of code please???

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
btw why doe'snt the clock start at 0.00?


sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
>explane evry line of code

That's not really what we're here for, you know...
 
this line makes it start @ 0, or where it was last paused...

StartTime = Timer - Val(Label1.Caption)

This line sets the clock relative to the start time...

t = Timer - StartTime


Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Option Explicit
Private Stopwatch As Date

Private Sub Command1_Click()
Stopwatch = 0
Timer1.Interval = 1000 / 5 ' divisor is how fast you want watch to go. In this case 5 times faster than normal
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
Timer1.Enabled = False

End Sub

Private Sub Timer1_Timer()
Stopwatch = DateAdd("s", 1, Stopwatch)
Label1.Caption = Format(Stopwatch, "hh:mm")
End Sub
 
Yeah, If you want to do it the "EASY WAY"...

j/k... looks good to me ;-)

(probably easier to understand too)

I still have a thing for the Timer Function (I guess I just Used it for too many years ;-))

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top