Irregular Serial transmission over Xbee shield & Sparkfun Joystick shield

Hey, I’m relatively new to Arduino and a complete novice when it comes to xbees. This is also my first question here as well.

At the moment I have one arduino with an official arduino xbee shield and a sparkfun joystick shield (DEV-09760) stacked together. This is the transmitter which is sending basic HIGH and LOW values from one button on the joystick shield across to the receiver.

On the receiving side I have another arduino with an xbee shield stacked on top and a LED. When the button is HIGH the LED is HIGH and likewise when the button is LOW the LED is LOW.

Simple enough, I’ve dumbed things down so I can try to get to the root of the problem.

Except my problem lies with the serial transmission. When I have this setup on 9600 baud I get a stream of data over the serial monitor and it works as it should. Just move it to the end of the desk to the other side of my laptop and suddenly it’s out of reach and the data is delayed with long pauses. I have no idea what’s causing this. It’s barely arm’s length.

I doubt the problem lies with the shields, I’m pretty sure there’s no issue over pin usage. Everything is working fine.

Any help is appreciated!

Here’s the transmitting arduino with the xbee shield and joystick shield:

// TX 

const byte inputPin = 4;


void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT);
  digitalWrite(inputPin, HIGH);
}

void loop()
{
  
  if (digitalRead(inputPin) == HIGH) {
    Serial.print('o');
  }
  else if (digitalRead(inputPin) == LOW)
  {
    Serial.print('f');
  }
  
  Serial.println();
  delay(10);
}

And here’s the receiving arduino with the xbee shield:

// RX

int ledPin = 13;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);

}

void loop()
{
  if (Serial.available()) {
    int light = Serial.read();
    
    if (light == 'o') {
      digitalWrite(ledPin, HIGH);
    }
    else if (light == 'f') {
      digitalWrite(ledPin, LOW);
    }
    Serial.print("Arduino received: ");
    Serial.write(light);
    Serial.write(10);
  }
    Serial.flush();
}

Screen Shot 2013-07-28 at 18.32.49.png

I don't that this is the problem, but you could be overwhelming the serial port on the transmitting arduino/xbee. Try putting a delay(10); after each Serial.print();

Really just a stab in the dark.

Hello, Why do you call flush() ? I think you can avoid it, the next incoming byte will be treated in the next loop and you probably won't have a buffer overflow since your code is short enough to handle all bytes. To see what happends, you can also add after your else, else if :

else
{
   Serial.print("unknown byte : ");
    Serial.write(light);
    Serial.write('\n');
}

to see what is received. I think that sending

Serial.println();

will send a new line and this is what you receive and don't handle. The three followings are equivalents:

Serial.write(10);
Serial.print('\n');
Serial.println();

good luck