sending data back and forth between xbees

I am using two xbees each connected to an Arduino Pro Mini. At the beginning of my program, I want to send an accelerometer value from the first xbee to the second xbee, and then immediately from the second xbee to the first. I’m not sure the best way to do this to avoid timing issues. The communication worked when I had one xbee only transmitting the data and the other one receiving.

My strategy: set a variable k = 1 initially, and the do…while loop containing the transmit call runs until k =0, which is transmitted to it at the end of the receive subroutine on the receiving xbee. This isn’t working and I tried to debug, but for some reason Serial.print commands didn’t work while the other xbee was transmitting, and the Serial.println(n) did not print anything after n = receive(). Can you see what I did wrong or suggest a better way to send data back and forth?

Thank you!

Here is the code the first xbee uses, the second xbee uses the same code except it transmits before receives:

void loop()

{

int k = 1;
n = receive();
Serial.println(n);
do{
transmit();
if (mySerial.available())
{k = mySerial.read();}
}while (k==1);

//rest of my code.

}

void transmit(){

pulseX = pulseIn(xPin,HIGH);

mySerial.print(pulseX);

mySerial.print(“,”); // comma delimiter between numbers

delay(100);

}

uint16_t receive(){

uint16_t n = 0;

char c;

while (!mySerial.available()) ; // wait for a character…

do

{

if (mySerial.available())

{

c = mySerial.read(); // get the next character

if (c != ‘,’)

{

n *= 10; // multiply higher order digits

n += atoi(&c); // add lower order digit, atoi converts string to integer

}

}

}

while (c != ‘,’); // we have the whole number

mySerial.print(0); //indicate to other xbee that data has been received

return n;

}