Controlling a DC-motor simultaneously with two servos

Hello everyone :slight_smile:

I've had some trouble controlling two servos and a DC motor at the same time. When I run my code (I switch between the 3 of them and use the servo library for each of them (that is the DC motor is connected to an ESC so I can use the servo library)) I can only run one at a time. Whenever I run the DC motor and I choose to send a signal to one of the servos the DC motor just stops running altogether as if the signal from that pin just stopped...

Can anyone help?

Thanks in advance

Hello everyone :slight_smile:

I need to control a DC motor and a servo at the same time, I need the motor to run continously and the servo to run when I tell it to run. Both are controlled using the servo library (DC motor is connected to an ESC which can be controlled by PWM).
Whenever I run my program I tell the motor to start running but when I need to send a PWM signal to my servo, the PWM from the pin on which the motor is connected stops... Is there anyway to avoid this?

Thanks in advance
Have a nice day :slight_smile:

Is there anyway to avoid this?

Yes. You have not provided near enough information, though. Things like which ESC you are using, which servo, how either are powered or what code you are running.

Sorry,

The ESC I'm using is this one : Dynam Detrum 18A ESC(3A BEC) 60P-DYE-1001-18A-ESC

and the two servos : http://www.ercmarket.com/flightline-fls-80-fls-80.html

The whole thing is powered by an 11.1 V LiPo battery, I've used some voltage regulators to scale down the voltage to 5 V for the servos.

The code I'm currently using is this:

#include <Servo.h>


//NOTE: set pulse width to 1 ms to stop, 2 ms for full speed. 1 ms = 45, 2 ms = 135

Servo myservo;

String readString;


void setup()

{
  //start by arming the motor

// Motor pin
  myservo.attach(7);
  Serial.begin(9600);

}

// The value 45 corresponds to arming the ESC
void Arm() { 
  myservo.write(45); 
  delay(2000);
  Serial.println("Armed!");
}

void rightFlap()
{
//Servo1 attached to pin8
 myservo.attach(8);
}
void leftFlap()
{
//Servo2 attached to pin9
myservo.attach(9);
}


void loop() 
{ 

  while (Serial.available()) {
    delay(10);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string

    if(readString=="A") {
      Arm();
      readString="";

    }  

    else if(readString == "R")
    {
      Serial.println("Right flap chosen");
      rightFlap();
      readString="";
    }

    else if(readString == "L")
    {
      leftFlap();
      readString = "";
    }

    else if(readString == "M")
    {
      Serial.println("Motor chosen");
      myservo.attach(7);
      readString= "";
    }

    else {
      int n;
      char carray[4]; //converting string to number
      readString.toCharArray(carray, sizeof(carray));
      n = atoi(carray);


      readString="";
      Serial.println("Number: "); 
      Serial.println(n); 


      myservo.write(n); 
    }
  }

  delay(10);

}

That isn't very much information. Please read this: http://arduino.cc/forum/index.php/topic,148850.0.html

You need to supply information like the code you are using, schematics of your circuit, information about your components, etc.

I see multiple attaches, but very few detach.

Edit: I've probably said this before, but I'm going to repeat it to make sure the message gets through.

DO NOT CROSS-POST. IT WASTES TIME.

Topics merged.

Have a nice day.

DO NOT CROSS-POST. IT WASTES TIME.

sorry I don't understand what you mean?

edit

I see what you mean now, I thought I had removed that topic before posting here

If you want to control all three servos at the same time you need to define 3 separate servo objects - such as

Servo motor;
Servo servo1;
Servo servo2;

motor.attach(pinX);
servo1.attach(pinY);
servo2.attach(pinZ);

And then write appropriate numbers to the appropriate devices - e.g. motor.write(65).

...R

Multiservo test code.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Thank you zoomkat and Robin2 that really helped ! :slight_smile:

Hi, im really new at this and was looking to see if someone can help me out here. I need a code to run both these motors at the same time using these parts.

Motors

Arduino Uno

Pololu MotorShield

The shield page has a link to an Arduino library with a demo sketch. Was it really too much trouble to try that first?