1

I currently gather a string via bluetooth to my arduino and the string only has 5 different letters. "HELLO". I now wish to store these individual letters in a string on their own. Right now i have this code:

First I create a single string:

String readString;

Then I read my BTLEserial which is where I get the value that is being sent to the arduino and i store it in my string:

char c = BTLEserial.read();
readString += c;

readString now has the value "HELLO". Now i want to split it and what I have tried is to do it like this:

String stringOne = readString.substring (0,1); //H
String stringTwo = readString.substring (1,2); //E
String stringThree = readString.substring (2,3); //L
String stringFour = readString.substring (3,4); //L
String stringFive = readString.substring (4,5); //O

But with this i do not get the correct values in each string.

Appreciate every help I can get! :)

3 Answers 3

2

An array is 0 indexed, which means that number 5 is out of index. "Hello" should be index valued 0-4 and not 0-5.

Here is a better code to try out:

void setup() {
    Serial.begin(9600);
}

void loop() {

String recivedMessage = "";

// Read
while (Serial.available())
{
    recivedMessage += Serial.read();
}

// Print debug
Serial.print("recivedMessage: ");
Serial.println(recivedMessage);


//// Uncomment the row under if you are not sure that the splitting is working
// recivedMessage = "Hello";

// Split
char first  = recivedMessage.substring(0, 1)[0];    // H
char second = recivedMessage.substring(1, 2)[0];    // E
char third  = recivedMessage.substring(2, 3)[0];    // L
char forth  = recivedMessage.substring(3, 4)[0];    // L
char fifth  = recivedMessage.substring(4)[0];       // 0

// Print debug
Serial.print("Cut message: ");
Serial.println(first);
Serial.println(second);
Serial.println(third);
Serial.println(forth);
Serial.println(fifth);

}

11
  • Yea i changed that but it is not working very good. I think I need to create another solution without strings and use char instead. Do you have any example? Would appreciate it a lot
    – Martman
    Commented Mar 24, 2017 at 12:10
  • Just change Serial.available() and Serial.Read() to whatever channel(Bluetooth) you are listning to. I have tried this code and its working. If this doesn't work for you then you have a problem with the library or something else. This code is = true; =)
    – Sigma
    Commented Mar 24, 2017 at 12:37
  • I just tried it with my BLE and if i use the same type of code i only get numbers and not letters when I try this solution.
    – Martman
    Commented Mar 24, 2017 at 13:19
  • You are getting/reading bytes, thats how serial communication works, or is it binary? Anyway you need to translate it from byte to char and then concat to string. If you lookup that number on a ACSII table you will see every number correlate to that char ex. 80 is A
    – Sigma
    Commented Mar 24, 2017 at 14:58
  • 2
    Found an easy solution. Just put char before the read, it will cast it as char. Ex. (char)Serial.Read()
    – Sigma
    Commented Mar 24, 2017 at 15:05
0

I think it would be easier if you used a char array. Here's some sample code.

char recievedData[100]
int numChars = 0
//Now we are in the receiving loop
recievedData[numChars] = BLTEserial.read()
numChars++

Now that you have a character array with all of the members, you can simply use the numChars variable to figure out how much you got from the serial buffer (for example, using it as the upper limit for a for loop).

10
  • And to get the first letter out It gets stored here? recievedData[0]? I think I am missing the last part
    – Martman
    Commented Mar 24, 2017 at 12:11
  • Yes, it starts from index 0 and it will go to the counter - 1.
    – Tri
    Commented Mar 24, 2017 at 12:25
  • Also, you need to go out and learn the basics. Everything I just told you is within the material that you would learn in the first month of an intro to programming class.
    – Tri
    Commented Mar 24, 2017 at 12:40
  • it is weird because if i write it out like this: Serial.println (recievedData[0]); and check the serial monitor i see no letter but only blank space that gets recieved. I should get a "H" from that
    – Martman
    Commented Mar 24, 2017 at 12:55
  • What about when you print index 1?
    – Tri
    Commented Mar 24, 2017 at 12:56
0

String are the not the recommended way to use with Arduino although try using single parameter instead of two like substring(1) and let me know

char data[5];
for(int i=0;i <5 ;i++)
{
 data[i]=Serial.read();
}
char data1=data[0];
char data2=data[1];

so on and so forth ..

18
  • Yeah I guess I will need to create a solution without a string. Do you have quick examples I could see codewise? Would appreciate it a ton
    – Martman
    Commented Mar 24, 2017 at 12:11
  • You can simply declare a character array
    – varnit
    Commented Mar 24, 2017 at 12:56
  • ok and how do i get each character in the char? data[0] for the first letter ("H")? data [1] for the second ("E") etc?
    – Martman
    Commented Mar 24, 2017 at 13:02
  • which Bluetooth library you are using by the way ?
    – varnit
    Commented Mar 24, 2017 at 13:06
  • i am using a adafruit BLE and here is the codeexample: learn.adafruit.com/…
    – Martman
    Commented Mar 24, 2017 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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