Question on controlling servos.

I have made a little program loop to make the robot move in a square. I may have messed up somewhere because it just goes in a straight line forever. One of the servos is on backwards and thats the only way that I could have done it. That is why the code has negatives in it. Thanks in advance for your help. Here is my code:

#include <Servo.h>
Servo servoOne; // Define our Servo
Servo servoTwo;
void setup()
{
servoOne.attach(10);
}

void loop() {
servoTwo.attach(9); // Attach and apply power
servoOne.write(-180);
servoTwo.write(180);
delay(1000); // Allow transit time
servoTwo.detach();
servoOne.write(90);
}

Also I have a Leonardo board

90 is stop. 0 to 89 moves the servo in one direction and 91 plus moves it the other way.

Mark

And how do you control how far they go? Thanks that explains why I was having errors
Edit: I have continuous motion servos

tfricks:
And how do you control how far they go? Thanks that explains why I was having errors
Edit: I have continuous motion servos

Unless the last command position you send to the servo library was a 90 (or whatever the exact 'stop position' value for your specific servo is) the servo will turn continuously in one direction or the other depending on if the last value sent was larger or less then the stop value. The further the value is from the stop value the faster the servo will turn in that position.

Lefty

Just for info, the below is one way to get opposite side continuous rotation servos to move together at the ~same speed.

int n = readString.toInt();
myservo1.write(n);
myservo2.write(180-n);

Why are you attaching and detaching one of the servos in loop()? Why not both of them? Or, the more correct, none of them?

PaulS:
Why are you attaching and detaching one of the servos in loop()? Why not both of them? Or, the more correct, none of them?

I agree, esp about detaching servos. A 'detached servo' is operating in a undefined state as no servo manufacture characterizes how their servo will behave with power applied but with no control pulses being sent, they are simply not designed to operate in that mode.

Lefty

Some time back I tried the detach function with a servo, and the result was the servo behaved just like it was not connected to the arduino. Detaching a diy type of continuous rotation might be a handy thing.