Hello,
Im sending comma separated values as a string to arduino from a bluetooth with a "!" at the beginning of each string. So far with this code im able to put them in an array, but i have 2 problems.
First one is that these 3 values that im sending as a string, can be negative or float number so the incoming string length is changing all the time. Ive tryed this code with a 10 character length but its not working ok.
blueToothSerial.flush();
char input[10];
memset(input, '\0', 10);
byte inByte = '\0';
while(inByte != '!') {
inByte = blueToothSerial.read(); // Wait for the start of the message
}
if(inByte == '!') {
while(blueToothSerial.available() < 10) { // Wait until we receive 5 characters
;
}
for (int i=0; i < 10; i++) {
input[i] =blueToothSerial.read(); // Read the characters into an array
}
}
Second that i dont have any idea how to do it is that i want to split this string in to 3 different variables..
First one is that these 3 values that im sending as a string, can be negative or float number so the incoming string length is changing all the time. Ive tryed this code with a 10 character length but its not working ok.
You'll need to determine a maximum length string that you can handle. Suppose all the values are floats, all negative, and all as large as possible. Each number can meaningfully have a sign, some number of digits before the decimal point (does 8 seem reasonable?), a decimal point, and some number of digits after the decimal point (only 7 have any hope of being accurate). So, allow up to 20 characters per number, including the comma, so your array should be 60 characters long.
If you have any control over the sending end, and can limit the number of decimal places, or know that the maximum absolute value of the number will be less than 8 digits, you can make the array smaller.
while(blueToothSerial.available() < 10) { // Wait until we receive 5 characters
I love it when the comments match the code.
Second that i dont have any idea how to do it is that i want to split this string in to 3 different variables..
A little research on the strtok() and atof() functions is in your future, then.