Problem with Arduino mega 2560 serial port

I am using the arduino mega for the last three months and every thing is OK. But last week, I programmed the board to send data through the serial port. Programming done...but no data received from the board? (Using the serial monitor ) Then I changed the board with a new arduino mega.. The same problem ...no data received. The same program run very well on arduino uno. Please help me to solve the problem

Show your print statements. Perhaps you are outputting non-printable characters? Serial monitor is opened to the correct speed also?

Thank you… The program is long… The strange thing is that the same program run perfectly on the Arduino Uno and Arduino Due !!!
One more point… Some times if I reset the mega after the uploading it starts to send serial data

Muaiad:
Thank you… The program is long…

Without code is going to be extremely difficult to help you debug a problem with your code.

The program is long

Then you need to make simple code like below so the issue can be studied.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

/*Very simple serial test code. Copy and paste into the IDE code 
area. Upload via clicking the IDE upload arrow. When upload 
finished, open the serial monitor by clicking the serial monitor 
icon in the upper right of the IDE. Ensure 9600 baud rate is 
selected in the serial monitor. Type something in the serial 
monitor text box and send. What was typed in the text box should 
be sent back to the serial monitor display. */

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

Dear All…This is my program…As I said earlier it works fine with Arduino Uno and Due… but not with mega?
Thank you all.

Robot_Arduino_Servo_Speed_int_VAR.ino (3.7 KB)

                             Serial.flush();

Do you have a clue what this does?

99.9% of the uses of Serial.flush() are unnecessary and/or wrong. You are not the exception.

if (ServoNum ==5) // servo 6(hand)

six is not equal five

 void TurnServos(int ServoNum, int Speed, int toDegree)

Why is the function name plural? The function moves ONE servo.

What is on the other end of the serial port? Why are you not echoing what you receive, so you can know that communication happens at all?

Thank you all for you responses…
The problem still not solved… I have tried the Physical Pixel example from arduino

and same response…no communication between the arduino mega and the processing program.
this is the code:

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

/* Processing code for this example

 // mouseover serial 
 
 // Demonstrates how to send data to the Arduino I/O board, in order to 
 // turn ON a light if the mouse is over a square and turn it off 
 // if the mouse is not. 
 
 // created 2003-4
 // based on examples by Casey Reas and Hernando Barragan
 // modified 30 Aug 2011
 // by Tom Igoe
 // This example code is in the public domain.

 import processing.serial.*; 
 
 float boxX;
 float boxY;
 int boxSize = 20;
 boolean mouseOverBox = false;
 
 Serial port; 
 
 void setup()  {
 size(200, 200);
 boxX = width/2.0;
 boxY = height/2.0;
 rectMode(RADIUS); 
 
 // List all the available serial ports in the output pane. 
 // You will need to choose the port that the Arduino board is 
 // connected to from this list. The first port in the list is 
 // port #0 and the third port in the list is port #2. 
 println(Serial.list()); 
 
 // Open the port that the Arduino board is connected to (in this case #0) 
 // Make sure to open the port at the same speed Arduino is using (9600bps) 
 port = new Serial(this, Serial.list()[0], 9600); 
 
 }
 
 void draw() 
 { 
 background(0);
 
 // Test if the cursor is over the box 
 if (mouseX > boxX-boxSize && mouseX < boxX+boxSize && 
 mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
 mouseOverBox = true;  
 // draw a line around the box and change its color:
 stroke(255); 
 fill(153);
 // send an 'H' to indicate mouse is over square:
 port.write('H');       
 } 
 else {
 // return the box to it's inactive state:
 stroke(153);
 fill(153);
 // send an 'L' to turn the LED off: 
 port.write('L');      
 mouseOverBox = false;
 }
 
 // Draw the box
 rect(boxX, boxY, boxSize, boxSize);
 }

 */

no communication between the arduino mega and the processing program.

What version of Processing on what operating system?

I am using Processing 2.0.3.

and I have tried it on window XP and Window 7

You might try the 1.5.1 version of Processing.

I will try.... But why the same program run perfectly when I replace the arduino Mega with UNO or DUE!!!!!!!!!!!!!

Are you changing the to the appropriate com port in Processing?

The obvious difference between uno and mega are the pin numbers. I'd start looking at that.

Does this program work on your Mega?

void setup() {
   Serial.begin(9600);
}

void loop() {
   Serial.println("Hello World");
   delay(1000);
}

...R

Yes it work...

[quote author=James C4S link=topic=225134.msg1639611#msg1639611 date=1395154283] Are you changing the to the appropriate com port in Processing? [/quote] Yes

The Arduino code in your Reply #7 works fine on my Mega

...R

Apparently I have the same problem as you, but might differ abit.

There seems to be no change in whether my LED is ON or OFF when i move over the square, although the RX lights seems to be flickering like a wild beast.