Coding two servo motors

We are making a drawing machine. We need help with creating a code to make two servo motors turn different wheels at varying speeds to make a pen draw a one line closed shape (a non perfect blob/circle shape is our aim like a blob fish!). The code we are using at the moment is not working and as we know little about coding we aren’t sure why maybe you can explain. Maybe if you can show us through a template code how to make these different commands we can put the angles and speeds in ourselves?

Here is the code we tried to make work - if you see we are trying to make a continuous rotation but the wheels move at different speeds so that our pen will draw curves on the paper.

#include <Servo.h>

Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo

void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000);
forward();
delay(2000);
forward();
delay(2000);
forward();
delay(2000);
forward();
delay(2000);
stopRobot();
delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(200);
servoRight.write(200);
}
void forward() {
servoLeft.write(200);
servoRight.write(260);
}
void forward() {
servoLeft.write(200);
servoRight.write(290);
}
void forward() {
servoLeft.write(200);
servoRight.write(200);
}
void forward() {
servoLeft.write(81);
servoRight.write(200);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}

this is what arduino is telling us
sketch_nov06a.ino: In function ‘void forward()’:
sketch_nov06a:31: error: redefinition of ‘void forward()’
sketch_nov06a:27: error: ‘void forward()’ previously defined here
sketch_nov06a.ino: In function ‘void forward()’:
sketch_nov06a:35: error: redefinition of ‘void forward()’
sketch_nov06a:27: error: ‘void forward()’ previously defined here
sketch_nov06a.ino: In function ‘void forward()’:
sketch_nov06a:39: error: redefinition of ‘void forward()’
sketch_nov06a:27: error: ‘void forward()’ previously defined here
sketch_nov06a.ino: In function ‘void forward()’:
sketch_nov06a:43: error: redefinition of ‘void forward()’
sketch_nov06a:27: error: ‘void forward()’ previously defined here
redefinition of ‘void forward()’

You've got multiple instances of the function "forward", all doing different things. You can't do that.

but the wheels move at different speeds so that our pen will draw curves on the paper.

What I see is code that will not compile.

void forward() {
  servoLeft.write(200);
  servoRight.write(200);
}
void forward() {
  servoLeft.write(200);
  servoRight.write(260);
}
void forward() {
  servoLeft.write(200);
  servoRight.write(290);
}
void forward() {
  servoLeft.write(200);
  servoRight.write(200);
}
void forward() {
  servoLeft.write(81);
  servoRight.write(200);
}

You have more than one forward function in your program. Which one would you like the program to use ? Note the values written to the 2 servos. They may need to be different to allow for the different rotation speeds of the servo.

A suggestion. Use a forward function that takes parameters for the speed of each servo, such as

void forward(byte speedLeft, byte speedRight)
{
  servoLeft.write(speedLeft);
  servoRight.write(speedRight);
}

and call it like this

forward(100, 200);

#include <Servo.h>

Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo

void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000);

}

// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(151);
servoRight.write(79);

}

So we have this code now and it works! BUT i am still confused as to how to make different speeds within one command ? so for example first one motor 70 then 90 then back to 70 again and the other 120 then 100 then 120 again ?

i am still confused as to how to make different speeds within one command ?

Did you read my suggestion to use a function that is passed 2 parameters ? Call it with the required left and right speeds as and when required.

As you mention servos and speeds I assume that you are using so called “continuous rotation servos”.

UKHeliBob: Did you read my suggestion to use a function that is passed 2 parameters ? Call it with the required left and right speeds as and when required. As you mention servos and speeds I assume that you are using so called "continuous rotation servos".

I was about to ask the very same question. And I also wanted to remind eszter that only values between 0 and 180 are valid, with 90 being 'centre' on a positional servo or 'stop' on a continuous-rotation type. All of those 200 and 260 values in the original posted code are invalid.

So eszter, are they continuous-rotation? We need that point clarified.

Also, use code tags as described in #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html