B4R Code Snippet See human readable Arduino running time

Discussion in 'B4R Code Snippets' started by hatzisn, Jul 17, 2019.

  1. hatzisn

    hatzisn Active Member Licensed User

    With the following code you can see human readable Arduino running time:

    Code:
    #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, 10), ":"NumberFormat(iM, 20), ":"NumberFormat(iSec, 23)))
        
    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: Jul 17, 2019
    Johan Schoeman likes this.
  2. Erel

    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:
    Code:
    Sub LogCurrentRunningHour(curmillis As Double)

     ...
     
    Log(NumberFormat(iH, 10), ":"NumberFormat(iM, 20), ":"NumberFormat(iSec, 23))
    End Sub
     
  3. hatzisn

    hatzisn Active 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...
     
  4. Erel

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice