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

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

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

Post by animefruit »

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

            #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;
}                    

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

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

Post by animefruit »

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.

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

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

Post by dastels »

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

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

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

Post by animefruit »

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

User avatar
animefruit
 
Posts: 332
Joined: Tue Feb 25, 2020 1:04 pm

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

Post by animefruit »

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

         #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?

User avatar
dastels
 
Posts: 15667
Joined: Tue Oct 20, 2015 3:22 pm

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

Post by dastels »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”