Android Question Chronometer in miliseconds

Discussion in 'Android Questions' started by ivavilagu, Jan 27, 2015.

  1. ivavilagu

    ivavilagu Member Licensed User

    I have coded a little chronometer showing milliseconds (the library Clocks doesn´t work with miliseconds). It works but It is a little bit slow and doesn´t count the correct time. This is the code:

    Code:
    dim lbTIME as label
    dim timer1. as timer
    dim countSEC as long
    dim countMM as long

    timer1.initialize(
    "Timer1"1)
    countSEC = 
    0
    countMM = 
    0

    sub timer1_Tick
        countMM = countMM + 
    1
        
    if (countMM = 100then
              countMM = 
    0
              countSEC = countSEC + 
    1
        
    end if
        lbTIME.Text = 
    NumberFormat(countSEC, 20) & ":" & NumberFormat(countMM, 20)
    end if
    is there any way to execute the timer_Tick event faster?
     
  2. sorex

    sorex Expert Licensed User

    I think it's better to use 10ms for the running timer and show the full ms detail.

    it will show different value anyway (not rounded to 10 values I mean) since there are things in the background delaying your timer.
     
  3. DonManfred

    DonManfred Expert Licensed User

    i also do think that is is NOT a good idea to force 1000 Events in one Second in B4A
     
  4. ivavilagu

    ivavilagu Member Licensed User

    Yes, I do too, but Android Chronometer shows miliseconds.
     
  5. sorex

    sorex Expert Licensed User

    the entire idea is actually wrong.

    you should store the start ticks and at each timer even you should do currenttick-starttick to get the right time.
     
  6. sorex

    sorex Expert Licensed User

    like I said it will show miliseconds too is you update it 10 times a second.
     
    ivavilagu likes this.
  7. ivavilagu

    ivavilagu Member Licensed User

    You're agree. I`m changing the code.
     
  8. ivavilagu

    ivavilagu Member Licensed User

    The problem is the same, I will do current tick-starttick every milisecond
     
  9. sorex

    sorex Expert Licensed User

    what problem is the same?

    use it like this

    Code:
    dim starttime as long=DateTime.Now
    and in your timer

    Code:
    Dim t As String
    t=
    DateTime.Now-starttime
    t=
    Floor(t/1000)&":"&Floor((t Mod 1000)/10)
    lbTIME.Text=t
     
  10. ivavilagu

    ivavilagu Member Licensed User

    Obviously, if I update 10 times every second It will not show milisecons
    Thank you for the code but the problem is to update the label every centisecond. Your code and mine works fine with deciseconds (0.1 second) but if I try with centiseconds (0.01 second), as you said, It forces to execute the tick event 100 times every second and this is the reason why the chronometer doesn´t work

    I can work with centiseconds in order to mesure the correct time.
    I'm forced to work with deciseconds in order to show the correct time.
     
  11. sorex

    sorex Expert Licensed User

    if you want mili you just use

    Code:
    t=Floor(t/DateTime.TicksPerSecond)&":"&Floor(t Mod 1000)
     
  12. sorex

    sorex Expert Licensed User

    in your initial code it couldn't work either since you used

    Code:
    if (countMM = 100then
              countMM = 
    0
    I think the problem is your interpretation of all this.
    It's not if you want to go from deci seconds to centi that you need to make the timer run 10 times faster.

    just change the extraction from the time difference.
     
  13. ivavilagu

    ivavilagu Member Licensed User

    Sorry, maybe I don´t explain well in English (I´m from Spain). I modified your code and works!!!

    Code:
    t=Floor(t/DateTime.TicksPerSecond)&":"numberformat((Floor(t Mod 100)),2,0)
    Thanks!!!!
     
  14. sorex

    sorex Expert Licensed User

    you're welcome.
     
  15. DonManfred

    DonManfred Expert Licensed User

    You suggested 10ms so it´s firing 100 events a second.
    But you are right... using 100ms (10 events a second) or 200ms (5 events a second) should be enough. Even for a chronometer if it is coded right...
     
  16. udg

    udg Expert Licensed User

    Are you aware of this lib by Jem Miller?

    udg
     
    Last edited: Jan 27, 2015
  17. ivavilagu

    ivavilagu Member Licensed User

    With the help of Sorex I have made a chronometer showing centiseconds digits on screen
     
    udg likes this.
  18. Troberg

    Troberg Well-Known Member Licensed User

    Remember, what you measure is one thing, how often you show it is another. So, you can measure with millisecond accuracy, but only update the screen at decisecond intervals. It's not like anyone will be able to read the digits at 1000 Hz (especially as the screen only updates at maybe 50Hz...).

    However, once you press stop (or whatever you do when you want the exact measurement, then you read the time at that precise moment.

    For example, it's common to do the opposite when doing minute accuracy clocks. Even though you only show minute accuracy, you update the screen at second accuracy, to prevent some odd effects if a timer event is delayed (Which happens. Remember, a timer is not an exact tick, it's more like "wait this long and then get back to me when you have time for it".), which could mean that one minute was skipped because it didn't get an event because the event got pushed to the next minute.

    Another example, which I often use, is in status displays where I display "x objects of y done". These can often get quite flickery, if the operations are quick, which I solve by only update the screen once per second. Sure, the numbers will "skip", but they will be readable and not cause epileptic seizures.

    So, think about disconnecting the measurement from the display, and do each as good as you can depending on their different needs.
     
    DonManfred likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice