overlapping multiple servo movements

[if this belongs in programming questions instead -- let me know and i'll move it over]

So as a preface, i'm pretty new to arduino programming and have been trying to solve this problem for a bit. I have two servos set up on an arm with a version of the basic 'sweep' sketch that includes some easing (that i found online). Adding in the 2nd servo as such.. ( my new line for the 2nd servo was: myservo2.write(calangle); )

...has this look by default:

http://dl.dropbox.com/u/1378390/servo1.mov

#include <Servo.h>  
 
Servo myservo;
Servo myservo2;
float timeAngle;
float timeAngleadjust;
float moveAngle;            
int oldposition;
float servocon;
float sfactor;

// servo_smoothmove by witpim. 2007

void smoothmove(int cposition) {
 int anglediff = abs(cposition-oldposition);
 sfactor = servocon/anglediff;
 for (timeAngle=0.000; timeAngle<=1;) {
   timeAngle = timeAngle+sfactor;
   timeAngleadjust = (1.0/2.0) - (1.0/2.0*cos(timeAngle*PI));  //coming from here http://this.is/torfi/robotblog/index.php?getInfo=1&projectID=34
   moveAngle = (anglediff*timeAngleadjust);
   int calangle;
   if (oldposition > cposition) {
     calangle =  oldposition - (int)moveAngle;
   }
   if (oldposition < cposition) {
     calangle =  oldposition + (int)moveAngle ;
   }
      
   myservo.write(calangle);
   myservo2.write(calangle);   
 }
 
 oldposition = cposition;
}


void setup() {
   Serial.begin(19200);
   servocon = 0.01;  // servo factor, the higher the faster
   myservo.attach(9);
   myservo2.attach(10);  // attaches a servo connected to pin 2
}
void loop() {
 smoothmove(40);
 smoothmove(110);
}

Both arms move the same way, at the same time. So, I tried adding a copy of the smoothmove function and alternating the smoothmove calls in the loop like:

void loop() {
 smoothmove(40);
 smoothmove2(40);
 smoothmove(110);
 smoothmove2(110);
}

but that has this effect:

http://dl.dropbox.com/u/1378390/servo2.mov

The first servo moves and completes its action, then the 2nd, then back to the first etc. What i'm looking to do is "overlap" the actions so the 2nd servo begins moving about midway through the first's movement, like so:

http://dl.dropbox.com/u/1378390/servo3.mov

Any ideas on how to accomplish this?

Thanks-

Try something like this:

#include <Servo.h>  
 
Servo serv[2];
float timeAngle[2];
float timeAngleAdjust[2];
float moveAngle[2];
int oldposition[2];
int desiredPosition[2];
int anglediff[2];
float servocon;
float sfactor[2];

void setPosition(int i, int position) {
  desiredPosition[i] = position;
  anglediff[i] = abs(desiredPosition[i] - oldposition[i]);
  sfactor[i] = servocon / anglediff[i];
  timeAngle[i] = 0.0;
}

void smoothmove(int i) {
  if (timeAngle[i] <= 1) {
    timeAngle[i] += sfactor[i];
    timeAngleAdjust[i] = (1.0/2.0) - (1.0/2.0*cos(timeAngle[i] * PI));
    moveAngle[i] = (anglediff[i] * timeAngleAdjust[i]);
    int calAngle;
    if (oldposition[i] > desiredPosition[i]) {
      calAngle = oldposition[i] - (int) moveAngle[i];
    } else if (oldposition[i] < desiredPosition[i]) {
      calAngle = oldposition[i] + (int) moveAngle[i];
    }
    serv[i].write(calAngle);
  } else {
    oldposition[i] = desiredPosition[i];
  }
}

void setup() {
   Serial.begin(19200);
   servocon = 0.01;  // servo factor, the higher the faster
   serv[0].attach(9);
   serv[1].attach(10);  // attaches a servo connected to pin 2
}
void loop() {
  // run these to update position
  smoothmove(0);
  smoothmove(1);
  
  // set the position to a new one every once in a while
  if (millis() % 1000 == 0) {
    setPosition(0, 40);
    setPosition(1, 90);
  } else if (millis() % 1000 == 500) {
    setPosition(0, 90);
    setPosition(1, 40);
  }
}

Basically the idea is to move the movement and setting position functions into different functions, so that they can be executed separately, and thus allow simultaneous movements.