TX/RX problem. App to Arduino

This is part of my code that sends a value (3) to an Arduino. This turns on an LED and is working correctly.
The Arduino returns the text "LED ON" but the text does not show on my app unless I press the button twice.
Should there be a delay between send/receive? if so how would I do that?

Try this example:

Dear @Tony_Pagett,
sure you have to put a delay between Tx and Rx.
You can do it by means of a clock.
Try with a clock with 100ms period. Don't enable it until the button is pressed.
In the button block put the enable clock.
Remove from the button block the instruction of text typing and put it in the clock block.
Put an "if" instruction to compare the text received against the one you want to receive.
Disable the clock if the two texts match.
Change the delimiter byte to 13 (CR= use a Println() in your Arduino code to end the TX from Arduino to the app) and put the number of bytes to receive = -1.
I couldn't try it since I don't have an Arduino ready, anyway give it a look and a try (if you want).
Best wishes.
Pagett_Button.aia (3.0 KB)

This still does the same thing. The first button pressed does nothing then aftyer that all button presses are one step behind

Thank you for your reply.
I have only been using MIT for 2 days so I have no idea yet how I would add a clock etc as you suggest

Hi Tony, the annexed .aia contains all.
Have you tried it ?
Cheers.

So sorry. I didn't see that you had attached anything. I will give it a try and let you know how I get on

Keep your finger crossed, i'm keeping mine as well. :slight_smile:

It connects to my bluetooth and Arduino
When I press button 1 the Label2 goes blank and then the app freezes

I'm not sure what you mean here

The BT Tx from Arduino shall be ended by a Carriage Return Line Feed characters pair (CR = 13 in the ASCII table, and LF = 10 in the ASCII table).
If you send the string from Arduino by using:
Serial.println("LED ON"); //this means that the string is sent closed by the CRLF characters pair.
if otherwise you send the string just by using Serial.print("LED ON"); // this means that the string is sent without the CRLF characters pair.
In a nutshell:
in your Arduino code you shall use Serial.println("LED ON"),
on your app you should set the terminator character 13 (or 10) as I did in the aia designer page:
image
Ok I will put together and Arduino board and I try :slight_smile:
Keep in touch.

EDITED:
New aia after having put together an Arduino (Mega). Now it works :slight_smile:
I also changed the delimiyter to 10, in the case...

Pagett_Button.aia (3.2 KB)

The Arduino code is here (Arduino Mega and a HC06 @115200 baud on Serial2):
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

Serial2.begin(115200);

}

void loop() {
char trig;
// put your main code here, to run repeatedly:
if(Serial2.available())
{
trig = Serial2.read();
Serial.write(trig);
if (trig == '3')
{
Serial2.println("LED ON");
Serial.println("LED ON");
}
delay(10);
}

I have copied all that you sent and all I get is a blank yellow box in Label1 that flashes "Waiting Arduino" if I press button1

Please see the Delimiter article in FAQ

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.
BlueToothClient1_Properties
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.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Sorry. My mistake. I made an error on the baud rating.
All working fine.

Will I need a clock timer for every button (LED ON, LED OFF Etc)?

This should work.

Set TimerInterval to 200, and Enable it in Screen1 Initialize.

Mike.

No, just send different commands.

Like Send 2 = Led 1 ON, Send 3 = Led 1 OFF
Send 4 = Led 2 ON, Send 5 = Led 2 OFF

Mike.