DC motor and servo on one Arduino


I have included part of my code to program an autonomous robot. I am using two dc motors and two servos. My problem is that using the Servo library function attach() appears to prevent the dc motor from running. However, if I comment only those two lines that use attach, the motor works again. This happens despite the fact that no servo is attached to the Arduino.

For the record, I am using an external power source for both types of motors. I am also using three proximity sensors with 5V.

Is it even possible to run dc motors and servos from the same Arduino? If not, could anyone suggest what could be the cause of this issue.

#include <Servo.h>

Servo myservo;   
Servo myservo2; 
int pinI1 = 7;//define I1 interface
int pinI2 = 11;//define I2 interface 
int speedpinA = 9;//enable motor A
int pinI3 = 12;//define I3 interface 
int pinI4 = 13;//define I4 interface 
int speedpinB = 10;//enable motor B
int spead = 210;//define the speed of motor
int pingPin = 2;
int val;
int leftVal;
int rightVal;
int leftDistPin = 1;
int rightDistPin = 2;
int stackPin1 = 4; //I1 for stacking motor
int stackPin2 = 5; //I2 for stacking motor
int stackEnable = 3; //Enable stacking motor
int stackSpeed = 50; //Determine how far to lift arm during stacking
int counter = 0; //Keeps track of cans stacked
unsigned int duration, inches;

void setup()
 //Set dc motors
  //Set servos


//Motor logic functions not shown

void loop()
  val = readPing();
  leftVal = analogRead(leftDistPin);
  rightVal = analogRead(rightDistPin);
  Serial.print("leftVal: ");
  Serial.print("rightVal: ");
  if(val < 10) {
  else if(leftVal > 200) {
  else if(rightVal > 200) {
  else {

Thanks in advance.

It is difficult to tell what your code is doing. "I2 interface" means nothing to me.

If you are using some kind of H-bridge device to power your motor, then it is probably using PWM.

You also are using some kind of PWM to drive the servo control signal.

You can generate PWM on an arduino yourself purely in software, or with the use of the fancy built-in timer features. With the latter, the problem is that the servo library and the motor driver code you are presumably using, may be trying to use the same arduino timer inconsistently.

Says here that the servo library precludes PWM-ing pins 9 and 10... you're using both of them for motor speed.