Controlling 2 servos for blinking eyes and moving mouth in a puppet

So, I'm fairly new to much of Arduino, but slowly learning. Costuming is my main hobby, and I wondered if it was possible to use a single Arduino Uno to control two servos, one to make a set of eyes blink, and another to make a mouth open and close (a second blinking mechanism). It's for a prop plushie/puppet.

I have two servos and my Arduino in front of me, and I received a Sparkfun Inventors Kit a while ago for a present. I know the code I want to use for each servo, but since I want them to move independent of each other, I am stumped (I would like the eyes to close and open every 500/3500 ms and the mouth to open and close on a 1000/5000 ms cycle).

I searched through various topics, but I think what I want to do is either infeasible (I don't see how, but I admit it possible that I am asking too much of the Arduino) or so simple that the solution is getting bogged down in all the extra things people have in the example codes, and I don't know what I need, what I don't, and what the lines of code are doing.

The code I'd like to use for both seems simple, and was based off of the examples I could find (especially the SIK example), but both servos just synced up, with one running through the 500/3500 blink, and the other then running through the 1000/5000 blink, then back to the first, instead of each going through its own loop regardless of what the other was doing. I really don't want them synced. Is this possible, or must they be synced with each other?

Below was my best attempt, which gave me the above result.

Code start

#include <Servo.h>

Servo servoeyes;
Servo servomouth;

void setup()
{
servoeyes.attach(9);
servomouth.attach(10);
}

void loop()
{
int position;

servoeyes.write(90);
delay(500);
servoeyes.write(180);
delay(3500);

servomouth.write(90);
delay(1000);
servomouth.write(180);
delay(5000);
}

Code end

I tried changing the delay parts to servoeyes.delay and servomouth.delay, but the verify nixed that idea, and an hour of searching only gave me a headache and a need to seek out help on this forum.

Thank you for any help you can provide. Coding languages are not my forte, and I don't need a great deal of explanation, just some basic pointers for a newbie trying out Arduino.

Do not use delay, look at the blink without delay example code in the IDE. Also there are lots of tutorials. Search for state machine or how to do several things at once. It is the top post in this section.

As G_M said... depending on your learning style, you may find this video of use.

I don't often provide ready made code (that's the teacher in me), but in this case it happens that I have code "on the shelf" that should get you started. I haven't used it for a while, but if my comments are anything to go by, it does Blink Without Delay on a number of independent LEDs, each with their own off and on times.

Off the top of my head, those could be the open / close delays on independent servos I think..... but YMMV. It puts the BlinkWithoutDelay stuff in a couple of functions which make it easier to read.

edit... duh, forgot the code....

/* Blink without Delay
 
 by Jim.... this one puts bwod code in a function and
 allows for differing off and on times
 and for that to be with 2x independent LEDs
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED2 attached from pin 12, 14, 15 to ground.
 
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  12;      // the number of the LED pin
const int led2Pin =  14;
const int led3Pin =  15;

// Variables will change:
//led1
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval;           // interval at which to blink (milliseconds)
long onTime=890;
long offTime=105;

//led2

int led2State = LOW;             // ledState used to set the LED
long previousMillis2 = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval2;           // interval at which to blink (milliseconds)
long onTime2=275;
long offTime2=260;

//led3

int led3State = LOW;             // ledState used to set the LED
long previousMillis3 = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval3;           // interval at which to blink (milliseconds)
long onTime3=125;
long offTime3=1575;



void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);  
  pinMode(led2Pin, OUTPUT); 
  pinMode(led3Pin, OUTPUT); 
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  //and bwod() is where all the bwod stuff is done
  bwod();
  bwod2();
  bwod3();
}




void bwod()
{

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
      interval=onTime;
    }
    else
    {
      ledState = LOW;
      interval=offTime;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
} //end bwod


void bwod2()
{

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2) {
    // save the last time you blinked the LED 
    previousMillis2 = currentMillis2;   

    // if the LED is off turn it on and vice-versa:
    if (led2State == LOW)
    {
      led2State = HIGH;
      interval2=onTime2;
    }
    else
    {
      led2State = LOW;
      interval2=offTime2;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(led2Pin, led2State);
  }
}//end bwod2

void bwod3()
{

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis3 = millis();

  if(currentMillis3 - previousMillis3 > interval3) {
    // save the last time you blinked the LED 
    previousMillis3 = currentMillis3;   

    // if the LED is off turn it on and vice-versa:
    if (led3State == LOW)
    {
      led3State = HIGH;
      interval3=onTime3;
    }
    else
    {
      led3State = LOW;
      interval3=offTime3;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(led3Pin, led3State);
  }
}//end bwod3

Have look at how timing is managed and servos are moved in several things at a time and planning and implementing a program.

The trick is to move each servo a little at a time (say 1 degree) from 90 to 180 as that allows you to move each of them a little in turn so they appear to be moving simultaneously.

I suspect it would also be wise not to use a FOR loop to move the servos because nothing else can be done until the loop concludes.

...R