My servo go to 90 degree before the degree i want

I use 4 servo (mg995)
my servo always go to 90 degree before the degree that I wanted
this is the code of arduino

#include <Servo.h>

Servo servo0; // servo up&down for camera
Servo servo1; // servo right%left for camera
Servo servo2; // servo up&down for hose
Servo servo3; // servo right%left for hose

int servo_0_angle ; // servo up&down for camera
int servo_1_angle ; // servo right%left for camera
int servo_2_angle ; // servo up&down for hose
int servo_3_angle ; // servo right%left for hose
int relay = 0 ;
int sum =0 ;

void setup(){

// Set the baud rate
Serial.begin(9600);
servo0.attach(2); // servo up&down for camera
servo1.attach(13); // servo right%left for camera
servo2.attach(26); // servo up&down for hose
servo3.attach(42); // servo right%left for hose
pinMode(5, OUTPUT);
digitalWrite(5, LOW);

}

void loop(){
servo0.attach(2); // servo up&down for camera
servo1.attach(13); // servo right%left for camera
servo2.attach(26); // servo up&down for hose
servo3.attach(42); // servo right%left for hose

if(Serial.available() > 0 ) {
servo_0_angle = Serial.parseInt();
servo_1_angle = Serial.parseInt();
servo_2_angle = Serial.parseInt();
servo_3_angle = Serial.parseInt();
relay = Serial.parseInt();

if(servo_0_angle > 0)
  servo0.write(servo_0_angle);
  delay(1000) ;


if(servo_1_angle > 0)
  servo1.write(servo_1_angle);
  delay(1000) ;


if(relay > 0){

  if(servo_2_angle > 0)
    servo2.write(servo_2_angle);
    delay(1000) ;


  if(servo_3_angle > 0)
    servo3.write(servo_3_angle);
    delay(1000) ;
  
  digitalWrite(5, HIGH);
  delay(5000);
  relay = 0 ; }




// Compute a sum to prove we have integers
sum = servo_0_angle + servo_1_angle + servo_2_angle + servo_3_angle ;

// We do println to add a new line character '\n' at the end
// of the comma-separated stream of integers
Serial.print(servo_0_angle); Serial.print(",");
Serial.print(servo_1_angle); Serial.print(",");
Serial.print(servo_2_angle); Serial.print(",");
Serial.print(servo_3_angle); Serial.print(",");
Serial.print(relay); Serial.print(",");
Serial.println(sum); 

}
}

Do a write to your desired home position before you do the attach.

thank you very much that solve my servo problem.

@thamer1, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

In future, please use code tags when posting code (the </> button in the reply window).

Under each reply is a Solution checkbox. Please tick the one under the most useful reply to let others know that your problem was solved.

Just a little hint.

What you see

What the compiler sees

if(servo_0_angle > 0)
  servo0.write(servo_0_angle);
 
delay(1000) ;


if(servo_1_angle > 0)
  servo1.write(servo_1_angle);

delay(1000) ;

If you meant for the delays to be conditional, you'll need some of these { }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.