if else statements with analogWrite functions very slow sonar feedback response

Hello!

Any light on this subject would be very much appreciated.
The code below works as intended. Vibration motors 1, 2 and 3 are turned on from sound feedback at sensors
1, 2 and 3 respectively. If else statements allow motors to turn on at discrete distance intervals and decreasing in speed
as the detected object is further away. But, each feed back and motor run occurs each 3 seconds.
I cannot figure out why the long 3 seconds delay between each motor run.
Please help.

#include <SoftwareSerial.h> // to read data from range finders PINS 3, 5 and 7
#include
void setup()
{
Serial.begin(9600);
}
void loop()
{
const int pingsensor1Pin = 2;
const int pingsensor2Pin = 4;
const int pingsensor3Pin = 6;
const int echosensor1Pin = 3;
const int echosensor2Pin = 5;
const int echosensor3Pin = 7;
int inches;
long duration1, duration2, duration3, distance; // Duration used to calculate distance
const int motor1Pin = 9;
const int motor2Pin = 10;
const int motor3Pin = 11;
/* This Code uses 3 sensors fired nearly simultaneously and powers respective vibration motors 1, 2 and 3
When reflected back sound is detected and motor speed is inversely proportional to obstacle distance.
Sensor 1 has pin 2 for trigger and pin 3 for Signal. Sensor 2 has pin 4 for trigger and pin 5 for Signal.
Sensor 3 has pin 6 for trigger and pin 7 for Signal.
Output motor 1 on pin 9. Output motor 2 on pin 10. Output motor 3 on pin 11. */
pinMode(pingsensor1Pin, OUTPUT); // preparing sensor1 pin 2 as output for pulses
digitalWrite(pingsensor1Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor1Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor1Pin, LOW);
pinMode(pingsensor2Pin, OUTPUT); // preparing sensor2 pin 4 as output for pulses
digitalWrite(pingsensor2Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor2Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor2Pin, LOW);
pinMode(pingsensor3Pin, OUTPUT); // preparing sensor3 pin 6 as output for pulses
digitalWrite(pingsensor3Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor3Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor3Pin, LOW);

pinMode(echosensor1Pin, INPUT); // listen for sound wave reflected back on pin 3
duration1 = pulseIn(echosensor1Pin, HIGH);
pinMode(echosensor2Pin, INPUT); //listen for sound wave reflected back on pin 5
duration2 = pulseIn(echosensor2Pin, HIGH);
pinMode(echosensor3Pin, INPUT); //listen for sound wave reflected back on pin 7
duration3 = pulseIn(echosensor3Pin, HIGH);

inches = (duration1/2) / 74; // convert time_echo from sensor 1 to object distance in inches
if (inches > 2 && inches <= 16)
{analogWrite(motor1Pin, 150);
delay (500);
analogWrite (motor1Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor1Pin, 143);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor1Pin, 120);
delay (150);
analogWrite(motor1Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor1Pin, 100);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor1Pin, 80);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor1Pin, 60);
delay (150);
analogWrite (motor1Pin, 0);}

inches = (duration2/2) / 74; // convert time_echo from sensor 2 to object distance in inches
if (inches > 2 && inches <= 16)
{ analogWrite (motor2Pin, 150);
delay (500);
analogWrite (motor2Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor2Pin, 143);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor2Pin, 120);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor2Pin, 100);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor2Pin, 80);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor2Pin, 60);
delay (150);
analogWrite (motor2Pin, 0);}

inches = (duration3/2) / 74; // convert time_echo to object distance in inches
if (inches > 2 && inches <= 16)
{ analogWrite (motor3Pin, 150);
delay (500);
analogWrite (motor3Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor3Pin, 143);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor3Pin, 123);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor3Pin, 100);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor3Pin, 80);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor3Pin, 60);
delay (150);
analogWrite (motor3Pin, 0);}
}

Without the hashtag is kinda diffucult to read, please edit and use it.

On topic, pulseIn waits til the signal is reveived. This is what is maybe delaying the robot. And those delays add some time too, you have three consecutive if else statements each one delays 150ms thats at least 0.5 sec.

The culprits are all those delays in your code. The delay() function blocks the code from doing anything else until the delay is finished. So all the delay(150)and delay(500) add up, even the delayMicroseconds() too. The best thing to do is get rid of all the delays and look into the example sketch Blink Without Delay. Another solution is what another user Robin2 has made (if I can find the thread I will make a link to it)

Found the link, Demonstration code for several things at the same time

you need the delayMicroseconds() but you cannot do that like you are.

Put your pinMode here instead:

void setup()
{
  Serial.begin(9600);
  pinMode(echosensor1Pin, INPUT); 
  pinMode(echosensor2Pin, INPUT); 
  pinMode(echosensor3Pin, INPUT); 
  pinMode(pingsensor1Pin, OUTPUT); 
  pinMode(pingsensor2Pin, OUTPUT); 
  pinMode(pingsensor3Pin, OUTPUT); 
}

you are dealing with sonar, so the time between the signal transmitted and the read of the echo is critical, you have to do them one at a time:

digitalWrite(pingsensor1Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingsensor1Pin, HIGH); //fire the 8 sound pulses
  delayMicroseconds(10);
  digitalWrite(pingsensor1Pin, LOW);
  duration1 = pulseIn(echosensor1Pin, HIGH);
  digitalWrite(pingsensor2Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingsensor2Pin, HIGH); //fire the 8 sound pulses
  delayMicroseconds(10);
  digitalWrite(pingsensor2Pin, LOW);
  duration2 = pulseIn(echosensor2Pin, HIGH);
  digitalWrite(pingsensor3Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingsensor3Pin, HIGH); //fire the 8 sound pulses
  delayMicroseconds(10);
  digitalWrite(pingsensor3Pin, LOW); 
  duration3 = pulseIn(echosensor3Pin, HIGH);

consider using the NewPing library for simplicity's sake

What kind of ping sensor do you have the 3 pin or 4 pin sensor? You can and probably should use the NewPing library, it is better and more efficient than using pulseIn or using delayMicroseconds().