This example is based on the connection diagram described here: https://www.b4x.com/android/forum/threads/rgb-leds.65998/
In this case we will control the led color with a B4J program.
The B4J program is based on serial connector: https://www.b4x.com/android/forum/threads/tool-external-serial-connector.65724/
When you use the serial connector you need to run the program from the IDE and the click on the Disconnect button in the logs tab to release the serial port. You can then connect to the serial port from the serial connector.
When you want to make an update to the program you need to disconnect the serial connector (close button).
The B4J program will send the color value to the Arduino which will then update the led.
B4R code:
This line is very important:
Messages can arrive together. This will happen when you quickly change the color in the custom colors dialog.
This 'for loop' will:
1. Handle all the arrived messages.
2. Ignore partial messages.
Setting the saturation and brightness to 100% yields good results. The led color matches the selected color.
Note: There is a bug in B4R 1.00 beta #6. It will cause additional random bytes to be added to the messages. It is fixed for the next beta.
In this case we will control the led color with a B4J program.
The B4J program is based on serial connector: https://www.b4x.com/android/forum/threads/tool-external-serial-connector.65724/
When you use the serial connector you need to run the program from the IDE and the click on the Disconnect button in the logs tab to release the serial port. You can then connect to the serial port from the serial connector.
When you want to make an update to the program you need to disconnect the serial connector (close button).
The B4J program will send the color value to the Arduino which will then update the led.
B4R code:
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private rpin, gpin, bpin As Pin
Private const COMMON_ANODE As Boolean = False
Private astream As AsyncStreams
Private bc As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
astream.Initialize(Serial1.Stream, "astream_NewData", "astream_Error")
astream.WaitForMoreDataDelay = 5
Log("AppStart")
rpin.Initialize(3, rpin.MODE_OUTPUT)
gpin.Initialize(5, rpin.MODE_OUTPUT)
bpin.Initialize(6, rpin.MODE_OUTPUT)
End Sub
Sub Astream_NewData (buffer() As Byte)
Log("Received: ", bc.HexFromBytes(buffer), ", len: ", buffer.Length)
For i = 0 To buffer.Length - 4 Step 4
If COMMON_ANODE Then
'common is +
rpin.AnalogWrite(255 - buffer(i + 1))
gpin.AnalogWrite(255 - buffer(i + 2))
bpin.AnalogWrite(255 - buffer(i + 3))
Else
rpin.AnalogWrite(buffer(i + 1))
gpin.AnalogWrite(buffer(i + 2))
bpin.AnalogWrite(buffer(i + 3))
End If
Next
End Sub
Sub Astream_Error
End Sub
This line is very important:
B4X:
For i = 0 To buffer.Length - 4 Step 4
This 'for loop' will:
1. Handle all the arrived messages.
2. Ignore partial messages.
Setting the saturation and brightness to 100% yields good results. The led color matches the selected color.
Note: There is a bug in B4R 1.00 beta #6. It will cause additional random bytes to be added to the messages. It is fixed for the next beta.