Android Question How works the timer in B4A ?

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
 

DonManfred

Expert
Licensed User
First event is after the set time, so if you set 1000, you'll get the first event after one second.
The event will only fire AFTER you set the timer enabled.
An disabled timer dont fire any events due to changing the interval.
 

Troberg

Well-Known Member
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.
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.
 

RandomCoder

Well-Known Member
Licensed User
I use the following routine to ensure that the first tick is always after the preset time...
B4X:
.....
    ' 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.
 

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.
 

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.
 

Peter Simpson

Expert
Licensed User
Basic strobe light below:
B4X:
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.
 

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?
 

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.
 

RandomCoder

Well-Known Member
Licensed User
B4X:
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.
I like to create a toggle using this method...
B4X:
FlashOn = Not(FlashOn)
Nice and simple ;)
 

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.
 
Top