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...
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?
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.
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);
}
//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
  }
 }
}
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.