Go Down

Topic: (Solved) How to add,one more servo in this sketch? (Read 795 times) previous topic - next topic

arduinole

Oct 04, 2015, 04:45 pm Last Edit: Oct 05, 2015, 11:09 am by arduinole
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:
Code: [Select]
#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;
}

Robin2

You already seem to have two servos. You can add a third the same way that the second was added
Code: [Select]
Servo servo1;
Servo servo2;
Servo servo3;


You just need to attach each servo to the pin that controls it
Code: [Select]
servo3.attach(servo3pin);

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

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

arduinole

#2
Oct 04, 2015, 08:10 pm Last Edit: Oct 04, 2015, 08:13 pm by arduinole
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.

Code: [Select]
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;
}

Robin2

#3
Oct 04, 2015, 09:26 pm Last Edit: Oct 04, 2015, 09:27 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

arduinole

#4
Oct 04, 2015, 10:21 pm Last Edit: Oct 04, 2015, 10:23 pm by arduinole
Hello

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

Code: [Select]
#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;
    }
    }
}
//****************************************************************

Robin2

#5
Oct 05, 2015, 10:04 am Last Edit: Oct 05, 2015, 10:05 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

arduinole

#6
Oct 05, 2015, 11:08 am Last Edit: Oct 05, 2015, 11:09 am by arduinole
thank you  Robin2 :)
 
The sketch is working now, i add  servo1pos and servo2pos as you wrote.

Code: [Select]
#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;
    }
    }
}
//************************************************************************

Go Up