Xbee and arduino serial monitor printing error

Hello!

I have two arduinos connected to two Xbees series 2 using Xbee shield from SainSmart.

One is configured to coordinator and the other configured to end device.

Project first part is to have the coordinator to send a command to turn on a relay in the end device using Xbee Transmit Request. This part is working properly.*

Second part is to receive from the end device a digital value from a sensor and display in the serial monitor of the coordinator. This part is also working properly.

However, when I include both parts in the same code the serial monitor prints random characters that impair the proper sensor reading.

  • Although it is working, the coordinator serial monitor always prints random characters.

CODE:

int input_1 = 10;
int readValue= 0;
void setup() {
Serial.begin(9600);

pinMode(input_1,INPUT);//LED will work as a switch. If it’s on=1 send the message to turn on, if it’s off=0 send the mesage to stay off

}

void loop() {
//------------------------------SENSOR READING---------------------------------------

if(Serial.available()>21){
if(Serial.read()==0x7E){

for(int i=0;i<20;i++){
byte discardByte=Serial.read();
}
int readValue=Serial.read();

if(readValue==0){
Serial.print("\n");
Serial.println(“Okay”);
Serial.print("\n");
} else if(readValue==16){
Serial.println(“It’s Dark. Turn Lights ON?”);
Serial.print("\n");
}
}
}

//------------------------------CONTROL LOAD---------------------------------------
Serial.write(0x7E);//start byte

Serial.write(0x00);//lenght
Serial.write(0x10);

Serial.write(0x10);//frame type and id
Serial.write(0x01);

//64 bit destination address
Serial.write(0x00);//serial high
Serial.write(0x13);
Serial.write(0xA2);
Serial.write(0x00);

Serial.write(0x41);//4184D035
Serial.write(0x84);
Serial.write(0xD0);
Serial.write(0x35);

Serial.write(0xFF);//16 bit address of destination device(broadcast)
Serial.write(0xFE);

Serial.write(0x00); //broadcast radius → set to maximum hops value
Serial.write(0x00); //options → unused bits=0

int val_1=digitalRead(input_1);

if(val_1== 0){

Serial.write(0);
Serial.write(0);
Serial.write(0x72); //checksum
}else {
Serial.write(0);
Serial.write(1);
Serial.write(0x71);//checksum
}

delay(1000);

}

Thank you!