Humidity sensor + servo motor

Hey! I'm quite new with arduino and i have a simple project to develop.
Simulating a floodgate, i need to make my servo spin in positive or negative direction in given data.
But i need to make the code stop spinning when it reachs the maximum angle until the analog reads another number that gives a different command. Can someone help?

#include <Servo.h>
#define pino_sinal_analogico A0
 
const int pinoServo = 6; 
int valor_analogico;
Servo s;
int pos;
 
void setup (){
  
  Serial.begin(9600);
  pinMode(pino_sinal_analogico, INPUT);
  
  s.attach(pinoServo);
  s.write(0); 
}
void loop(){
  
  valor_analogico = analogRead(pino_sinal_analogico);
  
  Serial.print("Porta analogica: ");
  Serial.print(valor_analogico);

  if (valor_analogico > 300 && valor_analogico < 950)
  {
    Serial.println(" Status: Solo umido");
    for(pos = 90; pos >= 0; pos--){ 
    s.write(pos); 
    delay(30);
  }}
  if (valor_analogico > 0 && valor_analogico < 300)
  {
    Serial.println(" Status: Solo seco");
   for(pos = 0; pos <= 90; pos++){ 
    s.write(pos); 
    delay(15);
  }
  }delay(1000);
}

I don't really understand what you want...but perhaps you need to save the last value of the analog read and then only perform your servo.write loops if the next value you read is different?

Steve

slipstick:
I don't really understand what you want...but perhaps you need to save the last value of the analog read and then only perform your servo.write loops if the next value you read is different?

Steve

Yes, that's what i want, but i don't know how to include that in the code

That's a technique called state change detection. See the "state change detection" example in the IDE - that's for a button, but for analog readings it's the same.

Well, due to noise you probably want to add some hysteresis, so only make a movement if the new reading is more than a certain amount different from the old.