How do i control multiple pins using max/msp

I am using the arduino diecimila to turn an LED on and off. To do this im using a very simple max/msp patch thats sends a value of 154 into the serial object to turn the LED on and 0 to turn the LED off.

This works fine with this sketch loaded on to the arduino. int LEDPin = 9;

void setup()
  // begin the serial communication
  pinMode(LEDPin, OUTPUT);

void loop()
  byte val;

  // check if data has been sent from the computer
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255)
    val =;
    // set the LED on or off
    analogWrite(LEDPin, val);

this works absolutely fine but i want to be able to control more than one pin out from max, so i can control multiple LEDs independently. how do i go about this? how can i get the serial object to transmit separate data to each pin out on the Arduino?

sorry if this is a very simple question, thanks

You could add a second piece of data to your command. This way you could wait till you have 2 characters in the buffer, then read them both. The first char could be the pin ID and the second could be the value. You are close. Here is what I came up with.

I added some debug stuff so you can see what you are writing via the serial port.

byte LEDPin, val;      // for incoming serial data
int iNumBytes;

void setup() {
      Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps

void loop() {

      // send data only when you receive data:
        iNumBytes = Serial.available();
      if ( iNumBytes > 1) 
            // read the incoming bytes:
                LEDPin =;
                val =;

              analogWrite(LEDPin, val);
                Serial.print("I wrote LEDPin# ");
                Serial.print(LEDPin, DEC);
                Serial.print(" Value ");
                Serial.println(val, DEC);