Go Down

Topic: Trouble running two servos simultaneously in opposite directions (Read 2615 times) previous topic - next topic

drud1995

Hello, I really need some help figuring out how to make this work. I am trying to set my two servos up for a project I am working on so that the first servo goes from 90 to 150 degrees and back, and also a second servo that goes from 90 degrees to 30 and back at the same time. All I get with my code is a twitching of the servos, so any help is greatly appreciated.

Here is my current code:

Code: [Select]

/* 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
                // twelve servo objects can be created on most boards
Servo myservotwo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards
 
int pos = 90;    // variable to store the servo position
int postwo = 90;    // variable to store the servo position
 
void setup()
{
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
  myservotwo.attach(5);  // attaches the servo on pin 5 to the servo object
}
 
void loop()
{
  for(pos = 90; pos <= 150; pos += 1) // goes from 90 degrees to 150 degrees
  for(postwo = 90; postwo >= 30; postwo -= 1) // goes from 90 degrees to 30 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    myservotwo.write(postwo);              // tell servo 2 to go to position in variable 'postwo'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  delay(5000);
  for(pos = 150; pos>=90; pos-=1)     // goes from 150 degrees to 90 degrees
  for(postwo = 30; postwo <= 90; postwo -= 1) // goes from 30 degrees to 90 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    myservotwo.write(postwo);              // tell servo 2 to go to position in variable 'postwo'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}


Gerihatrick

I have not looked at your code but two thoughts spring to mind. The servos need to supplied with at least 5v and directly from a voltage source eg a battery and all the earths need to connected together to function properly if the arduino is powered separately. Regards

drud1995

I apologize for any confusion! Here is a fritzing diagram to give a better representation of how the board setup currently looks that I want to use my code with. Thanks!

drud1995

Ended up getting help from a friend. For the benefit of everyone who needs it, here is the code to run two mirrored servo motors simultaneously. Enjoy!
 
Code: [Select]

/* 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 servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // twelve servo objects can be created on most boards
 
int pos = 90;    // variable to store the servo position
 
void setup()
{
  servo1.attach(8);  // attaches the servo on pin 9 to the servo object
  servo2.attach(7);  // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
  for(pos = 90; pos >= 19; pos --){
  servo1.write(pos);
  servo2.write(180-pos);
  }
  //delay(5000);
  for(pos = 19; pos <= 90; pos ++)     // goes from 19 degrees to 90 degrees
  {                               
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    servo2.write(180-pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}


AWOL

Code: [Select]
for(pos = 19; pos <= 90; pos ++)     // goes from 180 degrees to 90 degrees
If the comment doesn't describe the code in a meaningful way, it's better that it's not there at all.

UKHeliBob

Quote
the first servo goes from 90 to 150 degrees and back, and also a second servo that goes from 90 degrees to 30 and back at the same time
The clue is in the last 4 words.  You need to use a single for loop.

Code: [Select]

for (int pos = 90; pos <= 150; pos++)
{
  Servo1.write(pos);  // 90 to 150
  Servo2.write(180 - pos);  //90 to 30
  delay(someDelay);  //wait a little at each position
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Powering the servos from the Arduino is NOT good for the Arduino. They suck way more current than the Arduino can safely supply.

Go Up