Duplicate Serial Data with XBEE

Hello all. Noob here, so I’ll do my best not to annoy. I have 2 arduinos with xbees. I am trying to have a pot on one arduino transmit via serial to an LCD on another arduino. The problem is that the LCD displays the serial data twice. For instance, instead of showing “255”, it shows “255255”. The numbers aren’t perfectly in sync, the second number leads the first by a small amount.

Here is the send code.

int sensorValue = 0;    

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

void loop() {
  sensorValue = analogRead(A0);  
  Serial.print(sensorValue);
  delay(100);                     
}

Here is the receive code.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  lcd.begin(16, 2);
  Serial.begin(57600);
}

void loop()
{
    if (Serial.available()) {
      delay(100);
      lcd.clear();
        while (Serial.available() > 0) {
        lcd.write(Serial.read());
    }
  }
}

Thanks for the help sirs.

    if (Serial.available()) {
      delay(100);

Once we know that there is data to read, lets fart around and do nothing for a while.

Why?

Not sure, just modifying one of the examples? I think I've commented that out before, and it didn't fix the duplication.

It looks like the delay in the receiver gives the sender just enough time to send a second reading, so you get both. Increase the sender's delay to 250 or 500. That should solve it. Better though, devise a primitive protocol to control it & forget the delays altogether e.g. send a '*', then the number, then a '!'. Then your code can look for star * and read numbers until it sees!.

By god it worked. Just increased the delay of the sender. If things get hairy, ill start adding start and stop bytes. thanks sirs!

In solving the issue, you can send less often (the "solution" you choose) or you can read more often, by not delaying at all after learning that there is data to read.

But, if you are happy with your "solution", I'm fine with it.