0

OK, I'm working on a remote controlled robot. Both the robot and the remote use Arduino with a Bluetooth shield. The Bluetooth is working and I need to send something similar to this over Bluetooth:

X:1026,Y:1026,Button:1

There may be more buttons added and X,Y can be between 1026 and 0. How can I build that string from:

xPosition = 1026; yPosition = 1026; buttonState = 0;

I also need to be able to separate that string back into variables on the 2nd Arduino.

EDIT: I have seen other questions similar to this but they involved Serial. I am using Bluetooth.

EDIT AGAIN: I am sending the data over Bluetooth with the BlueToothSerial.print command from the SoftwareSerial.h library. (BlueToothSerial is SoftwareSerial BlueToothSerial(RxD,TxD);)

  • Have a look at the Serial.parseint function and format your output and input so the come in correct order. – Hans Neve Mar 26 '16 at 15:32
  • You do realise, don't you, that the fact that you're using Bluetooth is a complete red-herring? It is just being used as a bridge between two serial ports. What you read pertaining to Serial is the same as your bluetooth. – Majenko Mar 26 '16 at 16:23
  • Indeed, serial-interfaced Bluetooth is just serial... with one catch for the unwary. Because the bluetooth portion will be packetized, data may arrive at the far end in bursts, potentially with gaps between them at arbitrary points, such as the middle of a word rather than in any logical place. Well written receiving software won't care about that, as it should read across delays until finding a known end-of-message condition such as a newline character. But badly written software that makes invalid assumptions (such as assuming that a full message will be received at once) could be confused. – Chris Stratton May 25 '16 at 17:48
0

To make the string you can:

char outstring[30];
sprintf(outstring, "X:%d,Y:%d,Button:%d", xPosition, yPosition, buttonState);

Be sure to make the outstring big enough to store it all.

That is, of course, if you actually need to have the whole thing in one string all the time. You haven't said how you are sending it (yes, I know, bluetooth, but there is bluetooth and there is bluetooth, and there are different ways of it working.)

Splitting a string like that can be done with strtok() then looking for the location of the colon in each returned token. Or, depending on how the string data actually arrives, could be parsed on a character-by-character basis, which would be better. Again, because you haven't told us how the data is actually being transferred it's impossible to elaborate on that.

  • I edited the post with how I am sending the data. I can send it separately and receive it as one string, but I don't know how too separate the string into multiple variables. – Starwarsfan2099 Mar 26 '16 at 16:20
  • @Starwarsfan2099 Majenko hinted on using strtok() which is a C++ function and is well documented: cplusplus.com/reference/cstring/strtok you might also use the Arduino indexOf function to find the comma's "," in your text string, see this tutorial: arduino.cc/en/Tutorial/StringIndexOf – Paul Apr 26 '16 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.