Robotic Arm Code

Hello!
I’m trying to develop a code for a robotic arm, the code will read a ultrasonic sensor data using the ultrasonic library, calculate the distance between a object and a the sensor, and if the distance is smaller than 20cm the arduino will move 3 servos in the arm and grab the object.

I developed a code for this function, but seems that the arduino does not read the sensor data, the servos are moved no matter what is the distance of the object, somebody could help me?

Here’s the code:

#include "Ultrasonic.h"
#include <Servo.h>;

#define echoPin 13 // Pino 13 -> Echo
#define trigPin 12 // Pino 12 -> Trig
#define LED 5
Ultrasonic ultrasonic(trigPin,echoPin);
int parado = 0;
int inicio = 0;
Servo gservo;
Servo aservo;
Servo bservo;

void setup()
{
gservo.attach(8); //claw servo
aservo.attach(9); //joint servo
bservo.attach(10); //base servo
}

void mexendo(void) {
    aservo.write(180);
    delay(15);

    bservo.write(180);
    delay (1500);

    gservo.write(90); 
    delay(1500);
    
    aservo.write(150);
    delay(150);
    
    aservo.write(120);
    delay(150);
    
    aservo.write(90);
    delay(150);
    
    aservo.write(60);
    delay(150);
    
    aservo.write(30);
    delay(150);
    
    aservo.write(1);
    delay(150);
    
    gservo.write(1); 
    delay(1500);
    
    aservo.write(30);
    delay(150);
    
    aservo.write(60);
    delay(150);
    
    aservo.write(90);
    delay(150);
    
    aservo.write(180);
    delay(150);
    
    bservo.write(90);
    delay (1500);
    
    bservo.write(1);
    delay (1500);
    
    aservo.write(150);
    delay(150);
    
    aservo.write(120);
    delay(150);
    
    aservo.write(90);
    delay(150);
    
    aservo.write(60);
    delay(150);
    
    aservo.write(30);
    delay(150);
    
    aservo.write(1);
    delay(150);
    
    gservo.write(180); 
    delay(1500);
    
    aservo.write(30);
    delay(150);
    
    aservo.write(60);
    delay(150);
    
    aservo.write(90);
    delay(150);
    
    aservo.write(180);
    delay(150);
    
    gservo.write(1); 
    delay(1500);
    
    bservo.write(90);
    delay(150);
    
}

void quieto(void) {
    aservo.write(180);
    delay(15);

    bservo.write(90);
    delay (1500);

    gservo.write(1); 
    delay(1500);
}

void loop(){
  int i=0;
  long distance=ultrasonic.Ranging(CM);
  while(i<4) {
   distance=(distance + ultrasonic.Ranging(CM))/2;
   i++; 
  }
    if(distance>10) {
     digitalWrite(LED,HIGH); 
     mexendo();
     inicio=1;
  } else {
    digitalWrite(LED,LOW);
    if(inicio==0) { 
      quieto();
      delay(1000);
      parado=1;
      }
    
  }
  
}

Some Serial.print()s would be useful. As it is, I don’t see how you have any basis for understanding why the servos move. A link to the non-standard Ultrasonic library would be useful, too.