Go Down

Topic: Two servo running simultaneously in opposite direction.  (Read 1 time) previous topic - next topic

Aswinth

Hi all,

I am a beginner in arduino programming. I tried to write a program to run two servo motors (Hobby motors - Vega robotics V3003) in opposite direction. That is one from 0-180deg and another from 180-0 degree.

The code which i wrote works well but the problem is both motors does not rotate simultaneously, only after the first servo moves from 0-180 the second servo moves from 180-0. I am using arduino mega board.

Please help me with the code, thanks in advance

jremington

Post the code, as described in "How to use this forum".
I hope you are not trying to power the servos from the Arduino.

zoomkat

A very simple example of getting servos to rotate in opposite directions.

Code: [Select]

//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);
  myservo2.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
    int n = readString.toInt();  //convert readString into a number
    myservo1.write(n);
    myservo2.write(180-n); //turns opposide servo in desired direction
    readString="";
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

MarkT

using delay() while reading Serial is a fundamental misunderstanding of I/O - read till the end
of line or separator character, using if, not while.  When you see the separator
then process the chars so far, not before.  Calling delay just means you code won't work at high
baud rates!
Code: [Select]

void loop ()
{
  if (Serial.available ())
  {
    char c = Serial.read () ;
    if (c == '\n')   // assume newline is separator
    {
      int n = readString.toInt () ;
      myservo1.write (n) ;
      myservo2.write (180-n) ;
      readString = "" ;
    }
    else
      readString += c ;
  }
  ... any other stuff running in parallel
}
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

zoomkat

Quote
using delay() while reading Serial is a fundamental misunderstanding of I/O
Not really, it gets around having to add some type of end of data delimiter for simple testing purposes. Also, the code I posted compiles, what happened to yours?   8)
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

MarkT

Perhaps you don't understand that "...  any other stuff running in parallel" is a hint that
my way interworks with other code nicely in a way your's cannot.  Yes it doesnt compile.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

zoomkat

Perhaps you don't understand that "...  any other stuff running in parallel" is a hint that
my way interworks with other code nicely in a way your's cannot.  Yes it doesnt compile.
And what do you not understand in the statement "A very simple example of getting servos to rotate in opposite directions."?  Work on getting yours to compile!  8)
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

hieloiceberg

I know little knowledge of coding but swapping the wires on the motor and potentiometer (inside the servo) did it for me.

nielyay

Hi.
Quote
That is one from 0-180deg and another from 180-0 degree.
Please input your code.

Code: [Select]


#include <Servo.h>

Servo myservo;

int pos = 0;   

void setup()
 {
  myservo.attach(9); 
 }

void loop() {
  for (pos = 0; pos <= 180; pos += 1)
 {
    myservo.write(pos);             
    delay(15);                   
  }
  for (pos = 180; pos >= 0; pos -= 1)
 {
    myservo.write(pos);             
    delay(15);                     
  }
}


*use the simple code on the Arduino software toolbars. Examples > Servo > Sweep

Go Up