Timer resolution vs sound

GuenterL

Member
Licensed User
Longtime User
Hi all,
something to think about.
I do have a timer running with 10 Ticks/second (Timer.Initialize("Timer", 100))

Now I countdown from 30 to zero
I want to play one single beep every second during the last 10 seconds. (mp.play)

But it beeps multiple times every second (which is correct because of the timer interval) but what's the trick to make sure, to beep only once per second .
The problem is, that the timer fires in practice between 6 and 9 times, but NOT exact 10 times /second.

Any idea ????
Thanks in advance
 

GuenterL

Member
Licensed User
Longtime User
The idea was brilliant, but the problem now is, that the 2 timers are NOT in sync, that means, the countdown display and the beep are not in sync.
If the display changed ,the beep is delayed by 0.2- 0.3 sec.
Looks crackbrained.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Timers do not run on absolute timing, they will run the event subroutine as soon as possible after the delay specified, but not before. This depends on what else is running in your program, and presumably what else is running on the device.

It would be useful if you could post some code that demonstrates this.

Are your driving the countdown with a timer? Does it display what you want, you could use the same timer to make the beep but only do it once every 10 calls to the event sub.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I agree, having 2 timers can get hairy, I would do something like this:

B4X:
'Activity module

Sub Process_Globals

    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    Dim CountDownTimer As Timer

End Sub

Sub Globals
 
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    Dim Counter As Int
    Dim ShowTime As Label

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Counter = 30
    
    ShowTime.Initialize("ShowTime")
    ShowTime.Gravity = Gravity.CENTER_HORIZONTAL
    Activity.AddView(ShowTime, 0dip, 0dip, 100%x, 50%y)
    
    CountDownTimer.Initialize("CountDownTimer", 1000)
    CountDownTimer.Enabled = True
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CountDownTimer_Tick

    ShowTime.Text = Counter
    
    If Counter <= 10 Then
    
       Dim b As Beeper
    
       b.Initialize(500, 500)
       b.Beep 
    
    End If   
    
    Counter = Counter - 1
    
    If Counter < 0 Then
    
       CountDownTimer.Enabled = False
       Msgbox("Done", "")
    
    End If

End Sub
 
Upvote 0
Top