user23571: I have 2 DC motors (well, 4; but each pair is controlled by the same "signals") connected via a L298N, which is connected to the Arduino. In addition, I have a servo motor connected to the Arduino.

When I run the following program (in reality, it's a bigger program; however, I've managed to chiseled it down to the following):
#include <Servo.h>

const int servoPin = 3;
Servo servo;

const int LMotorsSpeedPin = 5;
const int RMotorsSpeedPin = 10;
const int LMotorDirectionBit1Pin = 7;
const int LMotorDirectionBit2Pin = 6;
const int RMotorDirectionBit1Pin = 8;
const int RMotorDirectionBit2Pin = 9;
const int MotorSpeed = 150;

void setup()
{
  // 1
  pinMode(servoPin, OUTPUT);
  servo.attach(servoPin);

  // 2
  pinMode(LMotorsSpeedPin, OUTPUT);
  pinMode(RMotorsSpeedPin, OUTPUT);
  pinMode(LMotorDirectionBit1Pin, OUTPUT);
  pinMode(LMotorDirectionBit2Pin, OUTPUT);
  pinMode(RMotorDirectionBit1Pin, OUTPUT);
  pinMode(RMotorDirectionBit2Pin, OUTPUT);

  // 3
  digitalWrite(LMotorDirectionBit1Pin, LOW);
  digitalWrite(LMotorDirectionBit1Pin, HIGH);
  analogWrite(LMotorsSpeedPin, MotorSpeed);

  // 4
  digitalWrite(RMotorDirectionBit1Pin, LOW);
  digitalWrite(RMotorDirectionBit1Pin, HIGH);
  analogWrite(RMotorsSpeedPin, MotorSpeed);

  // 5
  delay(10000);
}

void loop() {}


1 Sets up servo.
2 Sets up L298N pins.
3 Sets up left motor data.
4 Sets up right motor data.
5 Tests the code above for 10 [s].


I assume that you'll need some experience with the L298N; however, here's the gist.
*MotorsSpeedPin sends each motor what speed (MotorSpeed 0-255) they must go (via PWM).
Data into a pair of *MotorDirectionBit*Pin tell each motor what direction to go (i.e. back 01, stop 00 11, right 10).

When I run the program above, only the left motor runs. However, if I comment out 1 (which essentially renders the servo useless), both left and right motors start to work.
I find this problem quite odd. What does the servo code have to do with the motor code (and only one at that!)?

user4143: The Servo library uses Timer 1 to create the servo control signal. That means it can run the servo on any pin or combination of pins, and at the 50Hz speed that a servo likes best.
However, pins 9 and 10 also use Timer 1 to create the PWM signal.
It can't do both.
As soon as you start using the Servo library you lose PWM on pins 9 and 10.
So what can you do about it? Simply move your pins around to different pins. You can't use 9 and 10 for PWM, but you can use them for the servo. So put your servo on pin 10 and your right motor speed control on pin 3 and all should be happy.

23571: <p>I have 2 DC motors (well, 4; but each pair is controlled by the same "signals") connected via a L298N, which is connected to the Arduino. In addition, I have a servo motor connected to the Arduino.</p>

<hr>

<p>When I run the following program (in reality, it's a bigger program; however, I've managed to chiseled it down to the following):</p>

<pre><code>#include &lt;Servo.h&gt;

const int servoPin = 3;
Servo servo;

const int LMotorsSpeedPin = 5;
const int RMotorsSpeedPin = 10;
const int LMotorDirectionBit1Pin = 7;
const int LMotorDirectionBit2Pin = 6;
const int RMotorDirectionBit1Pin = 8;
const int RMotorDirectionBit2Pin = 9;
const int MotorSpeed = 150;

void setup()
{
  // 1
  pinMode(servoPin, OUTPUT);
  servo.attach(servoPin);

  // 2
  pinMode(LMotorsSpeedPin, OUTPUT);
  pinMode(RMotorsSpeedPin, OUTPUT);
  pinMode(LMotorDirectionBit1Pin, OUTPUT);
  pinMode(LMotorDirectionBit2Pin, OUTPUT);
  pinMode(RMotorDirectionBit1Pin, OUTPUT);
  pinMode(RMotorDirectionBit2Pin, OUTPUT);

  // 3
  digitalWrite(LMotorDirectionBit1Pin, LOW);
  digitalWrite(LMotorDirectionBit1Pin, HIGH);
  analogWrite(LMotorsSpeedPin, MotorSpeed);

  // 4
  digitalWrite(RMotorDirectionBit1Pin, LOW);
  digitalWrite(RMotorDirectionBit1Pin, HIGH);
  analogWrite(RMotorsSpeedPin, MotorSpeed);

  // 5
  delay(10000);
}

void loop() {}
</code></pre>

<ul>
<li><code>1</code> Sets up servo.</li>
<li><code>2</code> Sets up L298N pins.</li>
<li><code>3</code> Sets up left motor data.</li>
<li><code>4</code> Sets up right motor data.</li>
<li><code>5</code> Tests the code above for 10 [s].</li>
</ul>

<hr>

<p>I assume that you'll need some experience with the L298N; however, here's the gist.</p>

<p><code>*MotorsSpeedPin</code> sends each motor what speed (<code>MotorSpeed</code> 0-255) they must go (via PWM).
Data into a pair of <code>*MotorDirectionBit*Pin</code> tell each motor what direction to go (i.e. back <code>01</code>, stop <code>00</code> <code>11</code>, right <code>10</code>).</p>

<hr>

<p>When I run the program above, only the left motor runs. However, if I comment out <code>1</code> (which essentially renders the servo useless), both left and right motors start to work.</p>

<p>I find this problem quite odd. What does the servo code have to do with the motor code (and only one at that!)?</p>
 4143: <p>The Servo library uses Timer 1 to create the servo control signal. That means it can run the servo on any pin or combination of pins, and at the 50Hz speed that a servo likes best.</p>

<p>However, pins 9 and 10 also use Timer 1 to create the PWM signal.</p>

<p>It can't do both.</p>

<p>As soon as you start using the Servo library you lose PWM on pins 9 and 10.</p>

<p>So what can you do about it? Simply move your pins around to different pins. You can't use 9 and 10 for PWM, but you can use them for the servo. So put your servo on pin 10 and your right motor speed control on pin 3 and all should be happy.</p>
