0
\$\begingroup\$

I m building a simple obstacle avoidance robot. Here I Typed the code. But there is a bug I cannot find . The Motors are not working properly when they intergrated as whole. when I check the codes and run separatly it runs the dc motor smoothly. when I put all together motors are not spining. I have double checked my connections.
Here is the code

#include <Servo.h>

const int trigPin = 13;
const int echoPin = 12;


int getDistance() {
  long duration;
  int distance;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2;
  return distance;
}

int left_speed = 10;
int right_speed = 9;

int left_pin_1 = 7;
int left_pin_2 = 6;

int right_pin_1 = 4;
int right_pin_2 = 5;

int driveSpeed = 100;
Servo myservo;

void setup() {
  myservo.attach(11);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  pinMode(left_pin_1, OUTPUT);
  pinMode(left_pin_2, OUTPUT);
  pinMode(right_pin_1, OUTPUT);
  pinMode(right_pin_2, OUTPUT);
  pinMode(left_speed, OUTPUT);
  pinMode(right_speed, OUTPUT);
  setForward();

}

int turnLeft() {
  digitalWrite(left_pin_1, HIGH);
  digitalWrite(left_pin_2, LOW);
  digitalWrite(right_pin_1, HIGH);
  digitalWrite(right_pin_2, LOW);
}

int turnRight() {
  digitalWrite(left_pin_1, LOW);
  digitalWrite(left_pin_2, HIGH);
  digitalWrite(right_pin_1, LOW);
  digitalWrite(right_pin_2, HIGH);
}


int forward() {
  digitalWrite(left_pin_1, LOW);
  digitalWrite(left_pin_2, HIGH);
  digitalWrite(right_pin_1, HIGH);
  digitalWrite(right_pin_2, LOW);
  delay(300);
}

int driveStop() {
  digitalWrite(left_pin_1, LOW);
  digitalWrite(left_pin_2, LOW);
  digitalWrite(right_pin_1, LOW);
  digitalWrite(right_pin_2, LOW);
}


int setForward() {
  myservo.write(90);
}

int searchRight() {
  myservo.write(0);
  delay(500);
  int dist = getDistance();
  delay(500);
  return dist;
}


int searchLeft() {
  myservo.write(180);
  delay(500);
  int dist = getDistance();
  delay(500);
  return dist;
}



void loop() {
  forward();
}

int setDriveSpeed(int spd) {
  analogWrite(left_speed, spd);
  analogWrite(right_speed, spd);
}
```
\$\endgroup\$
2
  • \$\begingroup\$ Should int forward() not be void forward(). If that isn't the problem then post the minimum code that does work and the minimum code that doesn't work. \$\endgroup\$ – Transistor Feb 23 '20 at 21:10
  • \$\begingroup\$ int forward() works. the problems occurs when I import the servo.h and attach the the servo pin. after doing that dc motors doesnot work \$\endgroup\$ – Shakthi Sachintha Feb 23 '20 at 21:15
2
\$\begingroup\$

Solved! servo.h library messes up with the Arduino pwm pins. It disables the 9,10 pins even we haven't connected a servo to that. That is the issue here. Have to use pwm other than (9,10) for the motor driver's enable pin.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks for posting the answer. You can accept it after a couple of days (I think) to indicate that it's solved. You should capitalise and punctuate properly for maximum readability. Posts with correct spelling, punctuation, and grammar are easier to read. They also tend to get read and upvoted more frequently. Remember, you can always go back at any time and edit your post to improve it. See the site policy in Write to the best of your ability on the site's help pages. \$\endgroup\$ – Transistor Feb 23 '20 at 21:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.