Servo objects break motor control

So, I've been working on a robot car controlled through wifi and on the process of putting a camera on a stand rotated and tilted by 2 servos. As soon as I make any servo objects the right side wheels stop working.

Is this supposed to happen? Or whats going on?

The code in which all the motors work below:

const int RightB = 8;
const int RightF = 9;
const int RightPWM = 10;
const int LeftPWM = 11;
const int LeftB = 12;
const int LeftF = 13;
const int TrigBack = 4;
const int EchoBack = 5;
const int TrigFront = 6;
const int EchoFront = 7;
long durationBack;
int distanceBack;
long durationFront;
int distanceFront;
int command;
int LastAction;

void setup() {
  pinMode(RightB, OUTPUT);
  pinMode(RightF, OUTPUT);
  pinMode(RightPWM, OUTPUT);
  pinMode(LeftPWM, OUTPUT);
  pinMode(LeftB, OUTPUT);
  pinMode(LeftF, OUTPUT);
  pinMode(TrigBack, OUTPUT);
  pinMode(EchoBack, INPUT);
  pinMode(TrigFront, OUTPUT);
  pinMode(EchoFront, INPUT);
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  Serial.begin(115200);
}

void loop() {
  if (LastAction == 1) {
    distanceFront = frontsensor();
    if (distanceFront < 30) {
      forwardLimitStop();
    }
  }
  if (LastAction == 2) {
    distanceBack = backsensor();
    if (distanceBack < 20) {
      backwardLimitStop();
    }
  }
  if (Serial.available() > 0) {
  command = Serial.read();
  switch (command) {
  case 'F': {
    if (LastAction != 6) {
      forward();
    }    
    break;
  }
  case 'B': {
    if (LastAction != 7) {
      backward();
    }
    break;
  }
  case 'R': {
    right();
    break;
  }
  case 'L': {
    left();
    break;
  }
  case 'S': {
    stoprobot();
    break;
  }
  }
  }
}

int frontsensor() {
  digitalWrite(TrigFront, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigFront, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigFront, LOW);
  durationFront = pulseIn(EchoFront, HIGH);
  distanceFront= durationFront*0.034/2;
  return distanceFront;
}

int backsensor() {
  digitalWrite(TrigBack, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigBack, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigBack, LOW);
  durationBack = pulseIn(EchoBack, HIGH);
  distanceBack= durationBack*0.034/2;
  return distanceBack;
}

void forward() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, HIGH);
  analogWrite(RightPWM, 180);
  analogWrite(LeftPWM, 180);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, HIGH);
  LastAction = 1;
}

void backward() {
  digitalWrite(RightB, HIGH);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, HIGH);
  digitalWrite(LeftF, LOW);
  LastAction = 2;
}

void right() {
  digitalWrite(RightB, HIGH);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, HIGH);
  LastAction = 3;
}

void left() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, HIGH);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, HIGH);
  digitalWrite(LeftF, LOW);
  LastAction = 4;
}

void stoprobot() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 5;
}

void forwardLimitStop() {  
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 6;
}

void backwardLimitStop() {  
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 7;
}

But in the code on the reply (original message was too long =) the RightB and LeftB motors stop working:

Also have ultrasonic sensors on both sides of the robot. Motor controller is L298N. Wifi module is ESP8266 ESP-01 programmed to just relay messages to arduino.

Tried 2 different arduino boards. Uno r3 and duemilanove.

Any help or explanation much appreciated.

Will try to track down which of the 3 pins is the problem causer tomorrow(or sunday). But posted this before if there is a reasonable explanation.

You have not said what Arduino you are using but on an Uno and Nano the Servo library uses Timer1 which effectively disables the use of analogWrite() on pins 9 and 10.

If you must use pins 9 and 10 there is a ServoTimer2 library - but it will disable analogWrite() on pins 3 and 11

...R

#include <Servo.h>
Servo myservo1;
Servo myservo2;

const int RightB = 8;
const int RightF = 9;
const int RightPWM = 10;
const int LeftPWM = 11;
const int LeftB = 12;
const int LeftF = 13;
const int TrigBack = 4;
const int EchoBack = 5;
const int TrigFront = 6;
const int EchoFront = 7;
long durationBack;
int distanceBack;
long durationFront;
int distanceFront;
int command;
int LastAction;

void setup() {
  pinMode(RightB, OUTPUT);
  pinMode(RightF, OUTPUT);
  pinMode(RightPWM, OUTPUT);
  pinMode(LeftPWM, OUTPUT);
  pinMode(LeftB, OUTPUT);
  pinMode(LeftF, OUTPUT);
  pinMode(TrigBack, OUTPUT);
  pinMode(EchoBack, INPUT);
  pinMode(TrigFront, OUTPUT);
  pinMode(EchoFront, INPUT);
  myservo1.attach(2);
  myservo2.attach(3);
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  myservo1.write(90);
  myservo2.write(90);
  Serial.begin(115200);
}

void loop() {
  if (LastAction == 1) {
    distanceFront = frontsensor();
    if (distanceFront < 30) {
      forwardLimitStop();
    }
  }
  if (LastAction == 2) {
    distanceBack = backsensor();
    if (distanceBack < 15) {
      backwardLimitStop();
    }
  }
  if (Serial.available() > 0) {
  command = Serial.read();
  switch (command) {
  case 'F': {
    if (LastAction != 6) {
      forward();
    }    
    break;
  }
  case 'B': {
    if (LastAction != 7) {
      backward();
    }
    break;
  }
  case 'R': {
    right();
    break;
  }
  case 'L': {
    left();
    break;
  }
  case 'S': {
    stoprobot();
    break;
  }
  case '0': {
    myservo1.write(170);
    LastAction = 8;
    break;
  }
  case '1': {
    myservo1.write(130);
    LastAction = 8;
    break;
  }
  case '2': {
    myservo1.write(90);
    LastAction = 8;
    break;
  }
  case '3': {
    myservo1.write(50);
    LastAction = 8;
    break;
  }
  case '4': {
    myservo1.write(10);
    LastAction = 8;
    break;
  }
  case '5': {
    myservo2.write(170);
    LastAction = 8;
    break;
  }
  case '6': {
    myservo2.write(130);
    LastAction = 8;
    break;
  }
  case '7': {
    myservo2.write(90);
    LastAction = 8;
    break;
  }
  case '8': {
    myservo2.write(50);
    LastAction = 8;
    break;
  }
  case '9': {
    myservo2.write(10);
    LastAction = 8;
    break;
  }
  }
  }
}

int frontsensor() {
  digitalWrite(TrigFront, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigFront, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigFront, LOW);
  durationFront = pulseIn(EchoFront, HIGH);
  distanceFront= durationFront*0.034/2;
  return distanceFront;
}

int backsensor() {
  digitalWrite(TrigBack, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigBack, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigBack, LOW);
  durationBack = pulseIn(EchoBack, HIGH);
  distanceBack= durationBack*0.034/2;
  return distanceBack;
}

void forward() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, HIGH);
  analogWrite(RightPWM, 180);
  analogWrite(LeftPWM, 180);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, HIGH);
  LastAction = 1;
}

void backward() {
  digitalWrite(RightB, HIGH);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, HIGH);
  digitalWrite(LeftF, LOW);
  LastAction = 2;
}

void right() {
  digitalWrite(RightB, HIGH);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, HIGH);
  LastAction = 3;
}

void left() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, HIGH);
  analogWrite(RightPWM, 150);
  analogWrite(LeftPWM, 150);
  digitalWrite(LeftB, HIGH);
  digitalWrite(LeftF, LOW);
  LastAction = 4;
}

void stoprobot() {
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 5;
}

void forwardLimitStop() {  
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 6;
}

void backwardLimitStop() {  
  digitalWrite(RightB, LOW);
  digitalWrite(RightF, LOW);
  analogWrite(RightPWM, 0);
  analogWrite(LeftPWM, 0);
  digitalWrite(LeftB, LOW);
  digitalWrite(LeftF, LOW);
  LastAction = 7;
}

Ho do you have things wired up? Can you post a wiring diagram? Pencil, paper and a camera are good enough if you include proper detail (like numbers of the pins you are using)

Robin2:
You have not said what Arduino you are using but on an Uno and Nano the Servo library uses Timer1 which effectively disables the use of analogWrite() on pins 9 and 10.

If you must use pins 9 and 10 there is a ServoTimer2 library - but it will disable analogWrite() on pins 3 and 11

...R

Whoa. Thanks for the quick reply. Saved me a lot of headache.
I Have to say I never would have quessed this.

On the other note didn't I write I was using arduino uno r3? Or what do you mean I didn't say what arduino I was using?

vinceherman:
Ho do you have things wired up? Can you post a wiring diagram? Pencil, paper and a camera are good enough if you include proper detail (like numbers of the pins you are using)

The wiring is pretty much awful looking currently. With regular jumper wires and a breadboard. I've been thinking about doing them with distance cut cables and soldering it all together. To make it look decent when everything is done with prototyping.

That said the best way to currently show wiring is by the pin definitions of the code :smiley:

Delta_G:
Pro tip for you, no matter how sure you are that you know how a library or a class like servo works, go ahead and read all the documentation anyway. It will save you lots of this kind of frustration over thing you couldn't have guessed if you don't have to guess.

Thanks for the tip :). I'm pretty new to these things but will remember this case for sure and read the documents in the future. As long as they are just as nicely and neatly written as the servo documentation.

Malagath:
On the other note didn't I write I was using arduino uno r3?

Sorry - I missed that.

...R

Delta_G:
That doesn't show anything.

For me it shows pretty much. But thats ofc just me.
For sure a full schematic (preferably made with a schematic designing program) shows the whole picture alot clearer. (And also when you made the wiring yourself you pretty much remember where everything goes(which might also sometimes be a bad thing as you might not notice if something is wrong))

Delta_G:
Nobody wants to see a picture of a tangle of wires anyway.

With this I agree totally.

Delta_G:
They almost never are. Most of the time you end up wading through source code to figure out what they've got.

Oh dear. :sob: Thats way more than I signed up for.

Unnecessarily long wall of text tbh. But you are right that when sharing projects with other people you need to provide everything(or everything needed). Will remember that.

Personally I don't see the need to make a diagram for straightforward connecting colorcoded wires on 2 ends. And yes, I do make diagrams for most things I do with electronics. Especially if using transistors/fets/ic's and stuff.

Malagath:
Unnecessarily long wall of text tbh.

I don't agree.

And it all seems to be excellent advice - worth reading 3 or 4 times.

...R