controling more Servos at the same time coding trouble

Hi guys

I’m working with some servoes at the moment and I need some of your awesome advises.
I want to control 2-3 Servos at the same time with 2-3 potentiometers. I can make it work when I use Processing or MAX/ msp with my arduino. but i also need it to work only with the Arduino.

To begin I just want each servo to sweep between 0-180 degrees. The speed of each servo should be controlled by one of the 2-3 potentiometers. so servo1 is controlled by pot1 and servo2 is controlled by pot2
I got this working but the problem for me is that they take one servo at a time and i need it to be done at the same time.
So my question is how I combine this code so they all can be controlled at the same time.

hope you can help.
All the best
-Ken

//Build on the Servo Example Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
Servo myservo1; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int potpin1 = 1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int val1; // variable to read the value from the analog pin

int pos = 0; // variable to store the servo position
int pos1 = 0;

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
}

void loop()
{

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree

val=analogRead(potpin);
val=map(val,0,1023,20,1);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(val); // waits (val)ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
val=analogRead(potpin);
val=map(val,0,1023,20,1);
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(val); // waits (val)ms for the servo to reach the position
}
for(pos1 = 0; pos1 < 180; pos1 += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree

val1=analogRead(potpin1);
val1=map(val1,0,1023,20,1);
myservo1.write(pos1); // tell servo to go to position in variable ‘pos’
delay(val1); // waits (val)ms for the servo to reach the position
}
for(pos1 = 180; pos1>= 1; pos1 -=1) // goes from 180 degrees to 0 degrees
{
val1=analogRead(potpin1);
val1=map(val1,0,1023,20,1);
myservo1.write(pos1); // tell servo to go to position in variable ‘pos’
delay(val1); // waits (val)ms for the servo to reach the position
}
}

I also have an other code where they work at the same time but then they are not sweeping, but just setting the position of the servo.
Best code for me is if i could make a code where i had both sweeping where the poti controls the speed and the if i pushed a button it would change to this code and then the poti just controls the position. sorry if there is to many questions the most important is the one in the post before but I just like to put the ideas up there :slight_smile:
Thx again

// Modification of the Servo Example Knob
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott http://people.interaction-ivrea.it/m.rinott

#include <Servo.h>

Servo myservo; // create servo object to control a servo
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo

int potpin3 = 3; // analog pin used to connect the potentiometer
int potpin4 = 4; // analog pin used to connect the potentiometer
int potpin5 = 5; // analog pin used to connect the potentiometer

int val3; // variable to read the value from the analog pin
int val4; // variable to read the value from the analog pin
int val5; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.attach(10); // attaches the servo on pin 9 to the servo object
myservo2.attach(11); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val3); // sets the servo position according to the scaled value

val4 = analogRead(potpin4); // reads the value of the potentiometer (value between 0 and 1023)
val4 = map(val4, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val4);

val5 = analogRead(potpin5); // reads the value of the potentiometer (value between 0 and 1023)
val5 = map(val5, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo2.write(val5);

delay(15); // waits for the servo to get there
}