help needed arduino write and read through max msp

hello i am trying to modify a code in arduino .... i want to be able read analog pins (that is ok) and to write on digital pin (this where i need help) ... everything has to run through max msp

if somebody is patient maybe he could describe a bit why it does not work

here is what i modified... (oh yeah ... i am new to all this)

/*

  • Arduino2Max
  • Send pin values from Arduino to MAX/MSP
  • Arduino2Max.pde

  • This version: .4, October 2007

  • Copyleft: use as you like
  • by Daniel Jolliffe
  • Based on a sketch and patch by Thomas Ouellet Fredericks tof.danslchamp.org

*/

int x = 0; // a place to hold pin values
int ledpin = 13;

void setup()
{
Serial.begin(115200); // 115200 is the default Arduino Bluetooth speed
digitalWrite(13,HIGH); ///startup blink
delay(600);
digitalWrite(13,LOW);
pinMode(13,INPUT);
}

void loop()
{

if (Serial.available() > 0){ // Check serial buffer for characters

if (Serial.read() == 'r') { // If an 'r' is received then read the pins

if (Serial.write() == 'w') {

for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5
x = analogRead(pin);
sendValue (x);
}

for (int pin= 2; pin<=7; pin++){ // Read and send digital pins 2-7
x = digitalRead(pin);
sendValue (x);
}

for (int pin= 8; pin<=12; pin++){ // write and recieve digital pins 8-12
y = digitalwrite(pin);
receiveValue (y);
}

Serial.println(); // Send a carriage returnt to mark end of pin data.
delay (5); // add a delay to prevent crashing/overloading of the serial port

}

}
}

void sendValue (int x){ // function to send the pin value followed by a "space".
Serial.print(x);
Serial.print(32, BYTE);
}

Any luck? I am also haing troubles with arduino and mas msp. I know my board is working but I cannot get any information into max msp.

eo