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).