Communication between Arduino+XbeeProS1 and Arduino+XbeeProS1 -

Hello Guys,

I’m trying to make 2 arduinos with Xbee Pro S1 talk each other.
Actually achieved good results, although I can’t figure out the reason for some “weird” things.

Code on Transmitter

int axisRoll = 0;
int axisPitch = 1;

const int ez_left = 7; 
int rollVal;
int pitchVal;

long pulse_left, cm_left;

void setup() {
  Serial.begin(9600);
  
  pinMode(axisRoll, INPUT);
  pinMode(axisPitch, INPUT);
  pinMode(ez_left, INPUT); 
}

void loop() {
  rollVal = analogRead( axisRoll );
  rollVal = map( rollVal, 305 , 600 , -90 , 90 );
  pitchVal = analogRead( axisPitch );
  pitchVal = map( pitchVal, 308, 603 , -90 , 90 );

  //  Left Sensor
  pulse_left  = pulseIn(ez_left, HIGH);
  cm_left     = ( pulse_left/147 ) * 2.54;
 
  Serial.println( "<"+String ( rollVal ) + "," + String ( pitchVal )  + "," + String ( cm_left )+ ">" );
  delay(200);
}

Code on Receiver:

char inData[50];
int index = 0;
int busy = 0;
char messageFrag;
int header = 0;
String message;

void setup() {
  Serial.begin(9600);
}

void loop() {
    if ( Serial.available() > 0 ) {
        messageFrag = Serial.read();
     
        if ( header  == 1 ) {
            inData[index] = messageFrag;
            index++;    
        } else {
            if ( messageFrag == 60 ) { // beggining of the message
                header = 1;     
            } else if ( messageFrag == 62 ) { // end of the message
                header = 0; 
                printPacket();
            } else {       
                inData[index] = 0;
                index++;
            }
        }
    }
}

void printPacket() {
    for (int i = 0; i < 50; i++) {
      if ( inData[i] != 0 && inData[i] != 60 && inData[i] != 62 ) {
        Serial.print( char ( inData[i] ) );
      }
    }
    Serial.println("");
    index = 0;
    inData[index] = 0;    
}

What I expect to receive at each 200ms is something like:
10,10,165

But instead, I’m receiving 4 messages followed by a waiting of 1 second approximately:
10,10,165
10,10,165
10,10,165
10,10,165

I can’t understanding why it’s not running “smooth” so any help would be really appreciate.

Thanks

  Serial.println( "<"+String ( rollVal ) + "," + String ( pitchVal )  + "," + String ( cm_left )+ ">" );

Complete waste of resources. There are 7 calls to the constructor of the String class, and 5 calls to the copy constructor and 14 calls to the destructor FOR NOTHING. The XBee can’t tell that you didn’t do:

  Serial.print( "<");
  Serial.print(rollVal);
  Serial.print(",");
  Serial.print(pitchVal);
  Serial.print(",");
  Serial.print(cm_left );
  Serial.println(">" );
            if ( messageFrag == 60 ) { // beggining of the message

Is this somehow more readable to you than:

            if ( messageFrag == '<') { // beggining of the message
      if ( inData[i] != 0 && inData[i] != 60 && inData[i] != 62 ) {

You never (incorrectly) write a NULL, a ‘<’ or a ‘>’ to inData, so checking for them is silly.

If you DID properly write the NULL (after each character), you could simply:

        Serial.print(inData);

No loop needed, and no silly cast of a char to a char.

What I expect to receive at each 200ms is something like:
But instead, I’m receiving 4 messages followed by a waiting of 1 second approximately:

Sounds like a configuration issue. It’s not a programming issue.

What are you using the data on the receiver for? Is the timing critical?