Setting XBee AT commands as background process?

Hi,

I am currently working on a project where I would like to define what XBee module to send GPS coordinates to. I am currently able to do this using the following code:

//Send the GPS coordinates via XBEE to other modules
void sendGPS(){
  boolean OK = false;
  char buf[10]; //buffer to hold the data being converted
  //determine which module this is
  if(thisModule == 'A'){
    if(mothershipNew){ //make sure new data has been recieved before printing
      while(OK == false){ //keep trying to get into command mode
        Serial.println("-----------------------");
        Serial2.print("+++"); //get into command mode
        delay(1200); //wait for command mode
        OK = false;
        while(Serial2.available() > 0){
          Serial.write(Serial2.read()); //print response
          OK = true; //once responce received, continue
        }
        if(OK){
          Serial.println();
          Serial2.println("ATDN RESCUE"); //configure to send to RESCUE
          delay(100);//wait for command to set
          while (Serial2.available() <= 0) {
            Serial.write(Serial2.read());   
            //get response
          }
          Serial.println();
          Serial2.write(thisModule); //Print the Module Identifier
          Serial2.write(44);
          Serial2.write(ltoa(latitudeMothership, buf, 10)); //send as array of char
          Serial2.write(44);
          Serial2.write(ltoa(longitudeMothership, buf, 10));
          Serial2.write(10);
        }
        delay(1000);
      }
    }
  }
  else if(thisModule == 'B'){
    if(rescueNew){ //make sure new data has been recieved before printing
      while(OK == false){ //keep trying to get into command mode
        Serial.println("-----------------------");
        Serial2.print("+++"); //get into command mode
        delay(1200); //wait for command mode
        OK = false;
        while(Serial2.available() > 0){
          Serial.write(Serial2.read()); //print response
          OK = true; //once responce received, continue
        }
        if(OK){
          Serial.println();
          Serial2.println("ATDN RESCUE"); //configure to send to RESCUE
          delay(100);//wait for command to set
          while (Serial2.available() <= 0) {
            Serial.write(Serial2.read());   
            //get response
          }
          Serial.println();
          Serial2.write(thisModule); //Print the Module Identifier
          Serial2.write(44);
          Serial2.write(ltoa(latitudeRescue, buf, 10)); //send as array of char
          Serial2.write(44);
          Serial2.write(ltoa(longitudeRescue, buf, 10));
          Serial2.write(10);
        }
        delay(1000);
      }
    }
  }
  else{
    if(victimNew){ //make sure new data has been recieved before printing
      while(OK == false){ //keep trying to get into command mode
        Serial.println("-----------------------");
        Serial2.print("+++"); //get into command mode
        delay(1200); //wait for command mode
        OK = false;
        while(Serial2.available() > 0){
          Serial.write(Serial2.read()); //print response
          OK = true; //once responce received, continue
        }
        if(OK){
          Serial.println();
          Serial2.println("ATDN RESCUE"); //configure to send to RESCUE
          delay(100);//wait for command to set
          while (Serial2.available() <= 0) {
            Serial.write(Serial2.read());   
            //get response
          }
          Serial.println();
          Serial2.write(thisModule); //Print the Module Identifier
          Serial2.write(44);
          Serial2.write(ltoa(latitudeVictim, buf, 10)); //send as array of char
          Serial2.write(44);
          Serial2.write(ltoa(longitudeVictim, buf, 10));
          Serial2.write(10);
        }
        delay(1000);
      }
    }
  } 
  
}

The problem with this code is that it is obviously hindering the rest of my code from executing by causing roughly a two to three second delay. This is due to it waiting for a response from the XBee stating that it got into the programming mode and then delaying to get out of the programming mode. NOTE: I have tried sending the XBee the command "ATCN" to drop out of the command mode to quicken the process, but this resulted in the XBee sending "ATCN" to the other XBee module. I also need to wait a second for the XBee to enter the programming mode according to another blog I read about this.

My question is, is there any way I could make this happen in the background because I need my arduino to update the motor controller I am going to implement? A two second delay would cause my robot to turn left and right uncontrollably instead of heading in a straight line.

I generally don't like to use delay functions in my code and would be more than happy to try another approach that doesn't involve waiting.

Thanks in advance

P.S.

A little more info on the project:
Each Arduino / XBee receives GPS coordinates which it then sends to the other unit. One unit is supposed to track and go to the other unit. I am using an Arduino Mega 2560 and and Arduino Uno (with SoftwareSerial).

Rather than delaying 1200 ms and then throwing away any response from the XBee, it would be better to read and process the response as it arrives until you have got a success or failure indication.

API mode. No waiting. Send the command, get the response within milliseconds.

If transparent mode is an absolute requirement (although I'm not sure I've seen one yet) I'd go with a state machine construct to keep track of the process.

(There's really not such a thing as a "background process" with a standard Arduino sketch.)

        Serial2.print("+++"); //get into command mode
        delay(1200); //wait for command mode

The switch to command mode is immediate (once the XBee actually receives the command).

Why do you need to reconfigure the XBee, anyway? What is it talking to at other times? Where do you switch it back?

Hey,

Thanks for the quick replies!

Rather than delaying 1200 ms and then throwing away any response from the XBee, it would be better to read and process the response as it arrives until you have got a success or failure indication.

I agree, but for now I am just trying to get this to work for now and then add the error checking.

API mode. No waiting. Send the command, get the response within milliseconds.

I am not quite sure what you mean by APi mode.. I did a quick google search and it states that you can broadcast to all xbees? What I would like to do is only broadcast to a specific XBee then swap to send to the other one later on. I am also trying to use point to point communication because it seems to be a lot faster in sending and I don't think there is as much data loss, though I am by far no expert with the XBees and am not quite sure if this is true.

Why do you need to reconfigure the XBee, anyway? What is it talking to at other times? Where do you switch it back?

I would like to send all the GPS Coordinates to one specific XBee module and this one module needs to be able to send information back to the sending modules individually as well. So I basically have:

XBee 1: Send GPS Coordinates to XBee 3
XBee 2: Send GPS Coordinates to XBee 3
XBee 3: Receive all messages and once within certain range to XBee 2 -> send message to XBee 2.

So summing up, most of the time it is only transmitting to XBee module 3. I suppose I could hav the program keep track of which module it is sending to and then only change it as needed. This should get rid of the timing issues.

Thanks again.

firestork:
I am not quite sure what you mean by API mode.

If I do: http://www.lmgtfy.com/?q=xbee+api+mode

The first link I get back is:
http://www.digi.com/support/kbase/kbaseresultdetl?id=2184

And the second is a very good library that helps a lot with API mode:

firestork:
I agree, but for now I am just trying to get this to work for now and then add the error checking.

Oh. I thought the whole point of your post was that it was taking too long. That was why I commented on the thing that was making it take too long.

The first link I get back is:
http://www.digi.com/support/kbase/kbaseresultdetl?id=2184

Thank you so much! I cannot believe I never saw that. I really need to do more research before I start programming. That is exactly what I need for my project. Now I just need to convert my GPS coordinates to Hex, but that should be simple.

Thanks again Jack.