Two servos (with delay between them and different, discontinuous rotations)

I want to make two servos rotate to different angles and then stop, with a slight delay between. I have viewed multiple forums and tutorials, but those are based on either continuous rotation or are more complex then the code for the operation I need. I need to rotate the first servo approx. 90 and the second servo approx. 60 with about half a second delay in between.

The attached code is taken from the adafruit servo sweep tutorial, with some modifications. The main chance is that I moved the for() from void loop() to void setup().

I am new to arduino code, and servos so I think I am making this too complicated. Any help would be really appreciated!

Sweep_2_servo_with_delay_Vforum.ino (997 Bytes)

Test code for multiple servos. Note that this probably won’t work with an adafruit servo shield.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Please do us a favour and post the code here (using code tags) to make it easier to see and avoiding the need to download it.

Current code I am using (same as attached):

#include <Servo.h> 
 
Servo myservo;  

int pos = 0;  
 
Servo myservo2;

void setup() 
{ 
  myservo.attach(9);   

 for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       
  } 
 
  myservo2.attach(10);
  
delay(500);  
 
for(pos = 0; pos <= 30; pos += 1)  
  {                                  
            
    myservo2.write(pos);          
  } 
  
} 
 
void loop() 
{ 
  
}

What happens when you run it ? What do you want to happen when you run it ?

I want the first servo to rotate 90 deg. and then stop, and the second servo to rotate 30 deg. and stop with a half second delay between the first and second servos.

Right now the second servo rotates 30 deg, pauses, and then rotates another 30 deg.

It seems like there is an easier way to write a sketch that would allow to servos to perform this action, since there is no looping.

This will do what you describe but I suspect that your description is incomplete

#include <Servo.h>
Servo servo1;
Servo servo2;

void setup()
{
  Serial.begin(115200);
  servo1.attach(9);
  servo2.attach(10);

  servo1.write(90);
  delay(500);
  servo2.write(30);
}

void loop(){}

What type of servos are you using and how are they powered ?

I am using one small servo (plastic gear, not sure about the torque) to pull a pin, and one high torque servo (140 oz-in) to push a trigger. Both are powered by an uno, and they are currently wired to a breadboard. I am using a 1.3 Amp 2S Lipo battery when the arduino is not plugged into my computer. Both servos only need to rotate clockwise to the specified angle once, and then stop.

I'm not sure, but one possible cause I can think of that might affect the servos is the angle they start at relative to the 180 deg. range of the servo. I have not been setting the servos to zero deg. when running the code.

Both are powered by an uno

Wrong. Both should be POWERED by a separate power source and CONTROLLED by the Arduino.

UKHeliBob, your code works perfect! Thank you for your help.

I have not been setting the servos to zero deg. when running the code.

Have you done that now ?