Stuttering servos!

Hi everyone. I’m using a servo for a locking mechanism, where the servo is driving a pin into a latch. (For those familiar, it’s not unlike the latch in the reverse geocache box at The Reverse Geocache™ Puzzle | Arduiniana ). Everything’s hunky dory EXCEPT that the servo “stutters” every few seconds–meaning it’s continuing to try to turn as part of the loop. It’s a software thing, I’m certain, but I’m not sure how to fix it.

The code looks like this:

#include <SoftwareSerial.h>
#include <Servo.h>

int buttonState = 0; 
int read_me = 0;
const int TxPin = 7;
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

int outputPin = 11;      // Servo pin

Servo mrservo;

void setup() {
  mrservo.attach(outputPin);
  mrservo.write(110);
  Serial.begin(9600);
  Serial.println("Turning on");
  mySerial.begin(9600);
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  delay(1000);

}

void loop() {
  
buttonState = digitalRead(9);  
  mrservo.attach(outputPin); 
 
    if(buttonState == HIGH) {
     
        box_unlock();
      
      } else {
        
        box_lock();
                       
     }
     
}


void box_lock()  {

    mySerial.println("Box is UNLOCKED");          
        
          digitalWrite(outputPin, HIGH);

          mrservo.write(0);
          delay(1000);
          read_me = mrservo.read();
          digitalWrite(outputPin, LOW); 
         
          if(read_me == 0) {
            Serial.println("Now locked");
            mrservo.detach();  
            
          }
  
}

void box_unlock()  {

    mySerial.println("Box is LOCKED");          
        
          digitalWrite(outputPin, HIGH);

          mrservo.write(180);
          delay(1000);
          read_me = mrservo.read();
          digitalWrite(outputPin, LOW); 
         
         
          if(read_me == 180) {
            Serial.println("NOW UNLOCKED");
            mrservo.detach();  
            
          }
  
}

Any ideas? I’m sorta stumped, and I don’t want the servo constantly clicking away.

I did try to adjust the angles, figuring maybe it wasn’t reaching the full 0/180, but no such luck.

Help is appreciated!

Why on earth are you setting the servo control pin HIGH for 1-sec here? You should just be letting the servo library commands control the pin.

         digitalWrite(outputPin, HIGH);

          mrservo.write(0);
          delay(1000);
          read_me = mrservo.read();
          digitalWrite(outputPin, LOW);