🇺🇸Adafruit will not be shipping orders on Independence Day Monday 7/4/2022. Please allow extra time for your order to ship and plan accordingly.🇺🇸
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.

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

by animefruit on Sun Jan 23, 2022 3:43 pm

So I swapped the servos pins connected to the Arduino, and now with my normal code it does the opposite of what it did last time.

Now the servo now at pin 10 turns by itself and on the second turn, the servos turn at the same time, but when they were not swapped the servos turned at the same time but than on the second turn the servo that was at pin 9 turned by itself.

I wonder why it is doing this from me swapping the pins that the servos were connected to. What do you think?


Here is the code I used just in case you forgot:

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: 245
Joined: Tue Feb 25, 2020 1:04 pm

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

by animefruit on Sun Jan 23, 2022 11:18 pm

And yes, I do have potentiometers on the bottom of all my servos. I moved he potentiometer on the other servo that didn't work and now it works.

Now both of the servos move in opposite directions at the same time for both turns.

Thanks for the knowledge about the potentiometers on the servos or I wouldn't have fixed it.

animefruit
 
Posts: 245
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 24, 2022 12:01 pm

Awesome!

Keep in mind that I believe not all continuous servos have it. So if you're shopping for one, be sure to check.

Dave

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

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

by animefruit on Tue Jan 25, 2022 10:58 am

Ok. I will do that the next time I buy those servos.

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

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

by animefruit on Sun Mar 13, 2022 11:05 pm

Hello Dastels. Can you give me some help again? I ran this code that was working just fine but than all the sudden the other servo WAS NOT MOVING.

The only changes of the code that I did before this incident was that I changed the "delay(2000)" for both servos to "delay(1000)"

I commented on the changes in this 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);//was 2000
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  myservo1.attach(9);
  myservo2.attach(10);

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

  delay(2000);  //before my servo stopped I changed this to 1000

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


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

  delay(15);

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

  delay(2000); //before my servo stopped I changed this to 1000

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

  delay(1000);

  health = 500;
}     




Do you know if this change could have caused the problem?

How do I fix this?

animefruit
 
Posts: 245
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 Mar 14, 2022 11:31 am

Does the servo twitch or make noise? In my experience if the servo suddenly stopped working it is likely jammed or it's gears have been stripped (which can happen if it gets jammed).

Dave

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

Please be positive and constructive with your questions and comments.