Servo code help

Hi

I was wondering if anyone can help me. I am looking for code that will allow me to operate two servos.

I would like to have it operated by a reed switch or push button.

Basic it would go like this.

reed switch activated, servo 1 would turn 90 degrees & stop, then after the first servo has finished, servo 2 would then turn 90 degrees & stop. When the reed switch is activated again. Servo 2 would rotate back 90 degrees & stop, then servo 1 would rotate back 90 degrees & stop. I would also need the servos to rotate at say 150ms per position

any help welcomed :slight_smile:

ps I have an UNO

How tightly do they have to be synchronized? Assuming you’re talking about regular R/C servos, there is no feedback to the controller sending the signal, so you will need limit so you can tell when the servo is done moving. If sync isn’t too important, you can probably just time how long it takes to move and wait that amount of time.

As far as the speed, that depends on the servo itself, the load, and the applied voltage. If synchronization and slew rate is critical, perhaps you should consider stepper motors? If your tolerances are fairly loose, then servos are a good, inexpensive choice.

If these are continuous rotation servos, then you can control the speed at which they rotate by analog.Write-ing a value from 0 to 180, where 90 is neutral. For other servos, you can’t really control the speed without putting a billion “delay then rotate then delay then rotate” repititions. You can create a code along these lines (though you will need to do some tweaking and calibrating):

#include <Servo.h>

Servo servoL;

Servo servoR ;

//initializes servos as objects

int switch=2;

boolean switchState=digitalRead(switch);

void setup(){ set switch to input and the servos to output}

void loop(){ if(switchState= true){ analogWrite(servoL, desired angle), same for right}

if (switchState=false){do opposite}}

Hope this was helpful!

This is the code that came with the uno

#include <Servo.h>

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

// twelve servo objects can be created on most boards

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

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop()

{

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(150); // waits 15ms for the servo to reach the position

}

for(pos = 90; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable ‘pos’

delay(200); // waits 15ms for the servo to reach the position

}

}

I have altered it. it will slowly drive an rc servo 90 degrees & back. What I would like it to do is to be trigger by the reed. then it should move 90 degrees & stop. Then say after a 1 second has passed drive a second rc Servo. Whne the same switch is triggered it reverses the proses. if it makes sense

This is the code Ive got so far,

#include <Servo.h>

// constant variables used to set servo angles, in degrees

const int straight = 85;

const int divergent = 175;

// constant variables holding the ids of the pins we are using

const int buttonpin = 8;

const int servopin = 9;

// servo movement step delay, in milliseconds

const int step_delay = 200;

// create a servo object

Servo myservo;

// global variables to store servo position

int pos = straight; // current

int old_pos = pos; // previous

void setup()

{

// set the mode for the digital pins in use

pinMode(buttonpin, INPUT);

// setup the servo

myservo.attach(servopin); // attach to the servo on pin 9

myservo.write(pos); // set the initial servo position

}

void loop()

{

// start each iteration of the loop by reading the button

// if the button is pressed (reads HIGH), move the servo

int button_state = digitalRead(buttonpin);

if(button_state == HIGH){

old_pos = pos; // save the current position

// Toggle the position to the opposite value

pos = pos == straight ? divergent: straight;

// Move the servo to its new position

if(old_pos < pos){ // if the new angle is higher

// increment the servo position from oldpos to pos

for(int i = old_pos + 1; i <= pos; i++){

myservo.write(i); // write the next position to the servo

delay(step_delay); // wait

}

} else { // otherwise the new angle is equal or lower

// decrement the servo position from oldpos to pos

for(int i = old_pos - 1; i >= pos; i–){

myservo.write(i); // write the next position to the servo

delay(step_delay); // wait

}

}

}

}// end of loop

I just need it to start the second servo after waiting for the first to finish

Measure how much time the servo takes to move, then use a delay before moving the second. The button will NOT be read when in delay().

Thanks how do I add a second servo in to the sketch,

Google has all the answers.

I’ve worked out how to add the second servo, but I cant for the life of me figure out how to delay the second servo from moving until the first one has moved/finished

#include <Servo.h>

// constant variables used to set servo angles, in degrees
const int straight = 85; 
const int divergent = 175;


// constant variables holding the ids of the pins we are using
const int buttonpin = 8;
const int servopin9 = 9;
const int servopin10 = 10;



// servo movement step delay, in milliseconds
const int step_delay = 50;

// create a servo object
Servo myservo1; 
Servo myservo2; 
 
// global variables to store servo position
int pos = straight; // current
int old_pos = pos; // previous

void setup() 
{ 
  // set the mode for the digital pins in use
  pinMode(buttonpin, INPUT);
   
  // setup the servo
  myservo1.attach(servopin9);  // attach to the servo on pin 9
  myservo1.write(pos); // set the initial servo position
  myservo2.attach(servopin10);  // attach to the servo on pin 10
  myservo2.write(pos); // set the initial servo position
}

void loop() 
{ 
 // start each iteration of the loop by reading the button
 // if the button is pressed (reads HIGH), move the servo
  int button_state = digitalRead(buttonpin);
  if(button_state == HIGH){
    
    old_pos = pos;   // save the current position
    
    // Toggle the position to the opposite value
    pos = pos == straight ? divergent: straight;
       
    // Move the servo to its new position
    if(old_pos < pos){   // if the new angle is higher
      // increment the servo position from oldpos to pos
      for(int i = old_pos + 1; i <= pos; i++){  
        myservo1.write(i); // write the next position to the servo
        delay(step_delay); // wait     
        
        
        myservo2.write(i); // write the next position to the servo
        delay(step_delay); // wait
        
      }
    } else {  // otherwise the new angle is equal or lower
      // decrement the servo position from oldpos to pos
      for(int i = old_pos - 1; i >= pos; i--){ 
        myservo1.write(i); // write the next position to the servo
        delay(step_delay); // wait
        myservo2.write(i); // write the next position to the servo
        delay(step_delay); // wait
      }
    }
    
  } 
}// end of loop

anyone?

      for(int i = old_pos + 1; i <= pos; i++){ 
        myservo1.write(i); // write the next position to the servo
        delay(step_delay); // wait     
       
       
        myservo2.write(i); // write the next position to the servo
        delay(step_delay); // wait

You’re using the same for() statement for both servos… Why not make another for() for the second servo? This way the first servo moves, then the second one… You also need to make a second definition to hold the pos (position) and old_pos (old position).

steammad:
I’ve worked out how to add the second servo, but I cant for the life of me figure out how to delay the second servo from moving until the first one has moved/finished

This series of posts will be helpful:

https://learn.adafruit.com/multi-taskin … 1?view=all

https://learn.adafruit.com/multi-taskin … 2?view=all

https://learn.adafruit.com/multi-taskin … 3?view=all