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.
A very simple example of getting servos to rotate in opposite directions.
//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="";
}
}
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!
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
}
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)
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.
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.
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)
Servo myservo; // create servo object to control a servo
Servo myservo2;
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int rev = 0;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10);
}
void loop() {
rev=180;
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(rev); // reverse the position
rev--;
delay(15); // waits 15ms for the servo to reach the position
}
rev=0;
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(rev); // reverse the position
rev++;
delay(15); // waits 15ms for the servo to reach the position
}
}