Hi everybody,
I am new in using the arduino board and i need help in controlling multiple servos. My project is to control 3 servos using 4 potentiometer.
Each servos are programmed to perform different movement at the same time. But the problem is that the servos preform its movement one-by-one not at the same time. Is there any way/method to make the servos move at the same time? It could be timing problem/ interrupt but i not sure how to solve this problem. Help me please…
Below shows the sketch that i have written for 1 potentiometer controlling 3 servos.
// Proto_Testing
// by Khairul Shariman Kalid kshariman@gmail.com
/* Currently the problem is to perform multitasking
which is to control multiple output with variance
task using single input
- Is there any method/source code that can be applied for
multi tasking in arduino?
*/
#include <Servo.h>
Servo Arm1; //create servo object to control servo 1
Servo Arm2; //create servo object to control servo 2
Servo Arm3; //create servo object to control servo 3
int fwd = 0; // forward input control at analog pin 0
int fwdvalue = 0; //set initial value of analog input
int fwdpos1_Arm1 = 0; // variable to store the servo position
int fwdpos2_Arm1 = 0; // variable to store the servo position
int revpos1_Arm1 = 0; // variable to store the servo position
int revpos2_Arm1 = 0; // variable to store the servo position
int fwdpos1_Arm2 = 0; // variable to store the servo position
int fwdpos2_Arm2 = 0; // variable to store the servo position
int revpos1_Arm2 = 0; // variable to store the servo position
int revpos2_Arm2 = 0; // variable to store the servo position
int fwdpos1_Arm3 = 0; // variable to store the servo position
int fwdpos2_Arm3 = 0; // variable to store the servo position
int revpos1_Arm3 = 0; // variable to store the servo position
int revpos2_Arm3 = 0; // variable to store the servo position
void setup()
{
pinMode(fwd,INPUT); //set pin 0 as input
Arm1.attach(3); // attaches the servo on pin 3 to the servo object
Arm2.attach(5); // attaches the servo on pin 5 to the servo object
Arm3.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
fwdvalue = analogRead(fwd);
delay(15);
if(fwdvalue > 512)
{
Arm1_Rev();
Arm2_Fwd();
Arm3_Fwd();
}
else if(fwdvalue < 512)
{
Arm1_Fwd();
Arm2_Rev();
Arm3_Rev();
}
else
{
Donothing();
}
}
//…ARM_1…
void Arm1_Fwd()
{
for(fwdpos1_Arm1 = 0; fwdpos1_Arm1 < 180; fwdpos1_Arm1 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm1.write(fwdpos1_Arm1); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(fwdpos2_Arm1 = 180; fwdpos2_Arm1 > 0; fwdpos2_Arm1 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm1.write(fwdpos2_Arm1); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…
void Arm1_Rev()
{
for(revpos2_Arm1 = 180; revpos2_Arm1 > 0; revpos2_Arm1 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm1.write(revpos2_Arm1); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(revpos1_Arm1 = 0; revpos1_Arm1 < 180; revpos1_Arm1 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm1.write(revpos1_Arm1); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…ARM_2…
void Arm2_Fwd()
{
for(fwdpos1_Arm2 = 0; fwdpos1_Arm2 < 180; fwdpos1_Arm2 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm2.write(fwdpos1_Arm2); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(fwdpos2_Arm2 = 180; fwdpos2_Arm2 > 0; fwdpos2_Arm2 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm2.write(fwdpos2_Arm2); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…
void Arm2_Rev()
{
for(revpos2_Arm2 = 180; revpos2_Arm2 > 0; revpos2_Arm2 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm2.write(revpos2_Arm2); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(revpos1_Arm2 = 0; revpos1_Arm2 < 180; revpos1_Arm2 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm2.write(revpos1_Arm2); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…ARM_3…
void Arm3_Fwd()
{
for(fwdpos1_Arm3 = 0; fwdpos1_Arm3 < 180; fwdpos1_Arm3 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm3.write(fwdpos1_Arm3); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(fwdpos2_Arm3 = 180; fwdpos2_Arm3 > 0; fwdpos2_Arm3 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm3.write(fwdpos2_Arm3); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…
void Arm3_Rev()
{
for(revpos2_Arm3 = 180; revpos2_Arm3 > 0; revpos2_Arm3 -= 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm3.write(revpos2_Arm3); // tell servo to go to position in variable ‘pos’
delay(15);
}
for(revpos1_Arm3 = 0; revpos1_Arm3 < 180; revpos1_Arm3 += 5) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
Arm3.write(revpos1_Arm3); // tell servo to go to position in variable ‘pos’
delay(15);
}
return;
}
//…
void Donothing()
{
Arm1.write(0); // tell servo to go to position in variable ‘pos’
Arm2.write(0); // tell servo to go to position in variable ‘pos’
Arm3.write(0); // tell servo to go to position in variable ‘pos’
return;
}
//…