Servo Writing late

Hello :wave:
I have connected 4 Servos with Ultrasonic sensor by using Arduino UNO.
PROBLEM : When a object is detected by Ultrasonic sensor, 3 servos rotate fast and then other remaining one rotates at the last
I am using MG995 Servo Moters.
PLEASE HELP ME

The code is :arrow_double_down:

#include <Servo.h>

const int TRIG_PIN  = 6;  
const int ECHO_PIN  = 7;  
const int SERVO_PIN = 9; 
const int DISTANCE_THRESHOLD = 34; 

Servo servo; 


float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
  servo.attach(SERVO_PIN);   
  servo.write(0);
}

void loop() {
  
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  
  duration_us = pulseIn(ECHO_PIN, HIGH);
  
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo.write(180); 
  else
    servo.write(0);  


  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

Only a single servo is defined in your code. Please post the code that controls 4 servos and shows your problem

Where do the servos get their power from ?

I don't have a code of 4 servo and I don't know coding
the servos get power from a 6V 10Ah DC Battery

they were working earlier

When you said

I thought that you were describing a problem but seems that it is a requirement

Do you want 3 servos to move through the same angle then for a 4th one to move through the same angle ?

no
the all 4 servos should rotate 180 at one time
no servo should be late

but you said

its the problem bro :rofl:

Firstly, I am not your "bro"

I don't know what you want the code to do. Please try describing it again

Hello parththakurscsh2008

Post a timing diagram to show what happends and when.

OK
I am using 4 Servo and 2 Ultrasonic sensors in my project.
The project was working before :
When a Object comes in front of the Ultrasonic sensor, the 4 servos rotate 180 at a time. No servo was late.

The problem in project :
When a Object comes in front of the Ultrasonic sensor, the 3 servos rotate 180 but 1 servo is late. It rotates when 3 servos are rotated.

In order to do what you describe you must have some code

HOLD ON !
Have you, by any chance got all 4 servos connected to pin 9 of the Arduino ?

and I dont think its a servo problem
When all the servos comes to 0 degree, the servo which was rotating late, it come s back 0 normally, but a different servo comes at 0 late.

I have connected all servos signal pin in the 9 pin

Then no wonder you have a problem
Connect each servo to its own pin (any except pins 0 or 1)
Define 3 more servos in the same way that the first one is but with different names
attach() each of the new servos using their pin numbers
write() to each of the servos when you want them to move

There are neater ways to do this but get the clumsy method working first

ok I am doing

Hey
I have made the code But I am having error
Compilation error: 'else' without a previous 'if'

#include <Servo.h>

const int TRIG_PIN  = 6;  
const int ECHO_PIN  = 7;   
const int DISTANCE_THRESHOLD = 34; 

Servo servo1; 

Servo servo2;

Servo servo3;

Servo servo4;


float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);       
  pinMode(TRIG_PIN, OUTPUT); 
  pinMode(ECHO_PIN, INPUT);  
  servo1.attach(8);
  servo2.attach(9);
  servo3.attach(10);
  servo4.attach(11);   
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);

}

void loop() {
  
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  
  duration_us = pulseIn(ECHO_PIN, HIGH);
  
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    servo1.write(180);
    servo2.write(180);
    servo3.write(180);
    servo4.write(180); 
  else
    servo1.write(0); 
    servo2.write(0); 
    servo3.write(0);
    servo4.write(0);

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

I done this and uploaded the code
The problem of servos while writing to 180 degree is not solved
but, the problem of servos while writing to 0 is solved.
:slight_smile:

I noticed one thing that when I changed the code that only 3 servos will rotate,
THEY WORKED PERFECTLY

Hi, @parththakurscsh2008

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Please show us how you have all this connected together.

Thanks.. Tom... :grinning: :+1: :coffee: :australia: