from processing to arduino mega 2560 doesn't work

process code:

import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}
void draw() {
  if (mousePressed == true) 
  {                           //if we clicked in the window
   myPort.write('1');         //send a 1
   println("1");   
  } else 
  {                           //otherwise
  myPort.write('0');          //send a 0
  }   
}

arduino sode :

char val; // Data received from the serial port
 int ledPin = 13; // Set the pin to digital I/O 13
  void setup() {
   pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
   Serial.begin(9600); // Start serial communication at 9600 bps
 }
  void loop() {
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   if (val == '1') 
   { // If 1 was received
     digitalWrite(ledPin, HIGH); // turn the LED on
   } else {
     digitalWrite(ledPin, LOW); // otherwise turn it off
   }
   delay(10); // Wait 10 milliseconds for next reading
}

when i start on UNO, it does work. (mouse click -> LED on)

but when i start on MEGA 2560, it doesn't work. (no action -> LED on always)

so i types serial monitor 1 or 0, that work ON and OFF properly

why so from processing to arduino doesn't work?

problem on MEGA?

If it works from the Serial Monitor the Arduino code is fine. And it is very short and simple.

I don't know Processing. Is it possible it is not using the correct serial port?

...R

YES when i used UNO, i use corret serial port. so, only when mouse pressed, then LED light ON. LED is pluged arduino PIN 13. of course when no pressed, LED off.
but i use MEGA 2560, whenever mouse pressed or not, LED light on always after processing code upload.

  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port

How many ports are in the list? Is the Arduino actually connected to the first entry in the list?

If you switch boards, you also switch ports.