Robot arm coding troubles

Hi! I’m somewhat new to Arduino but I am learning pretty quickly but I’m confused about something.
I have a robot arm with a ping sensor on the end of it and I have it programmed to stop when i put my hand in front of it, yet when I do it only stops once it is complete with turning. How can I make it stop immediately? I am using continuous rotation servos. Also I have not added a claw yet so don’t ask why it isn’t in the code. Also the if and else statements are at the bottom.

WARNING!!! Newbie code ahead!

#define trigPin  2
#define echoPin  3
#define ledPin   13
#define piezo    10
#include <Servo.h>
Servo rotateServo;
Servo armServo1;
Servo armServo2;



void setup()
{
  //armServo1.attach(7);
  //armServo2.attach(8);
//rotateServo.attach(6);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(piezo, OUTPUT);
  startup();
}

void startup()
{
 armServo1.attach(7);
 armServo2.attach(8);
 armServo1.write(88);
 armServo2.write(85);
 delay(700);
 armServo1.write(90);
 armServo2.write(90);
 armServo1.detach();
 armServo2.detach();
}

void rotateLeft()
{
 rotateServo.attach(6);
 rotateServo.write(95);
 delay(1500);
 rotateServo.detach();
}

void rotateRight()
{
 rotateServo.attach(6);
 rotateServo.write(88);
 delay(1500);
 rotateServo.detach();
}

void idle()
{
  armServo1.detach();
  armServo2.detach();
  rotateServo.detach();
}

 
void loop()
{
     
  long duration, distance;
  digitalWrite(trigPin, LOW); // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 74;
  int inches = (distance);
  Serial.println(inches);
  delay(100);


  if(distance >= 3)
  {
  rotateLeft();
  delay(900);
  rotateRight();
  delay(900);
    
  }
else
{
tone(10, 20, 200);
idle();
}}

Your code is riddled with delay() calls. Whilst the delay is occurring no other code runs so it will carry on doing its own thing such as rotating left or right for 1500 + 900 + 900 milliSeconds before reacting to the nearby object and going idle. What you need to do is move a little, check for an object, move a little, check for an object and so on

You say that you are using continuous rotation servos. Do rotateServo.write(95); and rotateServo.write(88); really make the servo turn in opposite directions ?

Why keep attaching and detaching the servo in the move functions ?

That's because it only reads the distance at the top of loop(), and if that's >3 it goes into its rotate left and right sequences which include delays. There are delays of 1500 inside those rotate functions and then delays of 900 between them in loop(). The nature of delay() is that nothing happens during the delay so it doesn't read the distance until it finished all the delaying and gets back to the top of loop().

edit.... as UKHB says 8)

more edit.... Why are you using continuous servos on a robot arm? Robot arms by their nature need positioning and you can't position anything with a continuous servo.

As well as needing to abandon the use of the delay() function you will probably need to move your servos in short steps so they can all move at the same time. See the servo sweep example in the Arduino IDE.

The demo several things at a time illustrates how to use millis() to manage time.

...R