problem with timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JuAn2226
    New Member
    • Mar 2008
    • 27

    problem with timer

    hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.

    Private Sub Timer1_Timer()
    Randomize

    lblCar(n).Capti on = Int(Rnd() * 50)

    StartTime = Now
    lblStart(n).Cap tion = Format(StartTim e, "hh:mm:ss")

    n = n + 1
    If n = 4 Then
    Timer1.Enabled = False
    End If

    End Sub

    Private Sub Timer2_Timer()


    EndTime = Now
    lblEnd(i).Capti on = Format(EndTime, "hh:mm:ss")
    Timeduration = DateDiff("s", StartTime, EndTime)
    lblDuration(i). Caption = "" & Timeduration

    i = i + 1
    If i = 4 Then
    Timer2.Enabled = False
    End If

    End Sub
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by JuAn2226
    hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.
    I think you're not using timers properly. Timers will do whatever you write in them every time the interval is reached.
    e.g
    if your timer event says:

    [CODE=vb]textbox1.text = textbox1.text + 1[/CODE]

    And the interval is set to 1000, then each second, the textbox will increase by one. ^.^ Hope you can figure out how to get what you need, now.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Ignore this message. I'm just "subscribin g" to the discussion thread so I can see where it goes. :)

      Comment

      • JuAn2226
        New Member
        • Mar 2008
        • 27

        #4
        Originally posted by kadghar
        I think you're not using timers properly. Timers will do whatever you write in them every time the interval is reached.
        e.g
        if your timer event says:

        [CODE=vb]textbox1.text = textbox1.text + 1[/CODE]

        And the interval is set to 1000, then each second, the textbox will increase by one. ^.^ Hope you can figure out how to get what you need, now.
        .

        Sorry i couldn't understand ur code. i try out ur code but i get error msg said type mismatch n variable not define even though i have declare the variable. if u look at my code i got two timer. each timer i set to 5000 n 10000 interval. Is it ok to use two timer in one form? i getting wrong time duration even though the formula is correct. plz help me.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          I haven't been following this thread in detail. But I suspect your problem is to do with the way the timers work. They are independent. So one of them will be firing every (roughly) 5 seconds, and the other every (roughly) ten seconds.

          In other words, Timer1 will trigger twice as often as Timer2. Is this what you intend?

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            You said your first timer has a 5000ms interval
            so each 5 seconds this will happen:
            1. It'll change the seed of the random num generator.
            2. It'll change the caption of the nth label.
            3. It'll set the start time to now, with a convinient format. (are you sure you want to change the start time each 5 seconds?)
            4. add 1 to n, and if n = 4, it will be disabled.

            Timer2 will do this each 10 seconds:
            1. Change the end time (are you sure you want to change it each 10 secs?)
            3. Change the ith lblEnd
            2. Change Timeduration (since the other timer is changed each 5 secs, it'll be zero)
            3. Change the ith lblDuration
            4. Increase i and if its 4, stop.

            So... i'll assume both are enabled since the begining, and lets say you start your program at time 0, this is what you'll have:

            Time 5 (tick Timer1)
            i=0 , n = 1, StartTime=5, EndTime=0, Duration = 0
            Time 10 (tick both)
            i=1, n=2, StartTime=10, EndTime=10, Duration = 0 (it can be 5 sometimes)
            Time 15 (tick Timer1)
            i=1, n=3, starttime=15, EndTime=10, Duration = 0
            Time 20 (tick both)
            i=2, n=4, starttime= 20, EndTime=20, Duration = 0 (it could be 5 here too)
            Time 30 (tick Timer2)
            i=3, n=4, starttime=20, Endtime= 30, Duration = 10
            Time 40 (tick Timer2)
            i=4, n=4, starttime=20, Endtime=40, Duration = 20

            Now, i didnt understand exactly what you wanted. About my example, well, it was only an example, it shouldnt be a solution.
            I hope this can be of help, but please let us know if you have doubts, or if we can be of some help.

            Comment

            • pureenhanoi
              New Member
              • Mar 2007
              • 175

              #7
              Originally posted by JuAn2226
              hi plz help me, i would like to know if got two timer in form( vb), does it run parallel or synchronous. i got problem here where i want to calculate time duration the formula is EndTime-StartTime. I code the StartTime in first timer and the EndTime in second Timer(Timer2) with 5s and 10s Interval each. BUT i facing problem here where the Time Duration is inaccurate eventhough the formula is correct. I created array of 4 for startTime n End Time. Plz guide me to solve this problem.

              Private Sub Timer1_Timer()
              Randomize

              lblCar(n).Capti on = Int(Rnd() * 50)

              StartTime = Now
              lblStart(n).Cap tion = Format(StartTim e, "hh:mm:ss")

              n = n + 1
              If n = 4 Then
              Timer1.Enabled = False
              End If

              End Sub

              Private Sub Timer2_Timer()


              EndTime = Now
              lblEnd(i).Capti on = Format(EndTime, "hh:mm:ss")
              Timeduration = DateDiff("s", StartTime, EndTime)
              lblDuration(i). Caption = "" & Timeduration

              i = i + 1
              If i = 4 Then
              Timer2.Enabled = False
              End If

              End Sub
              see these statements:
              startTime = Now
              endTime = Now

              Each time Timers fire event, the startTime and andTime varriables always get the lastest moment.
              Now, two timers have inteval is 5S different. If Timer2 fires event first, so Duration = Now - StartTime = 5
              If two timer fire event together or Timer1 fires event first, so Duration = endTime-StartTime = Now - Now = 0.

              I think, you should set the startTime and EndTime out of Timer1 (or Timer2)_Timer()

              Comment

              • JuAn2226
                New Member
                • Mar 2008
                • 27

                #8
                Originally posted by pureenhanoi
                see these statements:
                startTime = Now
                endTime = Now

                Each time Timers fire event, the startTime and andTime varriables always get the lastest moment.
                Now, two timers have inteval is 5S different. If Timer2 fires event first, so Duration = Now - StartTime = 5
                If two timer fire event together or Timer1 fires event first, so Duration = endTime-StartTime = Now - Now = 0.

                I think, you should set the startTime and EndTime out of Timer1 (or Timer2)_Timer()
                Thanks 4 the reply. i tryed ur suggestion but its x working. But i got question here . I need to store random in array of 10.Then i have to check if the random number exits or x if no i have to store number which been generatedin array of 10 (in any index ) at the same time i store time in at array time. How do i go abt.


                Dim n As Integer



                Private Sub Form_Load()

                Randomize

                For n = 0 To 9

                lblCar(n).Capti on = Int(Rnd() * 50)

                Next n

                End Sub

                i dont know how to further plz guide me.

                Comment

                • pureenhanoi
                  New Member
                  • Mar 2007
                  • 175

                  #9
                  Originally posted by JuAn2226
                  Thanks 4 the reply. i tryed ur suggestion but its x working. But i got question here . I need to store random in array of 10.Then i have to check if the random number exits or x if no i have to store number which been generatedin array of 10 (in any index ) at the same time i store time in at array time. How do i go abt.


                  Dim n As Integer



                  Private Sub Form_Load()

                  Randomize

                  For n = 0 To 9

                  lblCar(n).Capti on = Int(Rnd() * 50)

                  Next n

                  End Sub

                  i dont know how to further plz guide me.
                  If you store 10 random number by a For routine, the time for each number are the same (coz the coputer can store thousands of number in a Second). So, you can use a Timer like your first post

                  Desiging:

                  put one timer and one Command button into form
                  set Timer Interval to 1000 (or any inteval you want)

                  Coding:
                  [CODE=vb]Option Explicit
                  Dim numberStored As Integer
                  Dim NumberArray(0 to 9) As Integer
                  Dim TimeArray(0 to 9) As Date

                  Private Sub Command1_Click( )
                  numberStored = 0 'havent stored any number yet
                  Timer1.Enabled = True 'start generate and storing
                  Command1.Enable d = False 'prevent user click while timer running
                  End Sub

                  Private Sub Timer1_Timer()
                  Dim tmpNumber As Integer
                  Dim i As Integer
                  Timer1.Enabled = False 'stop timing while storing number
                  Randomize
                  tmpNumber = Int(Rnd() * 50)
                  'check if this number already exist
                  For i = 0 To 9
                  If NumberArray(i) = tmpNumber Then
                  'the number exist -> do not store it
                  Timer1.Enabled = True
                  Exit Sub
                  End If
                  Next
                  'If this number does not exist -> store in Number Array
                  Dim index As Integer
                  index = AnyIndex 'calculate the index that you want
                  NumberArray(ind ex) = tmpNumber 'store the number
                  TimeArray(index ) = Now 'store the time
                  numberStored = numberStored + 1
                  If numberStored = 10 Then 'stop after stored 10 number
                  Timer1.Enabled = False
                  Command1.Enable d = True 'allow user use command to try again
                  Else 'do not reach 10
                  Timer1.Enabled = True 'continue storing
                  End If
                  End Sub[/CODE]

                  Comment

                  • JuAn2226
                    New Member
                    • Mar 2008
                    • 27

                    #10
                    Hi i try m modified ur code. ur code very helpful. I got some question here.

                    I need second timer. so i created one. To capture te endTime when car pass the second reader. The question here is can i randomize the time interval. this means i dont want to fix the time interval at properties window.

                    My code
                    Private Sub Timer2_Timer()
                    Randomize x
                    x = 2000 + Int(Rnd() * 3000)
                    Timer2.Interval = x
                    EndTime = Now
                    lblEnd(i).Capti on = Format(EndTime, "hh:mm:ss")

                    End Sub

                    after i modifed ur code i add this code together. its not showing any output. Plz help me. give some comment abt my code.

                    Comment

                    • JuAn2226
                      New Member
                      • Mar 2008
                      • 27

                      #11
                      Thanks for the reply. i understand what is the problem. but i still blur how to solve the problem. can i use randomize function for time interval. What i mean i dont want to fix the Timer 2 interval at the properties window but instead i want to randomize the interval. u might have clear of what i'm writing here by looking at my code


                      Private Sub Timer2_Timer()
                      Randomize x
                      x = 2000 + Int(Rnd() * 3000)
                      Timer2.Interval = x
                      EndTime = Now
                      lblEnd(i).Capti on = Format(EndTime, "hh:mm:ss")

                      End Sub

                      This code didnt give any output. i need some suggestion here. guide me plz. can this be done?
                      Last edited by debasisdas; Mar 14 '08, 09:57 AM. Reason: removed excess quote

                      Comment

                      • harshadd
                        New Member
                        • Dec 2007
                        • 180

                        #12
                        Even this is too late to subscribe this thread now , I still wanted to know what exactly you were trying to achieve?
                        If you are trying to calculate time diff between two events then you do not need a timer at all.
                        Timers are basically used to repeat the same code after a fixed interval (in Milli seconds)

                        Comment

                        • JuAn2226
                          New Member
                          • Mar 2008
                          • 27

                          #13
                          Originally posted by harshadd
                          Even this is too late to subscribe this thread now , I still wanted to know what exactly you were trying to achieve?
                          If you are trying to calculate time diff between two events then you do not need a timer at all.
                          Timers are basically used to repeat the same code after a fixed interval (in Milli seconds)
                          Hi thanks for the reply, yes i'm trying to calculate time different between two event.
                          1. I have to create array of 10 (CarID)

                          2. I have to generate random number for 50.That generated random number is store in the array(CarID) that been created previously.

                          3. the generator random number have to be checked in array if its in array(CarID) or not. If not then store the random number in the array and store the Time In(means the car pass reader 1)
                          .
                          4. If the the random number/ counter in the Array then place current time out(means car passes at reader 2.After that calculate Time Duration and calculate Speed.

                          5. CarNum(random number), TimeIn, TimeOut, TimeDuration,Sp eed and Average Speed is dispay.

                          6. I have to reset this application after the it gets the Average Speed and calculate new average speed for new group of cars.



                          The problem i'm facing here i already did coding for the part 2. Where i have to generate random number and check but i have problem with the time in and time out. if u say i dont have to use timer then how i go about? plz guide me.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Do you mean that you also need to generate random times?

                            Comment

                            • harshadd
                              New Member
                              • Dec 2007
                              • 180

                              #15
                              Please tell me what I understood is correct or not?

                              I got it this way: you have 50 Cars (50 random Numbers)
                              when any number appeared 1st time say (no 7) that means ur car of that no 7 is started from reader1.(or say passed from reader1)
                              when the same number (ie no 7) appeared again (by random generator) that means ur car of no 7 is passed from reader 2.
                              Now here on pass 1 and pass2 you are keeping the time entry of each pass, and then you will calculate the difference between two time entries for car no 7 (in this example) and so on for all the 50 cars...

                              In short you are trying to get the time difference between same random nos generated twice?????
                              HND

                              Comment

                              Working...