Dirty data coming in over XBee 2mW

Problem:


My analog sensor (maxbotix LV-EZ1 Ultrasonic Range Finder) is returning garbled meaningless strings about every 7 times it reads. Is there any way I can get it to just return clean data?

My hardware:
Arduino Fio v2 from Sparkfun
XBee 2mW on Arduino XBee 2mW as coordinator on an XBee explorer from Sparkfun (in API mode)
Maxbotix LV-EZ1 Ultrasonic Range Finder
Running on a Macbook Air

my Arduino sketch:

int val=0;

void setup()
{
  Serial.begin(9600);  
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}

void loop() {
  digitalWrite(13,HIGH);
  int val = analogRead(A0);
  float voltage = (val);
  Serial.print(voltage); 
  Serial.println(" ");
  digitalWrite(13,LOW);
}
  int val = analogRead(A0);
  float voltage = (val);
  Serial.print(voltage); 
  Serial.println(" ");

Why are you converting the integer to a float? The voltage read from pin is not in the range 0 to 1023 volts, so your "conversion" is useless.

Is there any way I can get it to just return clean data?

The Arduino with XBee IS sending clean data. Some interference is happening that corrupts the data between the two XBees.

What you need to do is send recognizable packets, with some information, like packet length and checksum, added, and test the packet on the receiving end. Corrupted packets should be discarded.

For instance, if you send <3:182:xxx>, where < is the start of packet marker, 3 is the payload size, and xxx is the checksum, you could discard any packet missing a start marker, any packet missing an end marker, any packet with the incorrect number of delimiters, any packet with a payload length not equal to the defined length, and any packet with an incorrect checksum.

Thank you!
I've kinda been hacking this together with random pieces of code from all over and adding new pieces here and there.

I'm very new to this XBee stuff, is there a tutorial that you know of that could help me learn how to discard those packets?

Or, you can switch the remote XBee to API mode and use the XBee library. That way you have the checksum returned and don't have to write your own parser to take apart the packet. You can even double check the transmission by using the ACK that is available with API mode. Use the latest version of the XBee library and you can even use a couple of digital pins for the XBee which leaves the rx,tx pins of the arduino for debugging, and you don't have to disconnect anything to change the arduino programming.

If you want to carry it to an extreme, use just the XBee, no arduino, as the sending unit and decode the packets at the controller to get your voltage reading from the remote. There's as many ways to do this as there are people doing it.