Multiple servos with delay

Hi my friends, this is my first topic, but I already learned a lot from you guys.

I’m working o my first Arduino based project, I was going ok, but I got stuck with my servos.

The intention is:

  • Servos start on position 30
  • If you push the button once, the servos will come from position 30 to position 120.
  • If you push the same button the second time, the servos will come back to position 30.
  • The servos must turn slowly
  • The servos must turn one after the other (not all on the same time)

With the code below I got almost everything working, but:

  • On the void setup I can’t set the servos to keep on position 30
  • On void loop I can control the speed by delay(20), but I cant make them work one after the other, they go all on the same time.

I would appreciate so much if someone gets me a hint to move forward of this.

Thank you Very Much!

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;


// Set digital pin numbers:

const int buttonPin = 13;  // The number of the Pushbutton pin

int buttonState = 0;  // Variable for reading the pushbutton status
int directionState = 0;  // Variable for reading direction of the servo

int pos = 30;  // Variable to store the servo position


void setup() {
  servo1.attach(14);  // attaches the servo on pin 8 to the servo object
  servo2.attach(15);
  servo3.attach(16);
  servo4.attach(17);
  servo5.attach(18);
  servo6.attach(19);


  
  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input

}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (directionState == 0) {
    //The button is pushed
    if (buttonState == HIGH) {
      directionState = 1;// The direction for the servo is clockwise

      // goes from 30 degrees to 120 degrees in steps of 1 degree
      for (pos = 30; pos < 120; pos = pos + 1)
      {
        
        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        
      }

    }


  } else if (directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise

      // goes from 120 degrees to 30 degrees in steps of 1 degree
      for (pos = 120; pos >= 30; pos = pos-1)
      {
        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }

    }

  }

}

In setup() you can do a servo.write(30) before you attach the servo object to a pin. That will cause the servo to move to 30 immediately.

You are stepping all of the servos in a single loop. To make them go in turn you need a separate loop for each servo.

Hi John, Thank you your extremely fast reply.

I have just fixed the initial position of all the servos, but I didn’t get how to create loops for each servo

Updated version with servo.write

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;


// Set digital pin numbers:

const int buttonPin = 13;  // The number of the Pushbutton pin

int buttonState = 0;  // Variable for reading the pushbutton status
int directionState = 0;  // Variable for reading direction of the servo

int pos = 0;  // Variable to store the servo position


void setup() {
  servo1.attach(14);  // attaches the servo on pin 8 to the servo object
  servo2.attach(15);
  servo3.attach(16);
  servo4.attach(17);
  servo5.attach(18);
  servo6.attach(19);

servo1.write(30);
servo2.write(30);
servo3.write(30);
servo4.write(30);
servo5.write(30);
servo6.write(30);
  
  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input

}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (directionState == 0) {
    //The button is pushed
    if (buttonState == HIGH) {
      directionState = 1;// The direction for the servo is clockwise

      // goes from 30 degrees to 120 degrees in steps of 1 degree
      for (pos = 30; pos < 120; pos = pos + 1)
      {
        
        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        
      }

    }


  } else if (directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise

      // goes from 120 degrees to 30 degrees in steps of 1 degree
      for (pos = 120; pos >= 30; pos = pos-1)
      {
        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }

    }

  }

}

Like this:

      // goes from 30 degrees to 120 degrees in steps of 1 degree
      for (pos = 30; pos < 120; pos = pos + 1) {
        servo1.write(pos);  // tell servo to go to position in variable 'pos'
        delay(10);  // waits 15ms for the servo to reach the position
      }
      for (pos = 30; pos < 120; pos = pos + 1) {
        servo2.write(pos);  // tell servo to go to position in variable 'pos'
        delay(10);  // waits 15ms for the servo to reach the position
      }
       for (pos = 30; pos < 120; pos = pos + 1) {
       servo3.write(pos);  // tell servo to go to position in variable 'pos'
        delay(10);  // waits 15ms for the servo to reach the position
      }
      for (pos = 30; pos < 120; pos = pos + 1) {
        servo4.write(pos);  // tell servo to go to position in variable 'pos'
        delay(10);  // waits 15ms for the servo to reach the position
      }
.
.
.

Do you see how that would sweep each one in turn rather than all at once.

See this http://forum.arduino.cc/index.php?topic=223286.0 and don't use delay

Mark

John,

Thank you once again, It worked.

For future references, here is the code and the VIDEO.

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;


// Set digital pin numbers:

const int buttonPin = 13;  // The number of the Pushbutton pin

int buttonState = 0;  // Variable for reading the pushbutton status
int directionState = 0;  // Variable for reading direction of the servo

int pos = 0;  // Variable to store the servo position


void setup() {
  servo1.attach(14);  // attaches the servo on pin 8 to the servo object
  servo2.attach(15);
  servo3.attach(16);
  servo4.attach(17);
  servo5.attach(18);
  servo6.attach(19);

  servo1.write(30);
  servo2.write(30);
  servo3.write(30);
  servo4.write(30);
  servo5.write(30);
  servo6.write(30);

  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input

}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (directionState == 0) {
    //The button is pushed
    if (buttonState == HIGH) {
      directionState = 1;// The direction for the servo is clockwise

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

        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }


      for (pos = 30; pos < 120; pos = pos + 1)
      {

        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }


      for (pos = 30; pos < 120; pos = pos + 1)
      {

        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }


      for (pos = 30; pos < 120; pos = pos + 1)
      {

        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }


      for (pos = 30; pos < 120; pos = pos + 1)
      {

        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }


      for (pos = 30; pos < 120; pos = pos + 1)
      {

        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(25);  // waits 15ms for the servo to reach the position
      }

    }


  } else if (directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise

      // goes from 120 degrees to 30 degrees in steps of 1 degree
      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo1.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }


      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo2.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }


      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo3.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }


      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo4.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }


      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo5.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }


      for (pos = 120; pos >= 30; pos = pos - 1)
      {
        servo6.write(pos);  // tell servo to go to position in variable ‘pos’
        delay(10);  // waits 15ms for the servo to reach the position
      }



    }

  }

}

Thank you