(Solved) How to add,one more servo in this sketch?

The sketch is working with one servo, but how can i add one more servo into the sketch?

I’ve have done some changes in the sketch,but I do not know what I should change or add in the void loop.
Here is the sketch:

#include <Servo.h>

Servo servo1;
Servo servo2;        //Servo2 
int button1=0;
int button2=0;
int button3=0;       // Servo2
int button4=0;       // Servo2
int pos=90;

void setup()
{
pinMode(5, INPUT);
pinMode(6, INPUT);
servo1.attach(3);
pinMode(7, INPUT);    //Servo2
pinMode(8, INPUT);   // Servo2
servo1.attach(9);    // Servo2
}

void loop()
{
button1=digitalRead(5);
button2=digitalRead(6);
servo1.write(pos);
delay(5);
pos=constrain(pos,0,180);

if(button1==1 && button2==0)
{
pos++;
}

else if(button1==0 && button2==1)
{
pos--;
}
else pos=90;
}

You already seem to have two servos. You can add a third the same way that the second was added

Servo servo1;
Servo servo2;
Servo servo3;

You just need to attach each servo to the pin that controls it

servo3.attach(servo3pin);

I guess you can figure out the rest from the code you already have.

...R

I will only have two servo, i have not write what shall be in the void loop for servo 2, only for servo 1. Button 3 and button 4,be used to control servo2.

void loop()
{
button1=digitalRead(5);
button2=digitalRead(6);
servo1.write(pos);
delay(5);
pos=constrain(pos,0,180);

if(button1==1 && button2==0)
{
pos++;
}

else if(button1==0 && button2==1)
{
pos--;
}
else pos=90;
}

Try adding additional code that is similar to the existing code for the first servo.

If it doesn't work post your latest attempt. And please always post the complete program.

...R

Hello

Here is my latest attempt, the sketch is compiling in arduino, but does not work.

#include <Servo.h>

Servo servo1;
Servo servo2;        //Servo2 
int button1=0;
int button2=0;
int button3=0;       // Servo2
int button4=0;       // Servo2
int pos=90;

void setup()
{
pinMode(5, INPUT);
pinMode(6, INPUT);
servo1.attach(3);
pinMode(7, INPUT);    //Servo2
pinMode(8, INPUT);   // Servo2
servo2.attach(9);    // Servo2
}

void loop()
//*******************************************************************
//Servo1
{
button1=digitalRead(5);
button2=digitalRead(6);
servo1.write(pos);
delay(5);
pos=constrain(pos,0,180);

if(button1==1 && button2==0)
{
pos++;
}

else if(button1==0 && button2==1)
{
pos--;
}
else { pos=90;
 }
 
//**********************************************************************
// Servo2
{

button3=digitalRead(7);            
button4=digitalRead(8);            
servo2.write(pos);                  
delay(5);                             
pos=constrain(pos,0,180);             
 
if(button3==1 && button4==0)
{
pos++;
}

else if(button3==0 && button4==1)
{
pos--;
}
else {pos=90;
    }
    }
}
//****************************************************************

You are using the same variable to hold the position of both servos. Just as you have two servos you need to have two different position variables - perhaps pos and pos2 although I would prefer servo1pos and servo2pos for greater clarity.

If you still have a problem you need to tell us more than "does not work"

...R

thank you Robin2 :slight_smile:

The sketch is working now, i add servo1pos and servo2pos as you wrote.

#include <Servo.h>

Servo servo1;        // Servo1
Servo servo2;        // Servo2
int button1=0;       // Servo1
int button2=0;       // Servo1
int button3=0;       // Servo2
int button4=0;       // Servo2
int servo1pos=90;    // Servo1
int servo2pos =90;   // Servo2
void setup()
{
pinMode(5, INPUT);    // Servo1
pinMode(6, INPUT);    // Servo1
servo1.attach(3);     // Servo1
pinMode(7, INPUT);    // Servo2
pinMode(8, INPUT);    // Servo2
servo2.attach(9);     // Servo2
}

void loop()
//*******************************************************************
// Servo1
{
button1=digitalRead(5);
button2=digitalRead(6);
servo1.write(servo1pos );
delay(5);
servo1pos=constrain(servo1pos,0,180);

if(button1==1 && button2==0)
{
servo1pos++;
}

else if(button1==0 && button2==1)
{
servo1pos--;
}
else { servo1pos=90;
 }
 
//**********************************************************************
// Servo2
{

button3=digitalRead(7);           
button4=digitalRead(8);           
servo2.write(servo2pos);                 
delay(5);                             
servo2pos =constrain(servo2pos ,0,180);             
 
if(button3==1 && button4==0)
{
servo2pos ++;
}

else if(button3==0 && button4==1)
{
servo2pos --;
}
else {servo2pos =90;
    }
    }
}
//************************************************************************