So i'm trying to get serial data from a bluetooth device and then get 3 variables out from it. For the sake of debugging i'm trying to make it work with standard Serial over usb first. My input is in the following format "3,4,6" where 3 4 and 6 each represent separate variables that need to be stored as doubles and is separated by the "," as the delimiter. I put together the following code from looking at examples here and there on the forum.
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
char delimiters[] = ",";
char* valPosition;
double angle[] = {0, 0, 0};
void setup() {
Serial.begin(9600);
}
void loop() {
readserialtochars();
parsedata();
}
void readserialtochars() {
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
}
void parsedata() {
if(started && ended)
{
valPosition = strtok(inData, delimiters);
for(int i = 0; i < 3; i++){
angle[i] = atof(valPosition);
Serial.println(angle[i]);
valPosition = strtok(NULL, delimiters);
}
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
The problem i'm getting is when i send a in the following in the serial comm "3,4,6" nothing is outputted where i should get my 3 values in individual lines.
This seems to be something that is required often, is there any sort of library or function inbuilt into the arduino library that handles this?