Arduino XBee Serial Communication

I want to control DC motors using Arduino Uno. The controls are provided by XBEE S2 through Tx-Rx pins of Uno. The logic works fine when a Serial Monitor is launched on a connected laptop. But UNO does not respond till a serial monitor is launched in GUI. Kindly suggest how to make UNO receive data without launching a serial monitor.

Code is as follows :

int LeftMotorForward = 10; // Pin 10 has Left Motor connected on Arduino boards.
int LeftMotorReverse = 9; // Pin 9 has Left Motor connected on Arduino boards.
int RightMotorForward = 8; // Pin 10 has Left Motor connected on Arduino boards.
int RightMotorReverse = 7; // Pin 9 has Left Motor connected on Arduino boards.
int EnableLeft = 6; // Pin 9 has Left Motor connected on Arduino boards
int EnableRight = 5; // Pin 9 has Left Motor connected on Arduino boards

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LeftMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(LeftMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(EnableLeft, OUTPUT); // initialize the pin as an output.
pinMode(EnableRight, OUTPUT); // initialize the pin as an output.
// sleep_disable();
}

void loop() {

//Serial.write(0);
if (Serial.available() >= 25) { // Make sure the frame is all there
if (Serial.read() == 0x7E) { // 7E is the start byte
for (int i = 1; i<19; i++) { // Skip ahead to the analog data
byte discardByte = Serial.read();
}

analogWrite(EnableLeft, 180);
analogWrite(EnableRight, 180);
int analogMSBX = Serial.read(); // Read the first analog byte data
int analogLSBX = Serial.read(); // Read the second byte
int analogMSBY = Serial.read(); // Read the first analog byte data
int analogLSBY = Serial.read(); // Read the second byte
int analogMSBZ = Serial.read(); // Read the first analog byte data
int analogLSBZ = Serial.read(); //Read the second byte

int analogReadingx = analogLSBX + (analogMSBX * 256);
//tempx = analogReadingx / 1023 * 1.2;
int analogReadingy = analogLSBY + (analogMSBY * 256);
//tempy = analogReadingy; // 1023 * 1.2;
int analogReadingz = analogLSBZ + (analogMSBZ * 256);
//tempz = analogReadingz * 1.2;

Serial.print(analogReadingx);
Serial.print("\t");
Serial.print(analogReadingy);
Serial.print("\t");
Serial.println(analogReadingz);

if( analogReadingx > 740 & analogReadingx <999 )
{
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorReverse,LOW);
}

else if (analogReadingx < 580 & analogReadingx > -999)
{
digitalWrite(LeftMotorReverse,HIGH);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorReverse,HIGH);
}

else if (analogReadingy < 580 & analogReadingy > -999)
{
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(LeftMotorForward,HIGH);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorReverse,HIGH);
}
else if (analogReadingy > 740 & analogReadingy <999)
{
digitalWrite(LeftMotorReverse,HIGH);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(RightMotorForward,HIGH);
digitalWrite(RightMotorReverse,LOW);
}
else
{
digitalWrite(LeftMotorReverse,LOW);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorReverse,LOW);
}
}
}

}

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

Hi,

  if (Serial.available() >= 25) { // Make sure the frame is all there
if (Serial.read() == 0x7E) { // 7E is the start byte
for (int i = 1; i<19; i++) { // Skip ahead to the analog data
byte discardByte = Serial.read();

If you have no USB connected or the monitor is not running, how will your sketch receive anything in the serial port?

Tom… :slight_smile:

When you are not using your Serial Monitor what program is sending data to the Arduino?

Provide some examples of what it sends.

...R