Android Question Timer stability

stari

Active Member
Licensed User
Longtime User
In the test project, I use a timer to produce a steady flicker.
However, the flicker is not stable.
What suggestion?

Timer:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
'    Dim timer_image As Timer
    Dim timer_blink 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 blink_step As Int         :blink_step = 0
    
    Dim led_txt As Int        :led_txt=50
    
'    Private lblLED As Label
'    Private lblTXT As Label
    Private lblLED As Label
    Private lblTXT As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("tttest1")
        
    timer_blink.Initialize("timer_blink",1)
    timer_blink.Enabled=False
    
    load_scene       
    
End Sub

Sub Activity_Resume
    timer_blink.Enabled=True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    timer_blink.Enabled=False
End Sub


private Sub load_scene
'    lblLED.Initialize("lblLED")
    lblLED.Top=0
    lblLED.Left=0
    lblLED.Width=100%x
    lblLED.Height=100%y
    lblLED.Color=Colors.White
    
'    lblTXT.Initialize("lblTXT")
    lblTXT.Top=lblLED.Top+led_txt
    lblTXT.Left=lblLED.Left+led_txt
    lblTXT.Width=lblLED.Width-(led_txt*2)
    lblTXT.Height=lblLED.Height-(led_txt*2)
    lblTXT.Color=Colors.Black
    lblTXT.Visible=True       
End Sub


Sub timer_blink_Tick
        
    blink_step=blink_step+1
    If blink_step=12 Then
        lblLED.Visible=False
    End If
    If blink_step=25 Then
        lblLED.Visible=True
        blink_step=0
    End If
    
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
However, the flicker is not stable.
What do you mean with not stable?
Your timer is running 1000 times a seconds. This may be where the problem starts.

Change the timer to tick every 100ms (or later) and adapt your counter to match the new timing.
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
Thks, DonManfred.
Not stable = frequency is not stabe.
With slower time period is very dificult, or imposibile, to achive, fx, 33Hz.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Thks, DonManfred.
Not stable = frequency is not stabe.
Actually, that is an unknown, given the information that you provide. When you set the visibility of a label/UI element, it actually sends an event into the event queue. Once your timer_blink_Tick sub finishes, the event queue is processed again. The delay in processing the event queue is not really deterministic, since Android is not a RTOS.
 
Upvote 0

Midimaster

Active Member
Licensed User
Apart from the fact, that you cannot see a "Blinking label" with a high frequency on 60Hz-Monitors, you should use the timer only to call a periodical interval to check if the LED-state should change. But do not use the timer to measure the time, that passed. Better use the DateTime.now value:
B4X:
Sub Globals
    Private TimerBlink As Timer, StopButton As Button, LED_ON As Boolean
    Private Hertz, RealTime, Interval, StartTime  As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Hertz=12
    Interval=1000/Hertz
    Dim TimerInterval As Int =Interval/10
    If TimerInterval<1 Then 
        TimerInterval=1
    End If
    TimerBlink.Initialize("TimerBlink",TimerInterval)

    StopButton.Initialize("Button")
    StopButton.Text="Press to stop"
    Activity.AddView(StopButton,0,0,100%x,20%y)
End Sub

Sub Activity_Resume
    TimerBlink.Enabled=True
    RealTime=DateTime.Now+1000
    StartTime=RealTime
End Sub

Sub Button_Click
    ExitApplication
End Sub

Private Sub TimerBlink_Tick
    If DateTime.Now>RealTime Then
        RealTime=RealTime+Interval/2
        If LED_ON=True Then
            LED_ON=False
            DoSomeThing
        Else
            LED_ON=True
            Log("Off")
        End If
    End If
End Sub

Sub DoSomeThing
    Dim Showtime As Double= (RealTime-StartTime)/1000
    Log($"On after $1.3{Showtime} sec  Interval= $1.1{Interval} msec"$)
End Sub
 
Last edited:
Upvote 0

stari

Active Member
Licensed User
Longtime User
Apart from the fact, that you cannot see a "Blinking label" with a high frequency on 60Hz-Monitors, you should use the timer only to call a periodical interval to check if the LED-state should change. But do not use the timer to measure the time, that passed. Better use the DateTime.now value:
B4X:
Sub Globals
    Private TimerBlink As Timer, StopButton As Button, LED_ON As Boolean
    Private Hertz, RealTime, Interval, StartTime  As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Hertz=12
    Interval=1000/Hertz
    Dim TimerInterval As Int =Interval/10
    If TimerInterval<1 Then
        TimerInterval=1
    End If
    TimerBlink.Initialize("TimerBlink",TimerInterval)

    StopButton.Initialize("Button")
    StopButton.Text="Press to stop"
    Activity.AddView(StopButton,0,0,100%x,20%y)
End Sub

Sub Activity_Resume
    TimerBlink.Enabled=True
    RealTime=DateTime.Now+1000
    StartTime=RealTime
End Sub

Sub Button_Click
    ExitApplication
End Sub

Private Sub TimerBlink_Tick
    If DateTime.Now>RealTime Then
        RealTime=RealTime+Interval/2
        If LED_ON=True Then
            LED_ON=False
            DoSomeThing
        Else
            LED_ON=True
            Log("Off")
        End If
    End If
End Sub

Sub DoSomeThing
    Dim Showtime As Double= (RealTime-StartTime)/1000
    Log($"On after $1.3{Showtime} sec  Interval= $1.1{Interval} msec"$)
End Sub

Thks, but is not much better.
As OliverA say, Android is not a RTOS.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I don't know if it can apply to your project, but eventually have a look at my old component dgTextEffect.
If I recall correctly it had a blinking effect too.

It was based on a standard Android component so you could derive your own solution using thata s a starting point.
 
Upvote 0

Midimaster

Active Member
Licensed User
How often do you wish to see the label blinking? It is not only a problem of RTOS or not, it is caused by optical effects. Fact is, that the monitor refreshes the screen only 60 times second and this limits the blinking to max 30Hz. But also all blink-frequenies between 15Hz and 30Hz cannot displayed correctly in a "30Hz blinking-system", because they are overlapped by a kind of "moiree effect". Only below 15Hz it will look like working correct. They same effect, why audio is recorded at 44100Hz to enable frequencies below 22050Hz.

If you would use a single LED connected to the Smartphone you would reach blinkings upto 500Hz only limited by the 1/1000 sec of the timer. For higher frequencies you need to activate the QueryPerformanceTimers. (Dont know whether they are really called like this on android-systems, but I know them from Rasperry and Windows)
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
How often do you wish to see the label blinking? It is not only a problem of RTOS or not, it is caused by optical effects. Fact is, that the monitor refreshes the screen only 60 times second and this limits the blinking to max 30Hz. But also all blink-frequenies between 15Hz and 30Hz cannot displayed correctly in a "30Hz blinking-system", because they are overlapped by a kind of "moiree effect". Only below 15Hz it will look like working correct. They same effect, why audio is recorded at 44100Hz to enable frequencies below 22050Hz.

If you would use a single LED connected to the Smartphone you would reach blinkings upto 500Hz only limited by the 1/1000 sec of the timer. For higher frequencies you need to activate the QueryPerformanceTimers. (Dont know whether they are really called like this on android-systems, but I know them from Rasperry and Windows)

Midi,master thanks for the comprehensive explanation.
Since I need a flicker of 1 to 100 Hz, I will add an ArduinoNano-controlled LED strip.
 
Upvote 0
Top