0

This question already has an answer here:

So for my arduino project I need to split this line of servos data

180 0 0 0 90 90 0

Which would be parsed as:

servo position1 servo positin2 servo positin3 ...servo position 6

in to separate servo values, but i am new at arduino and c typе languges so a little bit help will be very good. So my question is how can I split these values up?

marked as duplicate by gre_gor, sempaiscuba, VE7JRO, per1234, Greenonline Sep 4 '18 at 6:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Scanning string formatted data can be done with sscanf (docs). It takes format strings just like printf() does, but instead of printing formatted data, it parses formatted data into the given variables.

For example,

String input = "180 0 0 0 90 90 0";
//parse input (6 integers)
int servoPositions[6];
int numScanned = sscanf (input.c_str(),"%d %d %d %d %d %d", 
    &servoPositions[0], &servoPositions[1],&servoPositions[2],
    &servoPositions[3], &servoPositions[4],&servoPositions[5]);

//print positions again
Serial.println("Parsed servo positions:");

for(int i=0; i < 6; i++) {
    Serial.print("position ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(servoPositions[i]);
}

Outputs

Parsed servo positions:
position 0: 180
position 1: 0
position 2: 0
position 3: 0
position 4: 90
position 5: 90

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