Serial communication issue with Arduino Mega

I've been trying to learn the basics in Processing to Arduino serial communication. The code works for my Arduino Uno, but when swapping boards to an Arduino mega (Needed more digital pins) I've had no luck. I've been thinking that there might be coding differences between the two boards, but I can't seem to find any solution to this problem online. Any help would be appreciated!

Heres the code for the Arduino Board

 char val; // Data received from the serial port
 
 int ledPin = 8; // 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
}

And for processing...

import processing.serial.*;
Serial myPort;



void setup (){
  
  size(200, 200);
  
  
  String portName = Serial.list()[0];
  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');  
  println("0");  //send a 0
  }   
}

I've had no luck.

What the does that mean!

Mark

Sorry that is a bit vague.

When connected via USB to the computer and attempting to change the LED state through processing, the LED is not seen to turn on when connected to digital pin 8 and ground, whereas in the same setup it would work for an UNO board.

Are you using the correct comm port? LED on pin 8? or 13?

Mark

Are you changing the board in the IDE > tools > board to the board you are using?

The LED is connected to pin 8 (Forgot to update the notes). I changed it from 13 because the state of this pin was always on even when I later changed the code to pin 8.

I think I am using the correct comm port as it is the same one used to change the LED state successfully on the UNO board. I have also tried changing the designated port to others in the processing code, but have not managed to change the led state successfully.

Yeah I've changed the board in tools to the "Arduino Mega or Mega 2560" option in arduino.

Where is your mega code? The mega has several serial ports, does your code take that into account?

Computers sometimes change the COMx number even if you plug it into the same socket on your computer.

Have a look at the examples in serial input basics. They are simple reliable ways to receive data.

...R

The mega code is what I have posted above, which worked successfully for the UNO board.

I initially thought the multiple serial ports was the issue, but the USB serial port is started and read from when refered to as serial. The other 3 available ports are named serial1, 2 and 3.

Also I introduced a 100ms delay in the processing code and observed the USB RX led blinking at the same rate, so assume processing is communicating with the right port?

for your reference

http://forum.arduino.cc/index.php?topic=52151.0