Splitting a comma separated string through serial (Arduino)

Jacques Janse Van Vuuren picture Jacques Janse Van Vuuren · Apr 8, 2015 · Viewed 25.2k times · Source

So my arduino is receiving a string from serial, comprising of three values separated by commas, I'm trying to separate these values into three different variables, the rest I can do.

The string looks something like this "1000,1.5,0.9" or "5000,20,0.01"

I would like something like: a - 1000, b - 1.5, c - 0.9

Cheers

Answer

nirvana8510 picture nirvana8510 · Apr 8, 2015

I presume you are receiving the string that can be split in to three parts. Here's a sample code taken from this thread:

void setup(){
    Serial.begin(9600);
}
void loop(){
    String first  = Serial.readStringUntil(',');
    Serial.read(); //next character is comma, so skip it using this
    String second = Serial.readStringUntil(',');
    Serial.read();
    String third  = Serial.readStringUntil('\0');
    //parse your data here. example:
    //double x = Double.parseDouble(first);
}