I am using AI2 for the first time. The app connects to a Bluetooth device, and I want it so that Button1 sends the letter M when I click it (this works), and then displays the value that is returned in Label3 (this does not work, nor does it display the test text "Received".
I am assuming the part where I call BluetoothClien1.ReceiveText needs to be in a clock, but I can't figure out how to use two clocks simultaneously.
Think of your Bluetooth signal as a water pipe - water can only travel in one direction at a time.
You can't send and receive in the same instance of time, so separate those - use a Clock Timer for receive and set it to at least 1 second (1000 milliseconds).
Thanks very much for that input. I couldn't work out how to attach an image but you gave an explanation that I will use next time.
I am used to procedural programming rather than event based, so I didn't think to include the ReceiveText into the Timer block. I will give that a go later today.
All the Label3 stuff is me just putting in words so I can test / see what is happening, but I take your point.
I am very much enjoying this learning journey, especially at my ripe old age haha.
Thanks again for your help, but I am clearly misunderstanding what you are suggesting I need to do.
I tried putting the ReceiveText into the existing Clock1 block. That didn't work. I created a second Clock1 block, but Ai2 complained about having two handlers. So I created a second Clock (Clock2) and put the ReceiveText in there, but the program just hangs.
Where did I go wrong? (Hopefully I have managed at least to upload an image of my project this time )
Boy, am I finding this frustrating. I just can't get this clock/BT stuff sorted propertly.
When I disable the Clock2 block that receives the text from the Arduino, I can use the Button (MeasureButton) and it sends the appropriate text (M) to the Arduino. The Arduino responds and sends some text back, but clearly MeasureStatusLabel does not update as it is in a disabled block.
When I enable the Clock2 block, the MeasureButton does not even fire, i.e. MeasureStatusLabel does not update to Sent and the Arduino does not receive anything.
I am so confused by this and have been trying for hours and hours to work this out.
Standard advice:
Be sure to use println() at the end of each message to send from the sending device, to signal end of message.
Only use print() in the middle of a message.
Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.
Do not rely on timing for this, which is unreliable.
In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.
In your Clock Timer, you should check
Is the BlueTooth Client still Connected?
Is Bytes Available > 0?
IF Bytes Available > 0 THEN
set message var to BT.ReceiveText(-1)
This takes advantage of a special case in the ReceiveText block:
ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.