Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

How do I make my 2 servo motors move in opposite directions?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How do I make my 2 servo motors move in opposite directions?

by animefruit on Wed Jan 12, 2022 2:19 pm

Hello. This is Neal Cole.


I am trying to make 2 servo motors move in opposite directions.

I need them to move in opposite directions so that when they are facing each other they will go the same direction.

I looked up on the internet how to make 2 servos move in opposite directions.

I got the code from the very bottom of this page:

http://embeddedforus.blogspot.com/2016/ ... otors.html

I edited the code so that the servo motors on pin 9 and the servos motor for pin 10 move in opposite directions.

I also connected the yellow-ish cord to pin 10 for the servo that is supposed to be connected to pin 10.



Here is the code:

Code: Select all | TOGGLE FULL SIZE
                  #include <Servo.h>
#include "Wire.h"
Servo servoLeft; // Declare left servo signal
Servo servoRight; // Declare right servo signal

void setup() // Built in initialization block
{
servoLeft.attach(9); // Attach left signal to pin 13
servoRight.attach(10); // Attach right signal to pin 12

servoLeft.writeMicroseconds(1700); // 1.7 ms -> counterclockwise
servoRight.writeMicroseconds(1300); // 1.3 ms -> clockwise
}

void loop() // Main loop auto-repeats
{ // Empty, nothing needs repeating
}     
         


But the servo motor on pin 10 does not move!

Can you tell me why its not working or give me some code that will do the same without making my servo on pin 10 not work?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Wed Jan 12, 2022 3:29 pm

What MCU board are you using?
What servos are you using?
Are your connections good?
Is power going to both servos?
If you swap the servos, which one doesn't turn?
Have you tried changing the values to write?

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Sat Jan 15, 2022 12:51 am

I am using a Arduino Nano.
I am using Continous micro servos

I actually figured it out. Pin 10 wasn't soldered to the other servo. That is why that servo wasn't moving. Now it does work.
But when I ran the code the servos still do not go in opposite directions. They go in the same directions.

The reason to get these servos to move in opposite directions is because they are going to be facing towards each other.
And my real code I want to use has these sensors that are hit and after they are touched to much the servos move.

I managed to use that code with servos only using one digital pin.
The reason why I use 2 digital pins in this project is to tell the other servo to go the opposite direction while the 2 other servos move in the same direction.

Here is my code for using 1 digital pin:

Code: Select all | TOGGLE FULL SIZE
     #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo;
const int knockSensor = A0;

const int threshold = 50;
int health=500;
int sensorReading = 0;


int pos;


void setup()

{
 pinMode(LED_BUILTIN, OUTPUT);
 
  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
 
 lcd.begin (20 ,4);
  lcd.clear();
 
 
  Serial.begin(9600);
  myservo.attach(9);
 myservo.write(0);

}

void loop()
{
do
  {
     

    myservo.detach();
   
   
  sensorReading = analogRead(knockSensor);
   
 
   lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health);
   
   if (sensorReading > threshold) {
   
    Serial.println("Knock!");
health=health-sensorReading;
 


   
    Serial.print(sensorReading);   
    lcd.print(sensorReading);

   
    int sensorValue = analogRead(A0);
 
  Serial.println(sensorValue);
  delay(1);   
 
   } 

 
   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);             
myservo.attach(9);

  delay(15);
  myservo.write(1);
 
  delay(1000);
 
  myservo.detach();

  delay(6000);
  myservo.attach(9);
 
  delay(15);
  myservo.write(179);
 
  delay(1000);
  myservo.detach();
 
  delay(1000);
  health=500;
}

           



I attempted to do the code for 2 servos having the same digital pin and the other servo having a different digital pin.
When I tried this the other servo motor did not move.
Here is that code ( I will have comments in slashes):


Code: Select all | TOGGLE FULL SIZE
     #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;//other servo for other digital pin

const int knockSensor = A0;

const int threshold = 50;
int health=500;
int sensorReading = 0;


int pos;


void setup()

{
 pinMode(LED_BUILTIN, OUTPUT);
 
  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
 

 lcd.begin (20 ,4);
  lcd.clear();
 
 
  Serial.begin(9600);
  myservo1.attach(9);
 myservo1.write(0);

myservo2.attach(10);//connected other servo to pin 10
myservo2.write(180);


 

}

void loop()
{
do
  {
     

    myservo1.detach();
    myservo2.detach();
   
  sensorReading = analogRead(knockSensor);
   
   
   lcd.setCursor(0, 1);
 Serial.println(health);
 lcd.println(health);
   
   if (sensorReading > threshold) {
   
    Serial.println("Knock!");
health=health-sensorReading;
 


   
    Serial.print(sensorReading);   
    lcd.print(sensorReading);

   
    int sensorValue = analogRead(A0);
 
  Serial.println(sensorValue);
  delay(1);   
 
   } 

 
   } while(health>1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);   
  delay(1000);             
myservo1.attach(9);
myservo2.attach(10);

  delay(15);
  myservo1.write(1);
  myservo2.write(1);
 
  delay(1000);
 
  myservo1.detach();
  myservo2.detach();

  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);
 
  delay(15);
  myservo1.write(179);
  myservo2.write(0);//guessing the point should be opposite from the other so I chose 0
 
  delay(1000);
  myservo1.detach();
  myservo2.detach();
 
  delay(1000);
  health=500;
}         



With this code it works just like the other code, but the other servo motot that is supposed to be moving the opposite direction isnt turning at all.

Can you tell me how to fix this in my code?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Sat Jan 15, 2022 12:02 pm

First try formatting it (there's a format option in one of the Arduino menus) so that it's readable.

Driving more than one servo on a pin is... interesting. But likely problematic. There's no gaurentee that servos will behave identically to the same control signal. If you need them to be in sync or turn at the same speed you might have to do some tweaking to them individually.

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Mon Jan 17, 2022 1:43 am

I made some progress with my new code.
I made it to where both servos turned at the same time in opposite directions in the first direction they did, but in the second direction the servo at pin 9 was the only servo that turned.

Here is my code(I Auto Formatted it and I have comments that tell more about what I found out about the code):

Code: Select all | TOGGLE FULL SIZE
   #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;//other servo for other digital pin

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);

  myservo2.attach(10);//connected other servo to pin 10
  myservo2.write(179);




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(0);//I think this goes opposite direction from servo on pin 10
  myservo2.write(179);

  delay(1000);

  myservo1.detach();
  myservo2.detach();

  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(179);//I think this servo is SUPPOSED to go the opposite direction as servo on pin 10,but insead the servo at pin 9 is the only one that moves
  myservo2.write(0);

  delay(1000);//this delay part seems to be where the pin 9 servo turns, without this the servo at pin 9 does not move
  //and if you increase the delay number the servo at pin 9 turns the same direction for a longer amount of time

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}                 




It has been a long time since I looked up where I got the code for moving the servo at the bottom of this code is from, but I found it and it has comments on it to explain what the code is doing. I got it from this link:
https://arduino.stackexchange.com/questions/4076/what-is-commonly-done-to-stop-a-servo-after-reaching-desired-position

Here is the code from that website I used in my bottom part of my code:

Code: Select all | TOGGLE FULL SIZE
     #include <Servo.h>
Servo myservo;  // create servo object to control a servo

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

void loop() {
  myservo.attach(9);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(1);
  // sets the servo position according to the scaled value
  delay(1000);
  // waits for it to get to the position
  myservo.detach();
  delay(1000);
  myservo.attach(9);
  // attaches the servo on pin 9 to the servo object
  delay(15);
  myservo.write(179);
  // sets the servo position according to the scaled value
  delay(1000); // waits for it to get to the position
  myservo.detach();
  delay(1000);     


Can you figure out why my servos do go in the opposite directions for the first direction but than in the second direction only one servo turns?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Mon Jan 17, 2022 10:15 am

That code is for regular servos whereas you're using continuous servos.

Your problems might be due to the repeated attach/detach. I'm not sure why the code does that but it seems to be:
- attach to the servo
- set it's position
- wait for it to get there
- detach

This makes some sense with regular servos since it doesn't need the PWM signal if you just need it to stay in place. But continuous need a continuous PWM signal to keep informing it of it's speed (i.e. it's "position").

Get rid of the attach/detach code other than the initial attaches and see if that fixes it.

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Mon Jan 17, 2022 2:01 pm

I removed some of the attach and detach code that was not needed. But when I did this it makes it to where both servos go opposite directions for the first turn, but non of them do the turn in the other direction.

Here is my code, I commented out some of the detach and attach except the ones that were needed. I commented on the code I commented out so you can see my thoughts:

Code: Select all | TOGGLE FULL SIZE
     #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  //myservo1.attach(9);      // I commented this out because the servo isn't supposed to move here anways
  //myservo1.write(0);

  // myservo2.attach(10);
  // myservo2.write(179);




}

void loop()
{
  do
  {


    //myservo1.detach();         //I did not attach it now that I commented out he attaches above so I do not need any detaches
    //myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9); // I need to attach it here because ther servos need to move
  myservo2.attach(10);

  delay(15);
  myservo1.write(0);
  myservo2.write(179);

  delay(2000);

  //myservo1.detach();
  // myservo2.detach();   // I removed the detach and attach for this part because I think that is un-needed detach and attach code because:
                          //I think detach should just be used in the the botto of the code

  // delay(6000);
  // myservo1.attach(9);
  //myservo2.attach(10);

  //delay(15);
  myservo1.write(179);
  myservo2.write(0);

  delay(2000);

  myservo1.detach();  // the servo should detach so that it can attach next time through the loop
  myservo2.detach();

  delay(1000);

  health = 500;
}         



What do I do about this?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Mon Jan 17, 2022 2:07 pm

I would leave the attaches in setup and get rid of all the attaches and detaches in loop.

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Mon Jan 17, 2022 5:35 pm

Ok. I did that. When I do that, it does this the servos just run in opposite directions continuously.

Here is my code:

Code: Select all | TOGGLE FULL SIZE
    #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);     
  myservo1.write(0);

  myservo2.attach(10);
  myservo2.write(179);




}

void loop()
{
  do
  {


    //myservo1.detach();
    //myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  // myservo1.attach(9);
  //myservo2.attach(10);

  delay(15);
  myservo1.write(0);
  myservo2.write(179);

  delay(2000);

  //myservo1.detach();
  // myservo2.detach();


  // delay(6000);
  // myservo1.attach(9);
  //myservo2.attach(10);

  //delay(15);
  myservo1.write(179);
  myservo2.write(0);

  delay(2000);

  // myservo1.detach();
  // myservo2.detach();

  //delay(1000);

  health = 500;
}       


I found a video that explains that detach is used to stop the continuous servo. So that is why they have to detach and attach again.

Here is the video's link( edited it for a better link to use): https://www.youtube.com/watch?app=desktop&v=k5uPYfZ6u7E

So knowing that, how do I get the other servo to move in opposite directions in both turns?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Mon Jan 17, 2022 5:45 pm

Yes, it will stop it if that's what you want. For regular servos it will stop them from potentially jittering.

I'm starting to see what you're trying to do. So put the attaches & detaches back in.

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Wed Jan 19, 2022 11:25 pm

When I do that, it does the same exact thing as when I asked you first. One servo on pin 9 turns the first direction and the second direction. And the other servo on pin 10 turns on only the first direction. What do I do?

Just so you know, here is my code:

Code: Select all | TOGGLE FULL SIZE
       #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(9);
  myservo1.write(0);

  myservo2.attach(10);
  myservo2.write(179);




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(0);
  myservo2.write(179);

  delay(2000);

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(9);
  myservo2.attach(10);

  delay(15);
  myservo1.write(179);
  myservo2.write(0);

  delay(2000);

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}           

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Wed Jan 19, 2022 11:31 pm

Yes, but now I have a better idea of what you're trying to do. The code looks like it should behave like you want. Have you tried swapping the servos to see if the problem goes with the servo that won't reverse? That'll help narrow down the problem.

A thought: do your servos have centering setscrews? That might be all the way ito one direction on the servo that doesn't reverse.

Dave

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Fri Jan 21, 2022 11:48 pm

So I swapped the servos pins and degrees in the code if that is what you meant by swapping servos.

Here is my code with the swapped pins and degrees(I commented what I changed):

Code: Select all | TOGGLE FULL SIZE
   #include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"


Adafruit_LiquidCrystal lcd(0);

Servo myservo1;
Servo myservo2;

const int knockSensor = A0;

const int threshold = 50;
int health = 500;
int sensorReading = 0;


int pos;


void setup()

{
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");


  lcd.begin (20 , 4);
  lcd.clear();


  Serial.begin(9600);
  myservo1.attach(10);//swapped pins
  myservo1.write(179);// swapped degrees

  myservo2.attach(9);//swapped pins
  myservo2.write(0);//swapped degrees




}

void loop()
{
  do
  {


    myservo1.detach();
    myservo2.detach();

    sensorReading = analogRead(knockSensor);


    lcd.setCursor(0, 1);
    Serial.println(health);
    lcd.println(health);

    if (sensorReading > threshold) {

      Serial.println("Knock!");
      health = health - sensorReading;




      Serial.print(sensorReading);
      lcd.print(sensorReading);


      int sensorValue = analogRead(A0);

      Serial.println(sensorValue);
      delay(1);

    }


  } while (health > 1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(10);// I swapped these pins
  myservo2.attach(9);

  delay(15);
  myservo1.write(179);// I swapped these degrees
  myservo2.write(0);

  delay(2000);

  myservo1.detach();
  myservo2.detach();


  delay(6000);
  myservo1.attach(10);//I swapped these pins
  myservo2.attach(9);

  delay(15);
  myservo1.write(0);//I swapped these degrees
  myservo2.write(179);

  delay(2000);

  myservo1.detach();
  myservo2.detach();

  delay(1000);

  health = 500;
}                                                 


When I use this code it still does the same thing.

Did you mean to swap the servos actual cords that are connected to the pins on the Arduino?

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by animefruit on Sat Jan 22, 2022 12:22 am

I don’t know what you mean by centering setscrews.

But I did put a screw from the package my servo came with into that servo attached to pin 9 to hold together a piece of plastic on top of the “horn” that is supposed to be attached to the servo. I didn’t know what the screw was for that came with the servo.

I did not do this to the other servo that was attached to pin 9.

And I did not do that to the servo that is attached to pin 10.

Here is a picture of my servo that has the screw that hold the 2 pieces of plastic together:
Attachments
F4C91E89-C51F-4B3C-B4B5-EB75ABD5242D.jpeg
F4C91E89-C51F-4B3C-B4B5-EB75ABD5242D.jpeg (956.87 KiB) Viewed 154 times

animefruit
 
Posts: 231
Joined: Tue Feb 25, 2020 1:04 pm

Re: How do I make my 2 servo motors move in opposite directi

by dastels on Sat Jan 22, 2022 1:21 pm

On most continuous servos there's a small potentiometer that sets the stop point, usually the midpoint. I attached photo of the adjustment access hole on continuous regular (Fintec FS103R) and micro (Fintec FS90R) servos.

Dave
Attachments
2022-01-22 12.08.54.jpg
2022-01-22 12.08.54.jpg (7.16 MiB) Viewed 146 times
2022-01-22 12.08.27.jpg
2022-01-22 12.08.27.jpg (6.95 MiB) Viewed 146 times

dastels
 
Posts: 9705
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.