How can I stop my servo looping when my sensor doesn't detect movement? I have this project with my Arduino UNO where I want my servo motor SG90 to go full speed to 180 degrees when the PIR sensor sends HIGH value and go slowly back to zero when the value is LOW.
I have this code working where the servo goes full speed back to zero:
#include &lt;Servo.h&gt;

Servo myservo; //creates a servo object
int pos = 0; //variable to store servo position
//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pir = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup() {
  myservo.attach(4); //attaches servo to pin 4
  Serial.begin(9600); //begins serial communication
  pinMode(pir, INPUT);
  pinMode(pirPos, OUTPUT);
  digitalWrite(pirPos, HIGH);
  //time for calibration
  
  Serial.println(&quot;calibrating sensor &quot;);
  for (int i = 0; i &lt; calibrationTime; i++) {
    Serial.print(calibrationTime - i);
    Serial.print(&quot;-&quot;);
    delay(1000);
  }
  Serial.println();
  Serial.println(&quot;done&quot;);
  
  //this waits until the PIR's output is low before ending setup
  while (digitalRead(pir) == HIGH) {
    delay(500);
  }
  
  Serial.print(&quot;SENSOR ACTIVE&quot;);
}

void loop() {
  if (digitalRead(pir) == HIGH)
    myservo.write(180);              // tell servo to go to position in variable 'pos'
  Serial.print(&quot;Motion detected!&quot;);

  if (digitalRead(pir) == LOW)
    myservo.write(0);              // tell servo to go to position in variable 'pos'
  Serial.print(&quot;Motion ended!&quot;);
}

I'd like the servo to go slowly back to zero, so I tried this :
#include &lt;Servo.h&gt;

Servo myservo; //creates a servo object
int pos = 0; //variable to store servo position
//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pir = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin

void setup() {
  myservo.attach(4); //attaches servo to pin 4
  Serial.begin(9600); //begins serial communication
  pinMode(pir, INPUT);
  pinMode(pirPos, OUTPUT);
  digitalWrite(pirPos, HIGH);
  
  //time for calibration
  Serial.println(&quot;calibrating sensor &quot;);
  for (int i = 0; i &lt; calibrationTime; i++) {
    Serial.print(calibrationTime - i);
    Serial.print(&quot;-&quot;);
    delay(1000);
  }
  Serial.println();
  Serial.println(&quot;done&quot;);
  
  //this waits until the PIR's output is low before ending setup
  while (digitalRead(pir) == HIGH) {
    delay(500);
  }
  
  Serial.print(&quot;SENSOR ACTIVE&quot;);
}


void loop() {
  if (digitalRead(pir) == HIGH)
    myservo.write(180);              // tell servo to go to position in variable 'pos'
    
  Serial.print(&quot;Motion detected!&quot;);

  if (digitalRead(pir) == LOW)
    for (pos = 180; pos &gt;= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    
  Serial.print(&quot;Motion ended!&quot;);
}

The problem is with this code the servo does go slowly back to zero but then goes full speed back to 180 and again and again ....
Any idea? I'm a beginner in Arduino, so I assume it must not be that hard to figure out.
Thanks.
Don't set the pos to 180 in the (digitalRead(pir) == LOW) branch if you do not want it to repeatedly jump up to 180 for as long as the condition is true.
void loop() {
  if (digitalRead(pir) == HIGH)
     pos = 180;
  myservo.write(pos);              // tell servo to go to position in variable 'pos'
    
  Serial.print(&quot;Motion detected!&quot;);

  if (digitalRead(pir) == LOW)
    for (; pos &gt; 0; pos -= 1) { // decay to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    
  Serial.print(&quot;Motion ended!&quot;);
}

Alternately, you could use pos as a state variable and simplify things:
void loop() {
  if (digitalRead(pir) == HIGH)
     pos = 180;
  elif (pos &gt; 0 ){
     pos--;
     delay(15);
  }
  myservo.write(pos);
}