-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);
}

here the Connections

enter image description here

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

  • The wiring diagram is very difficult to read. It would be better tor read if you redraw it with the builtin schematic editor. – Ariser - reinstate Monica Nov 22 '16 at 12:04
  • Does this 'if(160 >= distance > 90)' actually work as intended in arduinoees? I don't believe it does in any flavor of C. You also have 3 lines of 'duration = ...' so you're just overwriting the 1st 2 with the last one. – brhans Nov 22 '16 at 12:20
2

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)

| improve this answer | |
  • PS - You could streamline you setup function if you use arrays for the pin assignments, and you can do the same wit you motors too. – Code Gorilla Nov 22 '16 at 12:39
0

You don't want to use GPIO outputs to drive motors directly. You'll likely burn-out your Arduino with the schematic you've given. At a minimum you'll want to use transistors (that can handle the motor current load) to switch the motors on/off.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy