2

I need to control 2 servos in parallel, whereas one is of continuous rotation and another is of regular positioning control (angle) on arduino IDE. I tried but it works one after another, not parallel. How can I do that, please suggest. Thanks.

2
  • 1
    Please post the code that is not working as expected in a minimum example. Then I can try to help. – Kerbolosh May 4 at 6:57
  • you can only control one at a time. You can switch between both servo after, so that, it seems to be happening in parallel – ArduinoFan May 4 at 8:20
4

I am proposing one answer

https://wokwi.com/arduino/projects/297646292038844936

Here, we are not using the delay function. We are using the millis() Based on your actual application, you can control individual servo accordingly.

Here is the Arduino simulation: https://wokwi.com/arduino/projects/297646292038844936 enter image description here

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo

// twelve servo objects can be created on most boards
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 15;           // interval at which to blink (milliseconds)

int pos = 0;    // variable to store the servo position
int pos2 = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 9 to the servo object

}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;


    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    pos = pos + 1;
    if (pos > 180) {
      pos = 0;
    }                      // waits 15ms for the servo to reach the position

    myservo2.write(pos2);              // tell servo to go to position in variable 'pos'
    pos2 = pos2 + 1;
    if (pos2 > 90) {
      pos2 = 0;
    } 



  }


}
2

The angel of two Servomotors should be same ? If yes, Try this for 180 deg. Servos : You can connect signal pin of ech servo to a specific PWM pin of Arduino like pin number '9' in this example It's just for test but you can change this how you want to

I hope it will work you, Good luck

#include <Servo.h> 

Servo myservo;

int PWMpin = 9;  // change this for your circuit configuration

int a = 0;

void setup() {
  myservo.attach(9);
  myservo.write(0);  // set servo to mid-point
}
void loop() {
  for (int i = 0; i < 180; i++) {
   myservo.write(i);
    delay(10);
  If (i == 180) {
    analogWrite(PWMpin, a);
    }
  }
}
3
  • 2
    Please don't post code as a image. – Python Schlange May 4 at 7:42
  • Thanks @Hamid 'Smith' Salehi for your reply. well, the servos I am using are not the same, i mean one is of continuous rotation and one is regular, so what i wish to have it like the continuous rotation servos should be rotate with the regular servo which should run between 0~180 deg in parallel. ans these are connected to pin 9 and 10 respectively. – Emlinux May 4 at 8:05
  • The only difference of servomotors is their rotation limitations? Both of them works by standard PWM ? – Hamid 'Smith' Salehi May 4 at 8:34
2

As i know, there is no way for a single arduino or any other microcontrollers to command on pins in exactly same time and it's because of microcontrollers nature. When you have to command 2 outputs Microcontroller will be act like this :

Command 1 .... To pin number 9 Command 2 .... To pin number 10

and for example if your microcontroller, like arduino or others do each command in one complete clock cycle and your clock frequency will be 16 MHz, so each cycle's time period is 62.5 nano-seconds.

So your microcontroller will do your command in first clock cycle and do the second command in next clock cycle, In the other words, command 2 will do 62.5 nano-second after command one (if each those commands takes just one clock cycle) but in normal 16 MHz arduino boards it takes much much more time to do commands like this and it was just an example to understand it's principle, and because of this reason i suggest you to connect both servomotors into same output PWM pin if you are using two standard servomotors; But the main issue is different angle limitations of your servomotors and it's impossible to control them when you want one of them to rotate 360 deg. and the other must rotate 180 deg. . If you connect those signal pin to a same output, you have two options with two issuse. First, if you command to the output pin to turn 360 deg. , All things will be good but when your output pin command for angles more than 180 deg. , The little servo will stop. Second, if you limit your rotation to 180 deg., You will not be able to rotate 360 deg. Your bigger servomotor.

I hope my description will be useful and face you by your complete pattern of what's going on in this project's issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.