Remove delay on ultra sensor

Hi, I need help with my program. There is a long delay when the sensor detects an obstacle and after obstacle have been removed from sight. There is another delay before it goes forward.

#include <AFMotor.h> //import your motor shield library
#define trigPin 13 // define the pins of your sensor
#define echoPin 12 
#define CW 7 // motor 1 clockwise
#define CCW 8 //motor 1 counter clock-wise
#define CW2 9 //motor 2 clockwise
#define CCW2 11//motor 2 counter clock-wise
int ledPin = 4; //pump

void setup() {
  Serial.begin(9600); // begin Computer language communitication  For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
  Serial.println("Motor test!");
    pinMode(ledPin, OUTPUT);
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send out sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  pinMode(CW,OUTPUT);  //set motor1 cw
  pinMode(CCW,OUTPUT); //set motor 1 ccw
  pinMode(CW2,OUTPUT);  //set motor2 cw
  pinMode(CCW2,OUTPUT); //set motor2 ccw

  }
void loop() {

   long duration, distance; // start the scanning
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(50); // delays sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(50); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); //pulse travel to the object and back to the sensor
  distance = (duration/2) / 29;// convert the distance to centimeters.The speed of sound is 340 m/s or 29 microseconds per centimeter.
  if (distance < 20)/*if there's an obstacle 25 cm, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
    digitalWrite(CW,LOW);  //Turn right
    digitalWrite(CCW,HIGH);
    digitalWrite(CW2,HIGH);
    digitalWrite(CCW2,LOW);
    delay(2000);
    
    digitalWrite(CW,LOW); //go straight
    digitalWrite(CCW,HIGH);
    digitalWrite(CW2,LOW);
    digitalWrite(CCW2,HIGH);
    delay(2000);
    
    digitalWrite(CW,LOW); //Turn right
    digitalWrite(CCW,HIGH);
    digitalWrite(CW2,HIGH);
    digitalWrite(CCW2,LOW);
    delay(2000);
    
    digitalWrite(CW,HIGH); //Turn left
    digitalWrite(CCW,LOW);
    digitalWrite(CW2,LOW);
    digitalWrite(CCW2,HIGH);
    delay(2000);
    
    digitalWrite(CW,LOW); //go straight
    digitalWrite(CCW,HIGH);
    digitalWrite(CW2,LOW);
    digitalWrite(CCW2,HIGH);
    delay(2000);
    
    digitalWrite(CW,HIGH); //Turn left
    digitalWrite(CCW,LOW);
    digitalWrite(CW2,LOW);
    digitalWrite(CCW2,HIGH);
    delay(2000);
    
           
}
  else {
   Serial.println ("No obstacle detected. going forward");
    digitalWrite(CW,LOW); //go straight
    digitalWrite(CCW,HIGH);
    digitalWrite(CW2,LOW);
    digitalWrite(CCW2,HIGH);

  digitalWrite(ledPin, HIGH);   // sets the pump on
  delay(8000);                  // turns on for 8 second
  digitalWrite(ledPin, LOW);    // sets the pump off
  delay(4000);                  // off for 4 seconds
  }
}

Anywhere you have (delay2000), and I think it occurs 6 times, stops anything else happening for 2s.

Reducing this delay to say 100 would be any easy modification to try.

You could make this delay time a variable, so that all instances can be changed in one go.

There are probably better solutions, like using the techniques used in the "blink without delay" example in the IDE.

The timing itself may be sensible, but indeed you'll need a different way of doing it. No delay() calls as they block everything else.

Blink without delay is a good starting point.
You'll have to add a whole lot of variables to keep track of when everything started and what function is active, to know when to stop doing something.

JohnLincoln:
Anywhere you have (delay2000), and I think it occurs 6 times, stops anything else happening for 2s.

Reducing this delay to say 100 would be any easy modification to try.

You could make this delay time a variable, so that all instances can be changed in one go.

There are probably better solutions, like using the techniques used in the "blink without delay" example in the IDE.

Alright, thanks a lot mate. I will try that.

wvmarle:
The timing itself may be sensible, but indeed you'll need a different way of doing it. No delay() calls as they block everything else.

Blink without delay is a good starting point.
You'll have to add a whole lot of variables to keep track of when everything started and what function is active, to know when to stop doing something.

I will give it a try. Thanks!