Serial in/out communication w/ maxmsp

Hey,

I´m struggling to get a serial in and out communication between an arduino and max msp running.
I want to use it for a motor fader.

To send data from Arduino to MaxMSP I followed the SerialCallResponse example.

Solo, the other way around, Max2Ard, works also. Using this code:

int inByte = 0; // incoming serial byte
char buffer[40];
int index = 0;
int value;

void setup()
{
Serial.begin(9600);
pinMode(11, OUTPUT);
}

void loop()
{
index = 0;
do

buffer[index] = Serial.read(); // get a byte from the serial port
if (buffer[index]!=-1) index = index+1; // -1 if no byte is present

} while (buffer[index–1] != 32); //keep collecting bytes until a space is received

value = atoi(buffer); // interpret buffer string as an integer
analogWrite(11,value); // set brightness of an LED on pin 11
}

But when I combine the codes I cant get a steady stream from the analog inputs to max msp, while being able to set the fader by sending messages from max.

Can you please hint me in a direction how to approach this ?
I hope these bits of code can reveal a problem...

do/while has to be just about the most useless statement in c. There is NO reason to execute the body of the statement at least once if there is no serial data to read.

F**king around testing that you did not read a -1 instead of using Serial.available() is silly.

Assuming that whatever is in the serial buffer is a complete packet, and only a complete packet, is WRONG!

But when I combine the codes

someCode + 0 = someCode. No clue what you are talking about.

Have a look at the examples in serial input basics

...R