Thanks for all the replies!
I ended using Arrch's approach,with a bit of tweaking (less code and achieves exactly what I am looking for).
Here is the updated code:
//receive 5 numbers via serial. First 2 numbers are the servo number, Second 3 numbers are the servo position
char inData[5]; // Allocate space for incoming serial data (2 digit servo number, 3 digit position)
char inByte; // Incoming serial character
byte index = 0; // Index into array; where to store the character
int8_t servo_num;
uint8_t servo_position;
char pos[4];
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0){
inByte = Serial.read (); //read serial input one byte at a time
inData[index] = inByte; //add serial input to array
index++; //increment index (length/bytes of serial data received)
if (index > 4 || inByte == 10 || inByte == 13){ //serial data is 5 bytes longs or received a charage return or line feed
inData[index] = '\0'; //Null terminate the string
index = 0;
servo_num = getServoNum();
servo_position = getServoPos();
Serial.println(servo_num);
Serial.println(servo_position);
}
}
}
int getServoNum(){
char num[3];
strncpy(num, inData, 2);
num[2] = '\0';
return(atoi(num));
}
int getServoPos(){
char pos[4];
strcpy(pos, inData+2);
pos[3] = '\0';
uint8_t t_pos = atoi(pos);
if (t_pos > 180){
t_pos = 180;
}
return(t_pos);
}