Hi all,
I am working on an app and am needing some help. I am receiving arduino data from an ultrasonic sensor. I am wanting to be able to display a message based on the values coming from the bluetooth, mm in this case.
I have been able to use the If/then statement to be able to display if the value is less/greater than a value, for example, greater than 30 mm displays "TOO LOW" and above 30mm displays "TOO HIGH" , however, I want to have a range where it is "IN RANGE".
This is what I'm trying to accomplish.
<30mm = TOO LOW
31mm - 60mm = IN RANGE
Be sure to use println() at the end of each message to send from the sending device, to signal end of message. 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.
So be sure to have a global variable available to catch the ReceiveText result, and use that global variable value for your range tests, otherwise you will be sucking different values for rach test phase.
To check for a stack of ranges, do it in strict ascending or descending order, using the if/then/elseif/else block from the control blocks. (You can extend an if/then block via its blue mutator button per taste.)
if value > 60 then too high
else if value > 30 then in range
else too low
Because the tests are sorted, and in the same if/then/elseif tree, they don't need to be repeated.
Hi Abraham! Thank you so much for your response. I have completed the steps, but I still believe that something is not quite right. I have added the variable as well as verify the delimiter value is correct, as well as the println on the arduino, but I am receiving an invalid argument on the app for the first bluetooth argument (distance over 60)
I apologize if is something simple I have overlooked, but I certainly appreciate the information you have given me already and any further assistance you provide. I am including a screenshot of the error I am receiving, as well as the code for my application as it is currently configured.
That worked! I am now able to get the values to show TOO LOW, JUST RIGHT, and TOO HIGH. I was also looking at a couple other threads on enhancements to the app to help troubleshoot issues. 1) Display "NO INPUT" when numbers are not being sent by the arduino device (to represent when a wire is unplugged, problem with arduino code, etc) and also to indicate when the bluetooth is disconnected.
I followed the instructions to add the "Else statement" to the number check, but I think I am not doing something right.
For the bluetooth, I have read and implemented the instructions that was posted on this link, but the app never seems to refresh and error out when I unplug the arduino and bluetooth device.
I'm attaching my latest code for any additional assistance you are willing to provide.
I did not touch your Clock, which is too fast, at 1 ms per cycle.
A more reasonable rate would be 30 ms.
Also, clearing your output if the source was too slow for you is unreasonable,
if you are going to clear your label before people can read it.
Better to leave the last output there for people to read.
I added a 2 second timeout for detecting loss of input. OT_RV_R1_ABG.aia (34.6 KB)
I've been looking for some guidance on clock rates to check incoming bluetooth messages. This is the best I've found so far.
Currently I'm sending data from an ESP32 at a rate of 5000ms. However that's a one-way street and I'm soon going to be sending messages back and forth, so I'm going to be reducing that 5000ms to something much less.
I'm looking for some guidance on this other than the clock rate needing to be less than the ESP32 send rate.
Do the rates have much of an impact on processsing/power consumption?
I could trial and error some of this, but would be helpful to start somewhere other than square 1.