     }
     break;
 }
-while(z > 700 && jointexcess_prevention==0){                              //Tilting the head to the left moves on to the previous joint
+while(z > 600 && z < 700 && jointexcess_prevention==0 && x > 0 && x < 1000 && y > 0 && y < 1000){                              //Tilting the head to the left moves on to the previous joint
    joint--;
    jointexcess_prevention++;
    if(joint < 1){

  y = infoPk[15] + (infoPk[14] << 8);
  x = infoPk[17] + (infoPk[16] << 8);
}
  Serial.print(" X = ") && Serial.print(x);
  Serial.print(" Y = ") && Serial.print(y);
  Serial.print(" Z = ") && Serial.print(z);
  Serial.print(" joint number= ") && Serial.print(joint);

  Dynamixel.setHoldingTorque(0x05, true);

  //Set profile acceleration
  Dynamixel.setProfileAcceleration(0x01, 0);           //Set profile acc for each servo
  Dynamixel.setProfileAcceleration(0x02, 0);
  Dynamixel.setProfileAcceleration(0x03, 0);
  Dynamixel.setProfileAcceleration(0x04, 100);
  Dynamixel.setProfileAcceleration(0x05, 100);

  //Set profile velocity
  Dynamixel.setProfileVelocity(0x01, 50);              //Set profile vel for each servo
  Dynamixel.setProfileVelocity(0x02, 50);
  Dynamixel.setProfileVelocity(0x03, 50);
  Dynamixel.setProfileVelocity(0x04, 100);
  Dynamixel.setProfileVelocity(0x05, 100);
 }

int x, y, z, jointexcess_prevention;
