Android Question How works the timer in B4A ?

anallie0

Active Member
Licensed User
Longtime 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
Longtime 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.
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

anallie0

Active Member
Licensed User
Longtime 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.
 
Upvote 0

anallie0

Active Member
Licensed User
Longtime 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.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime 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.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime 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?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime 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 ;)
 
Upvote 0

Beja

Expert
Licensed User
Longtime 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.
 
Upvote 0
Top