Move servo motors to 180 degrees and slowly in the total movenment

(I am new to this forum, if I have posted on the wrong place, please help me to move it to correct place…)

I would like to using a simple code controlling four servo motors. The problem is I would like the movement of motors to be slowly. I have read this topic ( http://arduino.cc/forum/index.php/topic,145865.0.html) and modify the original code of mine. Now the servo motors can move slowly, BUT 1. they still move fast as before when they back to the original position and 2. the motors cannot move to almost 180 degrees as before (before modifying the code, they can reach almost 180 degrees.)

I want them move to almost 180 degrees and move slowly at both 0 to 180 and 180 back to 0 .

Thanks for you time for reading this, I am quite new to motors…below is my code…

#include <Servo.h>


Servo servoA;        
Servo servoB;        
Servo servoC; 
Servo servoD; 

void setup() { 
  servoA.attach(2); 
  servoB.attach(3);  
  servoC.attach(4); 
  servoD.attach(5); 
} 

void loop() {  
  
  for(int j = 0; j<180; j+=1){
  forward(j);
delay(10);  
  } 
  
  delay(2000); 
  
for(int i = 180; i>180; i-=1){ 
  reverse(i);
  delay(10);
}
  delay(2000);
}


void forward(int j) {
  servoA.write(j);
  servoB.write(j);
  servoC.write(j);
  servoD.write(j);
}

void reverse(int i) {
  servoA.write(i);
  servoB.write(i);
  servoC.write(i);
  servoD.write(i);
}

I'm not sure what your definition of slowly is, but it looks like you should be moving it from one stop to the other in about 1.8 seconds. To slow that down, increase the delay(10) statements to delay(20) or delay(30) and see how that looks to you.

afremont:
I’m not sure what your definition of slowly is, but it looks like you should be moving it from one stop to the other in about 1.8 seconds. To slow that down, increase the delay(10) statements to delay(20) or delay(30) and see how that looks to you.

Thanks for your reply. Sorry to being not clear…I want the motors move slowly in the process of 0 degree to 180 degree, and also slowly move back from 180 degree to 0 degree.

Now my code is moving good (=slow enough) from 0 to 180 (which is the function “forward(int i)”) , but it still move fast as before when it have to move back from 180 to 0. BUT I want them both moving slow in these two parts, that is the thing I am still cannot achieve.

So I guess the problem is not about the time of delay? But still thanks for the reply!! :slight_smile:

btw, I keep saying “before” may be is a little bit confuse, below is the code that I haven’t edit it to achieve the slow movement.

#include <Servo.h>


Servo servoA;         
Servo servoB;         
Servo servoC; 
Servo servoD; 

void setup() { 
  servoA.attach(2); 
  servoB.attach(3);  
  servoC.attach(4); 
  servoD.attach(5); 
} 

void loop() {            
  forward();             
  delay(2000);           
  reverse();
  delay(2000);

}

void forward() {
  servoA.write(0);
  servoB.write(0);
  servoC.write(0);
  servoD.write(0);
}

void reverse() {
  servoA.write(180);
  servoB.write(180);
  servoC.write(180);
  servoD.write(180);
}

Your ‘forward()’ and ‘reverse()’ functions are functionally identical so you don’t need both.

Your ‘reverse’ loop starts at 180 and only runs while i>180 so it won’t run even one loop. I think you want it to end when you get back to 0.

#include <Servo.h>
Servo servoA;        
Servo servoB;        
Servo servoC; 
Servo servoD; 

void setup() { 
  servoA.attach(2); 
  servoB.attach(3);  
  servoC.attach(4); 
  servoD.attach(5); 
} 

void loop() {  

  for(int j = 0; j<180; j++) {
    move(j);
    delay(10);  
  } 

  delay(2000); 

  for(int i = 180; i>=0; i--) { 
    move(i);
    delay(10);
  }
  
  delay(2000);
}


void move(int j) {
  servoA.write(j);
  servoB.write(j);
  servoC.write(j);
  servoD.write(j);
}

Thanks for your reply!! I don't have my arduino and motors in hand right now, I will try the code few hours later!! Thank you for your correction!! I will keep update if I success (or not =( ). Thanksss!!!

Thanks johnwasser !!! The code work perfectly good to me!! Thank you so much!!!!