newbee HELP please why cant I slow it down

Hello everybody

I wrote a code as simple as it can be. As you can see. I need a smooth coordinated movement loaded into arduino to control two Hitec HS-5485 HB digital Servo. I do get the movement order. But how can I make it slowwwwer. It moves in a fix movement rate. What do I miss in the code. And why can I find any code written to program servos without help of PC or pots or joysticks.

regards

// coordinated slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
               // a maximum of eight servo objects can be created


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

void setup()
{
 
 servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
 servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object

 
}



void loop()
{
 
{
 
 servo1.write(45);      // 1  
 
 delay(1000);
 
 }

{
 
 servo2.write(135);     //2
 
 delay(1000);
 }
{
 
 servo1.write(135);    //3
 
 delay(1000);
 }

{
 
 servo2.write(45);    //4
 
 delay(1000);
 }

{
 
 servo1.write(75);    //5
 
 delay(1000);
 }

{
 
 servo2.write(105);    //6
 
 delay(1000);
 }

{
 
 servo1.write(105);    //7
 
 delay(1000);
 }
{
 
 servo2.write(75);    //8
 
 delay(1000);
 }
 
}

You could try something like this on each servo to delay it between each step. It would slowly move from 0 to 45. The speed can be set (roughly) by the delay.

Untested code:

for(int val=0; val<=45; val++){
servo1.write(val);
delay(5);
}

Hope that gives you an idea :wink:

Mowcius

thank you very much for help it solved the fast problem but. val++ or ++val command has a problem. when the 3 step comes it does not start form 135 degree bur it goes to 45 fast and then goes back 135 slowly. I tried every combination to no avail

// coordinated slow movement


#include <Servo.h> 

Servo servo1;  // create servo object to control a servo 
Servo servo2;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  
  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object 
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object

  
} 

 
 
void loop() 
 
{   

 for(int val=45; val<=45; ++val)
{servo1.write(val);
 delay(50);}

for(int val=45; val<=45; ++val)
{servo2.write(val);
delay(50);}  
  
for(int val=45; val<=135; ++val)
{servo1.write(val);
 delay(50);}

for(int val=45; val<=135; ++val)
{servo2.write(val);
delay(50);}  
  
for(int val=135; val<=45; --val) here it does not go form 135 to 45 
{servo1.write(val);
 delay(50);}
for(int val=135; val<=45; --val)
{servo2.write(val);
 delay(50);}
}
for(int val=135; val<=45; --val) here it does not go form 135 to 45

The middle part of the for loop (val <= 45) is a while condition. Execute the loop while the condition is true. Since val starts at 135, and the while condition is tested before the loop is executed, val is not less than 45, so the loop is skipped.

I think that you want the while condition to be “val >= 45”. Since val starts at 135, and gets smaller, the while loop would execute until val got down to 45.

The some problem applies to your first two loops, too.

dear PaulS

thank you very much for you help that did it. A simple < > differance makes huges different. I was getting lost in it. But know I have a bigger task.
Now I will try to make a perfect circle

Best regards

and thanks again

Now I will try to make a perfect circle

What kind of servo do you have? Most servos are limited to 180 degree sweep, unless they are continuous rotation servos, in which case, they aren't really servos any more.

I am using two Hitec Hs-5485HB digital sevos. Limited to 120 degree of movement. In my project my actual need is 20 degree to both sides. I am going to install a mirror in front of the system. and will but 36 mirror in to 6x6 row to make an art project making then move simultaniusly.

Regards

At the moment I have nice smooth rectangular corner to corner movement. which can be made to move from side to side movement easly.

But Now I am trying a perfect circular motion using two servo at the same time.

this is successfull code

// coordinated slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


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

void setup()
{

  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object


}



void loop()

{

 for(int val=45; val<=45; ++val)
{servo1.write(val);

 delay(50);}

for(int val=45; val<=45; ++val)
{servo2.write(val);
delay(50);}

for(int val=45; val<=135; ++val)
{servo1.write(val);
 delay(50);}

for(int val=45; val<=135; ++val)
{servo2.write(val);
delay(50);}

for(int val=135; val>=45; --val) 
{servo1.write(val);
 delay(50);}
for(int val=135; val>=45; --val)
{servo2.write(val);
 delay(50);}
}

But now I have to write something like this

// coordinated slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


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

void setup()
{

  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object


}



void loop()

{

 for(int val=90; val<=90; ++val)
{servo2.write(val);
 servo1.write(val);
 delay(50);}
 
  for(int val=90; val>=45; --val)
{servo2.write(val);
 servo1.write(val);
 delay(50);}
 
for(int val=45; val<=135; ++val)
{servo2.write(val);
 servo1.write(val);
 delay(50);}

for(int val=135; val>=45; --val)
{servo2.write(val);
 servo1.write(val);
 delay(50);} 
 
 
 
}

which obviosly doesnt work Because I need to seperate two servo movement and make them simultaneous.

Most of the time they will make reverse action

I mean while
servo1 start from 90 and will go to 135
servo 2 must start from 45 and should go to 90 “center” etc

I tried to reverse the position of servos to simply match their reverse sides but that did not worked.

Any suggestions to command two servos simultaneously to opposite sides.

This is current servo position maps

http://s4.directupload.net/images/100104/a7gnc2nh.pdf
regards

0ne other thing

I know linear action could mean no circular movement but only straith lines. I will try to cope it with changing delay command

Regards

The linear in the description of the servo means that if you plot the input signal versus position of the servo, the result is a straight line. In other words, calling servo with 45, 46, 47, 48, and 49 will produce the same amount of movement as calling it with 80, 81, 82, 83, and 84.

In your for loops to move the servos, you do not need to specify the same value for each servo. To move servo 1 from 90 to 135 while moving servo 2 from 45 to 90, do this:

for(int val = 45; val<=90; val++)
{
   servo1.write(val+45); // Move from 90 to 135
   servo2.write(val); // Move from 45 to 90
}

The servos can even move in opposite directions:

for(int val = 45; val<=90; val++)
{
   servo1.write(90-val); // Move from 45 to 0
   servo2.write(val); // Move from 45 to 90
}

The servos don’t even need to share a common value:

for(int val1 = 0, val2 = 50; val1<=30; val1++, val2--)
{
   servo1.write(val1); // Move from 0 to 30
   servo2.write(val2); // Move from 50 to 20
}

this is again great tip and great help

But again I cannot slow down the movement and they are not simultaneous. I will try the other codes now

Thanks alot for your help. I advanced a lot

// coordinated slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


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

void setup()
{

  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object
  

}



void loop()

{
 
   for(int val1 = 45, val2 = 90, val3 = 135; val1<=90; val1++, val2++, val3--)
{  
  { {servo1.write(val1); // Move from 0 to 30
     delay(500);}
   {servo2.write(val1); // Move from 0 to 30
   delay(500);}}
   {servo1.write(val2); // Move from 50 to 20
   delay(500);}
   {servo2.write(val2); // Move from 0 to 30
   delay(500);}
   {servo1.write(val3); // Move from 50 to 20
   delay(500);}
   {servo1.write(val3); // Move from 0 to 30
   delay(500);}
}
 

}

You have way more curly braces in that code than are needed.

Also, I suspect that you want each servo to start moving, then, once all have started, wait for the servos to get where they are going, and then start them all moving again.

In other words, you want one delay statement in the loop, not 6.

at last I did it

I dont know it is the most clever method but it works. I will try to improve later

Semi circular loop

// coordinated semi-circular slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


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

void setup()
{

  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object


}



void loop()

{

{ for(int val=135; val>=90; --val) //servo2
{servo1.write(val);
 servo2.write(val-45);
 delay(50);}}
{ for(int val=90; val>=45; --val) //servo2
{servo1.write(val);
 servo2.write(135-val);
 delay(50);}}
{ for(int val=45; val<=90; ++val) //servo2
{servo1.write(val);
 servo2.write(val+45);
 delay(50);}}
{ for(int val=90; val<=135; ++val) //servo2
{servo1.write(val);
 servo2.write(225-val);
 delay(50);}}


}

And this is a programmed horizontal and vertical movement

// coordinated slow movement


#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


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

void setup()
{

  servo1.attach(9) ;  // attaches the servo on pin 9 to the servo object
  servo2.attach(10) ;  // attaches the servo on pin 9 to the servo object


}



void loop()

{

{ for(int val=45; val<=135; ++val) //servo2
{servo2.write(val);
 delay(50);}}
 
 { for(int val=135; val>=45; --val) //servo2
{servo2.write(val);
 delay(50);}}
 
{ for(int val=45; val<=135; ++val) //servo2
{servo2.write(val);
 delay(50);}}
 
 { for(int val=135; val>=45; --val) //servo2
{servo2.write(val);
 delay(50);}}
 
{ for(int val=45; val<=90; ++val) //servo2
{servo2.write(val);
 delay(50);}}

{ for(int val=90; val<=135; ++val) //servo1
{servo1.write(val);
 delay(50);}}
 
 { for(int val=135; val>=45; --val) //servo1
{servo1.write(val);
 delay(50);}} 
 
{ for(int val=45; val<=135; ++val) //servo1
{servo1.write(val);
 delay(50);}}
 
 { for(int val=135; val>=90; --val) //servo1
{servo1.write(val);
 delay(50);}} 
 
 { for(int val=90; val<=135; ++val) //servo2
{servo2.write(val);
 delay(50);}}
 
 { for(int val=135; val>=45; --val) //servo2
{servo2.write(val);
 delay(50);}}
 

}

and this is basic matriix of circular motion

regards