CommunicationProblemd with 2 arduinos, 2 XBees and 2 XBee Shields


I am trying to send the analog data which one arduino reads from the joysticks to the other.
Reading the joystick and sending data looks good.
Receiving part also works but if statement goes wrong after receiving.
If receiving data is 504, the code needs to show ‘middle zone’ but it says ‘upper zone’.
Attachments are the snapshots of two parts operation.
Could you help me what is wrong in the following code?

  1. Transmitter code
// Reading analog value from Joystick

int JoyL_X_In = A8;
int JoyL_Y_In = A9;
int JoyR_X_In = A10;
int JoyR_Y_In = A11;
int JoyL_X_ADC, JoyL_Y_ADC, JoyR_X_ADC, JoyR_Y_ADC;
float JoyL_X_Vol, JoyL_Y_Vol, JoyR_X_Vol, JoyR_Y_Vol;

// Dummy values to send over serial
int value1 = 10;
int value2 = 100;
int value3 = 1000;

void setup() {
  pinMode(JoyL_X_In, INPUT);
  pinMode(JoyL_Y_In, INPUT);
  pinMode(JoyR_X_In, INPUT);
  pinMode(JoyR_Y_In, INPUT);

void loop() {

  // (1) Read Left Joystick X, Y
  JoyL_X_ADC = analogRead(JoyL_X_In);
  JoyL_Y_ADC = analogRead(JoyL_Y_In);
  // Serial.print("Joystick_Left_X_ADC : ");
  // Serial.println(JoyL_X_ADC);
  // Serial.print("Joystick_Left_Y_ADC : ");
  // Serial.println(JoyL_Y_ADC);

  // (2) Read Right Joystick X, Y
  JoyR_X_ADC = analogRead(JoyR_X_In);
  JoyR_Y_ADC = analogRead(JoyR_Y_In);
  Serial.print("Joystick_Right_X_ADC : ");
  Serial.print("Joystick_Right_Y_ADC : ");
  // (3) Convert ADC value of Left Joystick to Physical
  JoyL_X_Vol = (5.0/1023.0)*JoyL_X_ADC;
  JoyL_Y_Vol = (5.0/1023.0)*JoyL_Y_ADC;
  Serial.print("Joystick_Left_X_Voltage : ");
  Serial.print("Joystick_Left_Y_Voltage : ");
  // (4) Convert ADC value of Right Joystick to Physical
  JoyR_X_Vol = (5.0/1023.0)*JoyR_X_ADC;
  JoyR_Y_Vol = (5.0/1023.0)*JoyR_Y_ADC;
  Serial.print("Joystick_Right_X_Voltage : ");
  Serial.print("Joystick_Right_Y_Voltage : ");
 // delay(10);

 sendData(JoyL_X_ADC, JoyL_Y_ADC, JoyR_X_ADC, JoyR_Y_ADC);

void sendData(int JoyL_X_ADC, int JoyL_Y_ADC, int JoyR_X_ADC, int JoyR_Y_ADC){
 Serial.print('A');             // 'H' defines start of transfer
 // Serial.print(JoyL_X_ADC);
 Serial.print(JoyL_X_ADC,DEC); // First value to send
 Serial.print(",");            // Comma-separated
 Serial.println(" ");   
  1. Receiver code
unsigned int JoyL_X_ADC, JoyL_Y_ADC, JoyR_X_ADC, JoyR_Y_ADC = 0;
void setup()

void loop()
          // Serial.find is supposed to read from the serial buffer till it hits 'A'
          // parseInt reads the first number it encounters (long) until it hits a comma
          JoyL_X_ADC = Serial.parseInt();
          Serial.print("JoyL_X_ADC : ");
          if(562 <= JoyL_X_ADC <= 1023) // Forward
            Serial.println("Upper zone");
          else if(0 <= JoyL_X_ADC < 462) // Backward
            Serial.println("Lower zone");
            Serial.println("Middle zone");

Why don't you print out what Serial.available( ) has?

          if(562 <= JoyL_X_ADC <= 1023) // Forward

The result of comparing 562 and the value in JoyL_A_ADC will be either true or false.

Both true and false are less than 1023, so the statement will always evaluate to true.

That is NOT how to determine that a value is in a range. There are NO shortcuts.

if(JoyL_X_ADC >= 523 && JoyL_X_ADC <= 1023)

Of course, the value returned by analogRead() will ALWAYS be less than or equal 1023, so the second part of that test will always be true, making it useless.

Thank you Paul.
I haven't tried yet but after reading your comments, I realized that my expression inside if statement is like mathematical one NOT programming expression.
Let me try based on your advice.

Based on Paul’s comments, I corrected my code and tried again.
It worked.
However, I had another issue on received data.
Transmitting data is around 500 but receiving data is occasionally around 5 or 50 like attached.
Is this the problem of reading data in receiver part?

Is this the problem of reading data in receiver part?

Without seeing your code AS TEXT and the output AS TEXT, who knows?

Most likely it is an issue of not sending start and end of packet markers, and of not reading data, saving it when the start marker arrives, and using the saved data when the end marker arrives.