Android Question How works the timer in B4A ?

Discussion in 'Android Questions' started by anallie0, Apr 21, 2015.

  1. anallie0

    anallie0 Active Member Licensed User

    Hi
    How works the timer in B4A ?
    when I set the enabled property to true , the Tick event starts at 0 or 1?

    timer.png
     
  2. Troberg

    Troberg Well-Known Member Licensed User

    First event is after the set time, so if you set 1000, you'll get the first event after one second.
     
  3. DonManfred

    DonManfred Expert Licensed User

    The event will only fire AFTER you set the timer enabled.
    An disabled timer dont fire any events due to changing the interval.
     
    lemonisdead likes this.
  4. Troberg

    Troberg Well-Known Member Licensed User

    Yes, of course. I realize now that my post could be interpreted in two ways.

    What I meant was that, if you have a one second timer, the first tick will come one second after you enable it, as opposed to immediately and then every second.
     
    DonManfred likes this.
  5. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I use the following routine to ensure that the first tick is always after the preset time...
    Code:
    .....
        
    ' your other code
        tmr_Reset(2500' reset timer to 2.5s interval
    ....
     
    Sub tmr_Reset(ms As Int)
        tmr.Enabled=
    False
        tmr.Interval=ms
        tmr.Enabled=
    True
    End Sub
    Because if you change the interval on a timer that is already running then the very first event might not be to the duration set because some time will already have elapsed prior to changing the interval.
     
    Peter Simpson and lemonisdead like this.
  6. anallie0

    anallie0 Active Member Licensed User

    thank you all
    These information I needed because I use a timer for switch-on the flash, and another timer shutdown time for switch-off. I do a strobe light for the control of motor speed.
     
  7. anallie0

    anallie0 Active Member Licensed User

    I thought of doing so:

    Sub timer1_Tick
    switch-on the flash
    timer2.enabled=true
    End Sub

    Sub timer2_Tick
    switch-off the flash
    if flash is off timer2.enabled=false
    End Sub

    timer1 continuously variable max 200 Hz (5 milliseconds)
    timer2 fix at 3 o 4 milliseconds

    This evening I feel if it work well.
     
  8. anallie0

    anallie0 Active Member Licensed User

    don't work with short time.
     
  9. Peter Simpson

    Peter Simpson Expert Licensed User

    Basic strobe light below:
    Code:
    Sub Process_Globals
        
    Private FlashTimer As Timer
        
    Private FlashOn As Boolean = False
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        FlashTimer.Initialize(
    "FlashTimer"1)
        FlashTimer.Enabled = 
    True
    End Sub

    Sub FlashTimer_Tick
        FlashTimer.Interval = 
    250

        
    If FlashOn = False Then
            FlashOn = 
    True
            
    Log("Switch-on the flash")
        
    Else
            FlashOn = 
    False
            
    Log("Switch-off the flash")
        
    End If
    End Sub
    You can adjust the above code if needed by adding another timer.
     
  10. RandomCoder

    RandomCoder Well-Known Member Licensed User

    3 to 4 millisecond is very short for the timer when you bear in mind that it has to issue the event to the system which may already have other events queued, when this finally gets actioned you then intend to switch hardware and set the next event. It soon gets bottlenecked. The only way I can think that it might work would be to run on its own thread, you could then set a loop running and control the light within this loop. You might be able to use the system clock to accurately set the frequency, however I suspect that it might only work using a variable to count iterations of the loop which will vary from device to device.
    There's a lot of "might's" in my suggestion as I've never tried this and don't know for sure if it's possible to trigger hardware from another thread. Maybe someone else can confirm if it's worth a try?
     
    Peter Simpson likes this.
  11. RandomCoder

    RandomCoder Well-Known Member Licensed User

    PS. What I'm suggesting is usually considered bad practice and it will hammer the battery life, so only recommended for short periods of use.
     
  12. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I like to create a toggle using this method...
    Code:
    FlashOn = Not(FlashOn)
    Nice and simple ;)
     
    Peter Simpson likes this.
  13. Peter Simpson

    Peter Simpson Expert Licensed User

    RandomCoder likes this.
  14. Beja

    Beja Expert Licensed User

    You will need to turn the flash on in another sub to turn the flash on for the first time, then enable the timer and use the tick event for all subsequent flashes.
     
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