Problem with Arduino Mega Serial

I am interfacing two Megas with each other. When there is no data available at Serial1 port i want
my function getdata(), to work which is working fine but when i recieve a character 'M' i want my getdata1(), to work until i recieve a character 'W'.

The problem i am facing is whenever i recieve a char 'M' it goes to getdata1(), function but does not wait for 'W' to recieve and goes to getdata() again. Any help would be appreciated.

void loop() {
selectionmode();
danish();

//=============

}
void selectionmode() {

while(Serial1.available() == 0){
getData();

}
}
void danish(){
if(Serial1.available()>0) {
readInProgress2 = true;
char x = Serial1.read();
if (x == 'M') {

getData1();

}

while (x == 'W'){

getData();

}

}

if (readInProgress2){
readInProgress2 = false;

}

Danish_Javed:
when i recieve a character 'M' i want my getdata1(), to work until i recieve a character 'W'.

Your code only calls "getdata1()" once when a "M" is received. In there isn't any data in the Serial1 buffer, "getdata1()" won't get called again.

We really need to see your full code to help.

Please read the post "How to use this forum - please read". Item #7 deals with how to post code. Item #10 deals with using proper sentences (you're almost there, I'm being nit picky (hint "i" isn't a word)).

while (x == 'W'){

Do you mean != ?

What about changing the code in loop() so it is like this

void loop() {
       selectionmode();
       danish();
       getData();
       getData1();
}

and in each of the getData functions you could have code like

void getData() {
   if (command == 'W') {
      // all the other function code
   }
}

In that way your receiving code just needs to store the value into a variable called command (or whatever name you prefer).

...R

I have two Arduino Mega's communicating with each other. When there is no data available at Serial1 port i want my getData() function to work which is working fine. When i recieve a 'M' at Serial1 port i want my getdata1() function to work until i recieve a 'W' and get back to getdata() function.

The problem i am facing is that when Serial1 port recieves a 'M' it goes to getData1() function but does not wait for 'W' and goes back to the getData() function as the loop ends.lp wou

Any help would be appreciated.

void loop() {
selectionmode();
danish();
}
void selectionmode() {

while(Serial1.available() == 0){
getData();

}
}
void danish(){

if(Serial1.available()>0) {
readInProgress2 = true;
char x = Serial1.read();
if (x == 'M') {
getData1();
}

while (x == 'W'){
getData();

}
}

if (readInProgress2){
readInProgress2 = false;

}

}

Danish_Javed:
Any help would be appreciated.

We would appreciate it if you would take the trouble to read the Replies you have already been given.

...R

The problem is when a 'M' is recieved it goes to getdata1() function just once and then the char 'M' automatically gets flushed i guess and when the loop runs again it doesn't finds any data at serial1 port and goes to getdata() function.

i just want that when 'M' is recieved it goes to getData1() function every time until and unless a char 'W' is recieved.

when no data is recieved or 'W' is recieved getdata() function should work and getdata1() function should only work when a char 'M' is recieved till a char 'W' brings getData() function back again.

Danish_Javed:
i just want that when 'M' is recieved it goes to getData1() function every time until and unless a char 'W' is recieved.

I believe my suggestion in Reply #3 will go a long way to meeting your requirement.

...R