Pages: [1]   Go Down
Author Topic: DC motor and servo on one Arduino  (Read 808 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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.

Code:
#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()
{
  Serial.begin(9600);
 //Set dc motors
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
  //Set servos
  myservo.attach(5);
  myservo2.attach(6);
}

...

//Motor logic functions not shown

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

Thanks in advance.
Logged

Offline Offline
Edison Member
*
Karma: 28
Posts: 2045
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Retired from Arduino.
PMs are set to ignore all.

Pages: [1]   Go Up
Jump to: