Servo Sweep with conditions

Hey everyone, i have this soil sensor OBSoil + sweep servo. In this code the servo should be acting when Analogread(2) > 400 making the servo move from 0 degree to 180 degree and when is lower (else) return from 180 to 0 degree or just stay in 0 degree. (acting like a window)

When I try this code the servo it just acting back and forth.. i think its something with the FOR line in the servo, i tried to make the comparation there but it doesnt work.

This is the basic sweep servo code but im pretty sure its something with the FOR of the servo and the IF that im trying to put there.

//OB SOIL 
//# the sensor value description
//  # 0  ~300      dry soil
//  # 300~700     humid soil
//  # 700~950     in water
#include <Servo.h>
Servo myservo;
const int number = 400;
int pos = 0;

void setup(){
        Serial.begin(9600);
        myservo.attach(9);
}

void loop(){
         Serial.print("Moisture Sensor Value:");
         Serial.println(analogRead(2));  
         delay(100);
         int analogValue2 = analogRead(2); 
                if (analogValue2 > number) {
                for(pos = 0; pos < 180; pos += 1)  
                {                                   
                  myservo.write(pos);              
                  delay(15);                       
                }
                } 
                else {
                for(pos = 180; pos>=1; pos-=1)     
                {                                
                myservo.write(pos);               
                delay(15);                        
                  } 
               }
}