B4R Question how to blink LED without big delay ?

peacemaker

Expert
Licensed User
Longtime User
HI, All

If some data transmission is running - how to blink _sometimes_ by a LED to avoid big transmission delay ?
 
If it is blocking the thread then as far as my knowledge goes there is no way. If it is not then have a look at this. You can reprosduce this behaviour with an AddLooper(SubName).

 
Upvote 0
Beside my previous message if you have control over the data tranasmited (i.e. sending yourself data over TCP) then in each chunk of data sent/received call yourself the sub.
 
Upvote 0
Yes, seems, solved so:

B4X: Copy
Sub Process_Globals
    Private PacketCounter As UInt
    Private btLEDPin As Pin, btLEDPinState As Boolean
End Sub

Sub Start(BTledPinNumber As UInt)
    TransmissionLEDpinNumber = BTledPinNumber
    btLEDPin.Initialize(BTledPinNumber, btLEDPin.MODE_OUTPUT)
    Log("Starting BLE-Server")
    StartBLEServer
End Sub

Sub SendMessage(msg() As Byte)
    If BLE_client_Connected Then
        RunNative("SendBLEMessage", msg)
        PacketCounter = PacketCounter + 1
        LEDblink
    End If
End Sub

Sub NewBLEMessage 'called from inline C
    Log("New BLE message...")
    Dim mc(BLEMessage_Length) As Byte
    bc.ArrayCopy2(BLEMessage,0,mc,0,BLEMessage_Length)
    Log("Received: ", bc.StringFromBytes(mc))
    PacketCounter = PacketCounter + 1
End Sub

Private Sub LEDblink
    If PacketCounter > 20 Then
        btLEDPin.DigitalWrite(btLEDPinState)  'restore state
        PacketCounter = 0
    End If
    If PacketCounter > 17 Then
        btLEDPin.DigitalWrite(False)
    End If
    If PacketCounter > 13 Then
        btLEDPinState = btLEDPin.DigitalRead   'prev LED state
        btLEDPin.DigitalWrite(True)
    End If
End Sub
 
Last edited:
Upvote 0
If it is blocking the thread then as far as my knowledge goes there is no way. If it is not then have a look at this. You can reprosduce this behaviour with an AddLooper(SubName).


Actually the video mentioned before was the second option that I had and has been proven also correct...

My original was this:

B4X: Copy
Sub Process_Globals
    Dim initial1 As ULong = 0
    Dim initial2 As ULong = 0
    Dim bState1 As Boolean = False
    Dim bState2 As Boolean = True
End Sub

'In Setup AddLooper(MyLoopingSub)

Sub MyLoopingSub
    If Millis()-initial1 > 2500 Then
        bState1 = Not(bState1)
        If bState1 = False Then
            'Set LED1 to off
        else
            'Set LED1 to on
        End If
        initial1 = Millis()
    End If
    If Millis()-initial2 > 1700 Then
        bState2 = Not(bState2)
        If bState2 = False Then
            'Set LED2 to off
        else
            'Set LED2 to on
        End If
        initial2 = Millis()
    End If
End Sub
 
Upvote 0
Actually the video mentioned before was the second option that I had and has been proven also correct...

My original was this:

B4X: Copy
Sub Process_Globals
    Dim initial1 As ULong = 0
    Dim initial2 As ULong = 0
    Dim bState1 As Boolean = False
    Dim bState2 As Boolean = True
End Sub

'In Setup AddLooper(MyLoopingSub)

Sub MyLoopingSub
    If Millis()-initial1 > 2500 Then
        bState1 = Not(bState1)
        If bState1 = False Then
            'Set LED1 to off
        else
            'Set LED1 to on
        End If
        initial1 = Millis()
    End If
    If Millis()-initial2 > 1700 Then
        bState2 = Not(bState2)
        If bState2 = False Then
            'Set LED2 to off
        else
            'Set LED2 to on
        End If
        initial2 = Millis()
    End If
End Sub

This is the video I meant to post at first:

 
Upvote 0
Back
Top