Need suggestion on changing in Code for while (Serial.available ( ) )

Background: I am working with Node A [Matlab+Xbee (S1)] in one side and Node B [Arduino UNO+Xbee(S1)] on another side. I am trying to send one message [message 1] from Node A to Node B, Node B would get the message1, read it and send another message [message 2] to Node A. This concept is working properly.

My requirement: In this above case I am getting message only when I am sending a message to Node B. The reason is very simple as my program is working by 'while (Serial.available())'. But I want (even when I send only one message to Node B) that Node B will not stop sending the message after once, rather it would continuously send Node A the message it sent the first time till I send another message [message 3] from Node A to Node B.

My question: Need your suggestion on how should I change the code so that it would continuously send the 1st message while it would receive value as 100. So whenever Node A again would send value 101 it would start sending continuously the 2nd message (i.e. Sending the Speed data)?

Need your suggestion on how should I change the code

Well, I tried
reading your code
but I got tired of
trying to figure
out what code
went with what other code.

There is a reason that the Tools menu has an Auto Format option. Learn what that reason is, and try again.

You need to conceptually separate receiving from sending. Read up about State Machines.

Your code should be something like this pseudo code

int codeRecived;

void setup() {
}

void loop() {
   readStuffFromSerial();

   actOnValueOfCodeReceived();

}

void readStuffFromSerial() {
   codeReceived = 100 unless something else is received; 
}

actOnValueOfCodeReceived() {
  if codeReceived == 100 do stuff;

  if codeReceived == somethingElse do other stuff;
}

...R

Robin2:
You need to conceptually separate receiving from sending. Read up about State Machines.

Thanks for your answer, but I need the sending function to work without getting any data in receiver once it gets a data. But your mentioned code works each time it receives data in serial port.

My code will work even if nothing is received if the function readStuffFromSerial() is coded correctly. It defaults to 100.

...R