SR04 induces servo chatter when it gets false readings

[code]

// Example 5 - Receive with start- and end-markers combined with parsing
// This consists of
// #1 - parsing serial to two integers from format <7,180>
// #2 - writing the parsed integers to servos in the format of <pin number,degrees>
// #3 - incorporating NewPing library with 15 sensors, and writing the results to serial


const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
int firstIntegerFromPC = 0;
int secondIntegerFromPC = 0;
// int floatFromPC = 0.0;

boolean newData = false;

// my servo addition

#include <Servo.h>
Servo mys[20]; //Now we have 20 servos but only use 2-19 (ie 18 servos)
//

//Start Ping code addition
#include <NewPing.h>

#define SONAR_NUM     1 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  //NewPing(43, 44, MAX_DISTANCE),
  //NewPing(45, 46, MAX_DISTANCE),
  //NewPing(21, 22, MAX_DISTANCE),
  //NewPing(23, 24, MAX_DISTANCE),
  //NewPing(25, 26, MAX_DISTANCE),
  //NewPing(27, 28, MAX_DISTANCE),
  //NewPing(29, 30, MAX_DISTANCE),
  //NewPing(31, 32, MAX_DISTANCE),
  //NewPing(34, 33, MAX_DISTANCE),
  //NewPing(35, 36, MAX_DISTANCE),
  //NewPing(37, 38, MAX_DISTANCE),
  //NewPing(39, 40, MAX_DISTANCE),
  //NewPing(50, 51, MAX_DISTANCE),
  //NewPing(52, 53, MAX_DISTANCE)
};
// end newping addition============

void setup() {
  Serial.begin(115200);
  Serial.println("This demo expects 2 pieces of data - two integers");
  Serial.println("Enter data in this style <1, 180>  ");
  Serial.println();
  // my addition
  for (int i = 2; i < 20; i++)
  {
    mys[i].attach(i); // was i+2
  }
  // end my addition

  // Start Ping code void setup addition

  // already done   Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  // end ping code addition============
}
void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    servoMove();
    newData = false;
  }
  // Ping code addition to void loop

  {
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
      if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
        pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
        if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
        sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
        currentSensor = i;                          // Sensor being accessed.
        cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
        sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
      }
    }
    // The rest of your code would go here.
  }



  // end Ping code addition to void loop

}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

//============

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  // tim comments out strcpy(firstIntegerFromPC, strtokIndx); // copy it to firstIntegerFromPC
  firstIntegerFromPC = atoi(strtokIndx); // my addition convert first part to an integer

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  secondIntegerFromPC = atoi(strtokIndx);     // convert this part to an integer

  // Tim comments out strtokIndx = strtok(NULL, ",");
  //  Tim comments out floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
  Serial.print("Servo Number ");
  Serial.println(firstIntegerFromPC);
  Serial.print("Degrees movement ");
  Serial.println(secondIntegerFromPC);
  // Serial.print("Float ");
  // Serial.println(floatFromPC);

}
void servoMove() {
  // start my addition
  mys[firstIntegerFromPC].write(secondIntegerFromPC); //write servo position
  // end my addition
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");

    //  Serial.print(cm[0]);
    //  Serial.print(cm[1]);
    //  Serial.print(cm[2]);
  }
  Serial.println();
}


void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

[/code]