user84809: My code represents a water level sensor, and the components are the Arduino Uno, a breadboard, an ultrasonic servo and two LEDS in which one is red and the other is green.
The condition that I don't know how to include in my code properly to make the servo rotate and activate the LEDs simultaneously:
When the distance is <= 50, servo rotates 180 and activates the red LED
else
servo goes to zero and activates the green LED.
My code:
#include <Servo.h>
int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

// **************************************//
void setup()
{
 
 Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin);
 
}

void loop() {
  
  int duration, distance;
  pinMode(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 distance = pulseIn(echoPin, HIGH);
 distance = duration *30/2500;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  
  
 if (distance <= 50)
 
{
  digitalWrite(red, HIGH);
  srv.write(180);
   delay(1000);
} 
else 
{
  digitalWrite(green, LOW);
  delay(1000);
  srv.write(0);
}
}



```



[![System][1]][1]


  [1]: https://i.stack.imgur.com/6YXgf.png


user10285: Checking your program I found two errors IMHO. Please see the comments in the copied code below. The one also pointed out by @EdgarBonet definitely prevents your program from working correctly. The other one only has impact of the correctness if the values of the constants OUTPUT and LOW differ. I don't know if that is the case.
I'm pretty sure, that the continously turning servo is not caused by the program. There are two kinds of servos. One type that gets a signal which indicates the angle of the turn of the servos arms. The other type gets the same signal (called PWM = Pulse Width Modulation) and interprets it as the speed with that it continously turns the servo arms.
If you use the latter one the arduino sends a signal like turn about 180° but the servo understands turn with full speed. There is nothing you can do programatically to avoid that.
There are workarounds like build a feedback loop with a potentiometer that measures the angle of the turn or use stopper sensors.
But the simplest way is to use a servo that is made for your usecase. (A non continous or standard servo for e.g. RC Cars, RC Ships, RC Air Planes)
BTW: The picture your provided shows a simulation. If the Servo in the simulator turns continuously, perhaps there is a property you can use to change the servo type from continuous to standard. ??
#include <Servo.h>

int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin); 
}

void loop() 
{
  
  int duration, distance;
  // error: you already set the pinmode to OUTPUT
  // pinMode(trigPin, LOW);
  // perhaps you mean 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // error: duration or distance ? ;-)
  // distance = pulseIn(echoPin, HIGH);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 30 /2500;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance <= 50)
  {
    digitalWrite(red, HIGH);
    srv.write(180);
    // improvement: delay necessary?
    // delay(1000);
  }  
  else 
  {
    digitalWrite(green, LOW);
    // improvement: delay necessary?
    // delay(1000);
    srv.write(0);
  }
}


84809: <p>My code represents a water level sensor, and the components are the Arduino Uno, a breadboard, an ultrasonic servo and two LEDS in which one is red and the other is green.</p>
<p>The condition that I don't know how to include in my code properly to make the servo rotate and activate the LEDs simultaneously:</p>
<p>When the distance is &lt;= 50, servo rotates 180 and activates the red LED
else
servo goes to zero and activates the green LED.</p>
<p>My code:</p>
<pre><code>#include &lt;Servo.h&gt;
int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

// **************************************//
void setup()
{
 
 Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin);
 
}

void loop() {
  
  int duration, distance;
  pinMode(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 distance = pulseIn(echoPin, HIGH);
 distance = duration *30/2500;
  
  Serial.print(&quot;Distance: &quot;);
  Serial.print(distance);
  Serial.println(&quot; cm&quot;);
  
  
  
 if (distance &lt;= 50)
 
{
  digitalWrite(red, HIGH);
  srv.write(180);
   delay(1000);
} 
else 
{
  digitalWrite(green, LOW);
  delay(1000);
  srv.write(0);
}
}



```



[![System][1]][1]


  [1]: https://i.stack.imgur.com/6YXgf.png
</code></pre>
 10285: <p>Checking your program I found two errors IMHO. Please see the comments in the copied code below. The one also pointed out by @EdgarBonet definitely prevents your program from working correctly. The other one only has impact of the correctness if the values of the constants OUTPUT and LOW differ. I don't know if that is the case.</p>
<p>I'm pretty sure, that the continously turning servo is not caused by the program. There are two kinds of servos. One type that gets a signal which indicates the angle of the turn of the servos arms. The other type gets the same signal (called PWM = Pulse Width Modulation) and interprets it as the speed with that it continously turns the servo arms.</p>
<p>If you use the latter one the arduino sends a signal like turn about 180° but the servo understands turn with full speed. There is nothing you can do programatically to avoid that.</p>
<p>There are workarounds like build a feedback loop with a potentiometer that measures the angle of the turn or use stopper sensors.
But the simplest way is to use a servo that is made for your usecase. (A non continous or standard servo for e.g. RC Cars, RC Ships, RC Air Planes)</p>
<p>BTW: The picture your provided shows a simulation. If the Servo in the simulator turns continuously, perhaps there is a property you can use to change the servo type from continuous to standard. ??</p>
<pre><code>#include &lt;Servo.h&gt;

int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin); 
}

void loop() 
{
  
  int duration, distance;
  // error: you already set the pinmode to OUTPUT
  // pinMode(trigPin, LOW);
  // perhaps you mean 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // error: duration or distance ? ;-)
  // distance = pulseIn(echoPin, HIGH);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 30 /2500;
  
  Serial.print(&quot;Distance: &quot;);
  Serial.print(distance);
  Serial.println(&quot; cm&quot;);

  if (distance &lt;= 50)
  {
    digitalWrite(red, HIGH);
    srv.write(180);
    // improvement: delay necessary?
    // delay(1000);
  }  
  else 
  {
    digitalWrite(green, LOW);
    // improvement: delay necessary?
    // delay(1000);
    srv.write(0);
  }
}
</code></pre>
