user-1: First, thank you for taking the time to read my post.
I am attempting to use 3 ultrasonic sensors to control 3 DC vibrator motor using the PWM output of an Arduino. Fairly simple, I know, but I'm just a beginner. :)
Here's my code,
int trigpin_1 = 2;
int echopin_1 = 3;
int trigpin_2 = 4;
int echopin_2 = 5;
int trigpin_3 = 6;
int echopin_3 = 7;
int motor_1 = 8;
int motor_2 = 9;
int motor_3 = 10;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
  pinMode(motor_1, OUTPUT);
  pinMode(motor_2, OUTPUT);
  pinMode(motor_3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  long duration, distance;
  digitalWrite(trigpin_1, HIGH);
  digitalWrite(trigpin_2, HIGH);
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigpin_1, LOW);
  digitalWrite(trigpin_2, LOW);
  digitalWrite(trigpin_3, LOW);
  duration = pulseIn(echopin_1, HIGH); 
  duration = pulseIn(echopin_2, HIGH);
  duration = pulseIn(echopin_3, HIGH);
  distance = (duration/2) /29.1;
  if (distance > 200)
  {
    analogWrite(motor_1, 0);
    analogWrite(motor_2, 0);
    analogWrite(motor_3, 0);
  }
  else if(200 == distance > 160)
  {
    analogWrite(motor_1, 50);
    analogWrite(motor_2, 50);
    analogWrite(motor_3, 50);
  }
  else if(160 >= distance > 90)
  {
    analogWrite(motor_1, 100);
    analogWrite(motor_2, 100);
    analogWrite(motor_3, 100);
  }
  else if(90 >= distance > 60)
  {
    analogWrite(motor_1, 150);
    analogWrite(motor_2, 150);
    analogWrite(motor_3, 150);
  }
  else if(60 >= distance > 40)
  {
    analogWrite(motor_1, 200);
    analogWrite(motor_2, 200);
    analogWrite(motor_3, 200);
  }
  else if(40 >= distance)
  {
    analogWrite(motor_1, 255);
    analogWrite(motor_2, 255);
    analogWrite(motor_3, 255);
  }

  Serial.print (distance);
  Serial.println (" cm");
  delay(100);
}



but the problem is the DC motors it will be on all the time with 100% duty cycle. if anyone can help me will be thankful 

user10394: There is quite a bit wrong with your code I'm afraid.
Firstly the if statements.  If the values are kept in descending order then you can do this:
if (distance > 200)
{...}
else if (distance > 160)  // This point won't be reached unless the value is less than 200.
{...}
else if (distance > 100)
{...}

I think what you were trying to do was:
if (distance < 200 && distance > 160)
{...}

but its not necessary.
You are reading the pulse lengths into a single variable overwriting the previous values.  This means you are only reading sensor 3.  If you want to average the values then try this:
duration = pulseIn(echopin_1, HIGH); 
duration += pulseIn(echopin_2, HIGH); // take the result and add it to duration.
duration += pulseIn(echopin_3, HIGH);
duration /= 3;   // divide duration by three and assign the result to duration.

Also I'm not sure about the positioning of your sensors, its possible that they might interfere with each other.  In which case write a function to read from the sensor and store the results in an array.
i.e.
    const int sensorTriggerPins[] = {2, 4, 6};
    const int sensorEchoPins[] = {3, 5, 7};
long readSensor (const int& triggerPin, const int& eechoPin)
{
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH); 
}

void setup()
{
   long duration[3]; // Define a three element array.
   for (int sensor = 0; sensor < 3; ++sensor)
   {
      duration[index] = readSensor(sensorTriggerPin[index], sensorEchoPin[index]);
   }
}

I don't know if this will fix all your problems, but it should help.
(It might have been better if you posted this in the Arduino forum)

-1: <p>First, thank you for taking the time to read my post.</p>

<p>I am attempting to use 3 ultrasonic sensors to control 3 DC vibrator motor using the PWM output of an Arduino. Fairly simple, I know, but I'm just a beginner. :)</p>

<p>Here's my code,</p>

<pre><code>int trigpin_1 = 2;
int echopin_1 = 3;
int trigpin_2 = 4;
int echopin_2 = 5;
int trigpin_3 = 6;
int echopin_3 = 7;
int motor_1 = 8;
int motor_2 = 9;
int motor_3 = 10;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
  pinMode(motor_1, OUTPUT);
  pinMode(motor_2, OUTPUT);
  pinMode(motor_3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  long duration, distance;
  digitalWrite(trigpin_1, HIGH);
  digitalWrite(trigpin_2, HIGH);
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigpin_1, LOW);
  digitalWrite(trigpin_2, LOW);
  digitalWrite(trigpin_3, LOW);
  duration = pulseIn(echopin_1, HIGH); 
  duration = pulseIn(echopin_2, HIGH);
  duration = pulseIn(echopin_3, HIGH);
  distance = (duration/2) /29.1;
  if (distance &gt; 200)
  {
    analogWrite(motor_1, 0);
    analogWrite(motor_2, 0);
    analogWrite(motor_3, 0);
  }
  else if(200 == distance &gt; 160)
  {
    analogWrite(motor_1, 50);
    analogWrite(motor_2, 50);
    analogWrite(motor_3, 50);
  }
  else if(160 &gt;= distance &gt; 90)
  {
    analogWrite(motor_1, 100);
    analogWrite(motor_2, 100);
    analogWrite(motor_3, 100);
  }
  else if(90 &gt;= distance &gt; 60)
  {
    analogWrite(motor_1, 150);
    analogWrite(motor_2, 150);
    analogWrite(motor_3, 150);
  }
  else if(60 &gt;= distance &gt; 40)
  {
    analogWrite(motor_1, 200);
    analogWrite(motor_2, 200);
    analogWrite(motor_3, 200);
  }
  else if(40 &gt;= distance)
  {
    analogWrite(motor_1, 255);
    analogWrite(motor_2, 255);
    analogWrite(motor_3, 255);
  }

  Serial.print (distance);
  Serial.println (" cm");
  delay(100);
}
</code></pre>

<p><a href="https://i.stack.imgur.com/es4er.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/es4er.jpg" alt="here the Connections"></a></p>

<p><a href="https://i.stack.imgur.com/W9jEW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W9jEW.jpg" alt="enter image description here"></a></p>

<p>but the problem is the DC motors it will be on all the time with 100% duty cycle. if anyone can help me will be thankful </p>
 10394: <p>There is quite a bit wrong with your code I'm afraid.</p>

<p>Firstly the if statements.  If the values are kept in descending order then you can do this:</p>

<pre><code>if (distance &gt; 200)
{...}
else if (distance &gt; 160)  // This point won't be reached unless the value is less than 200.
{...}
else if (distance &gt; 100)
{...}
</code></pre>

<p>I think what you were trying to do was:</p>

<pre><code>if (distance &lt; 200 &amp;&amp; distance &gt; 160)
{...}
</code></pre>

<p>but its not necessary.</p>

<p>You are reading the pulse lengths into a single variable overwriting the previous values.  This means you are only reading sensor 3.  If you want to average the values then try this:</p>

<pre><code>duration = pulseIn(echopin_1, HIGH); 
duration += pulseIn(echopin_2, HIGH); // take the result and add it to duration.
duration += pulseIn(echopin_3, HIGH);
duration /= 3;   // divide duration by three and assign the result to duration.
</code></pre>

<p>Also I'm not sure about the positioning of your sensors, its possible that they might interfere with each other.  In which case write a function to read from the sensor and store the results in an array.</p>

<p>i.e.
    const int sensorTriggerPins[] = {2, 4, 6};
    const int sensorEchoPins[] = {3, 5, 7};</p>

<pre><code>long readSensor (const int&amp; triggerPin, const int&amp; eechoPin)
{
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH); 
}

void setup()
{
   long duration[3]; // Define a three element array.
   for (int sensor = 0; sensor &lt; 3; ++sensor)
   {
      duration[index] = readSensor(sensorTriggerPin[index], sensorEchoPin[index]);
   }
}
</code></pre>

<p>I don't know if this will fix all your problems, but it should help.
(It might have been better if you posted this in the Arduino forum)</p>
