B4R Code Snippet See human readable Arduino running time

hatzisn

Well-Known Member
Licensed User
With the following code you can see human readable Arduino running time:

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    AddLooper("TryLoop")
End Sub

Sub TryLoop
    Log(GetCurrentRunningHour(Millis))
End Sub


Sub GetCurrentRunningHour(curmillis As Double) As String
    Dim iH As Double
    Dim iM As Double
    Dim iSec As Double
    Dim sRet As String
    curmillis = curmillis/1000
 
    iH = Floor((curmillis/3600))
    iM = Floor((curmillis - iH * 3600 )/60)
    iSec = (curmillis - iH * 3600 - iM *60)
 
 
    sRet = JoinStrings(Array As String(NumberFormat(iH, 1, 0), ":", NumberFormat(iM, 2, 0), ":", NumberFormat(iSec, 2, 3)))
    Return sRet
End Sub
0:00:04.685
0:00:04.687
0:00:04.689
0:00:04.691
0:00:04.694
0:00:04.696
0:00:04.698
0:00:04.700
0:00:04.702
0:00:04.704
0:00:04.706
0:00:04.708
0:00:04.710
0:00:04.712
0:00:04.714
0:00:04.716
0:00:04.718
0:00:04.720
0:00:04.722
0:00:04.724
0:00:04.726
0:00:04.728
0:00:04.730
0:00:04.732
0:00:04.734
0:00:04.737
0:00:04.740
0:00:04.742
0:00:04.744
0:00:04.746
0:00:04.748
0:00:04.750
 
Last edited:

Erel

Administrator
Staff member
Licensed User
JoinStrings should be avoided whenever possible. Make sure that the stack buffer is large enough.

Depending on the use case you can avoid it. For example:
B4X:
Sub LogCurrentRunningHour(curmillis As Double)

 ...
 Log(NumberFormat(iH, 1, 0), ":", NumberFormat(iM, 2, 0), ":", NumberFormat(iSec, 2, 3))
End Sub
 

hatzisn

Well-Known Member
Licensed User
I created that to be used as a function. To be honest I do not know how to check the stack buffer...
 

Erel

Administrator
Staff member
Licensed User
You can check it with Log(StackBufferUsage).
I know that it is easier to use a sub that returns the value. Depending on your usage there might be cases where you will not be able to use it like this. For example if you call it inside a loop, as it will quickly run out of available memory.
 
Top