XBEE UNO R3 Sensor Data not reading correctly

I am sending data from an XBEE series 1 mounted on an UNO R3 to another XBEE series 1 mounted on another UNO R3. The communication is working; I have checked the readings by plugging the receiving XBEE to my computer and reading it on X-CTU and the reading are correct. On the receiving XBEE/Arduino there is a servo motor which moves depending on the data received.

What I think seems to be happing is that the servo moves twice on readings with more than 2 digits, it moves once for the 1st digit and a second time for the second digit. I am using two different types of sensors and I seem to get the same results which is the servo making two movements not one. The reason I think it is doing this is because I have two similar sketches; one is sending wind speeds from an anemometer to a Servo and the other which is posted below send the Electret Mic readings. To test this theory I have wrote a sketch to send 3 different readings every 10 seconds and each time a reading was received the servo moved twice within the 10 second gap, 1.34, 0.50,0.66.

Any ideas on what is going on with the sketch?

Electret Mic Sketch:

#define VERSION "1.00a0"

const int sampleWindow =250; // Sample window width in mS (500 mS = 20Hz)
unsigned int sample;


void setup()
{
  Serial.begin(9600);

}  

void loop()
{
  unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level
 
   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;
 
   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 3.3) / 1024;  // convert to volts
 
   Serial.println(volts);
     delay(10);
}

Servo Code:

#define VERSION "1.00a0"
#include <Servo.h>


Servo myservo;
int delayTimeF = 17;
int delayTimeM = 25;
int delayTimeS = 30;
int delayTimeVS = 40; 
int i = 0;    // variable to store the servo position 

void setup()
{
Serial.begin (9600);
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}  

void swing(int low, int high) {
   for (i=low; i<=high; i++) {
       myservo.write(i);
       delay(delayTimeVS);
    }
   for (i=high; i>=low; i--) {
       myservo.write(i);
       delay(delayTimeVS);
    }
}


void loop()
{
   
  if (Serial.available() >0){
    
  

  //if((Serial.read() >= 0.15) && (Serial.read() < 0.30)){ 
        
//swing(100,110);

//}   // end of loop
  
  if((Serial.read() >= 0.31) && (Serial.read() < 0.50)){ 
        
swing(100,110);

}   // end of loop

if((Serial.read() >= 0.51) && (Serial.read() < 0.70)){
      
swing(100,120);

}

if((Serial.read() >= 0.71) && (Serial.read() < 0.90)){
           
swing(100,130);


}
if((Serial.read() >= 0.91) && (Serial.read() < 1.50)){
         
swing(100,140);

}
if((Serial.read() >= 1.51) && (Serial.read() < 2.00)){
         
swing(100,150);

}
if((Serial.read() >= 2.01) && (Serial.read() < 3.00)){
         
swing(100,160);

}
if((Serial.read() >= 3.01) && (Serial.read() < 4.00)){
         
swing(100,170);

}
}
}

A sample reading:

0.50
0.66
0.34
1.34

  if((Serial.read() >= 0.31) && (Serial.read() < 0.50)){

You are sending “0.50”, for example. Not a single one of those characters has a integer value that is between 0.31 and 0.50. Nor any of those other fractional values that you have.

You have a long way to go to properly send packets (you need start and end markers) and to properly read those packets and convert the strings back to values.

Start with changing the sender to send ‘<’, then the value, then ‘>’ using three Serial.print() statements.

Then, use some code like this on the receiver:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says “Process the packet” is where you put code to call atof(inData) and to move the servo based on the returned float.