help to detach servo after movement

Hi I would like some help with some code, I am moving 2 servos controlled by buttons and want to be able to detach the servo after I have moved them back, to stop the servo noise/buzz.

The problem I am having is if I putt servo detach after the second button press the servos just detaches before it has moved.

 #include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
int pos = 0;    // variable to store the servo position

void setup(){
  
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  
 
}

void loop(){

  //myservo1.write(140); 
  //myservo2.write(140);
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  int sensorval1 = digitalRead(3);
 
  
 
  if (sensorVal == LOW) {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 9 to the servo object
   
    for(pos = 140; pos>=40; pos-=2)     // goes from 140 degrees to 40 degrees
    {                                  // in steps of 1 degree
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      myservo2.write(pos);
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    
  }
      
    if (sensorval1 == LOW) {
      
    for(pos = 40; pos < 140; pos += 1)  // goes from 40 degrees to 140 degrees
    
    {                               
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      myservo2.write(pos);
      delay(20);                       // waits 20ms for the servo to reach the position

      myservo1.detach();
      myservo2.detach();
      
    }
    
  }

}

I am new to Arduino so any advice would be welcome

The problem I am having is if I putt servo detach after the second button press the servos just detaches before it has moved.

It needs time to move. You already have a delay of 20 milliseconds in there. As an experiment try increasing it, although that is not the best way to wait a while it is the easiest to test.

Is there any load on the servo after it has moved ?

Detaching servos is usually a bad idea because the servo will no longer retain its position. Any load will move it.

But if you really want to do it then your current detach() commands are inside your second for loop. They need to be after it has completed.

It would be easier to see what’s going on if you used the really helpful Auto Format (Ctrl-T) command in the IDE.

Steve

Thanks for your help, there is no load on the servos when I want them detached, the arms I have attached are resting on a stop with gravity to hold them in place.

I don’t know what auto format is I will have to look it up.

I have changed the code by moving the detach() command as suggested and it now works how I want.

Thanks for your help.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
int pos = 0;    // variable to store the servo position

void setup(){
  
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  
 
}

void loop(){

  //myservo1.write(140); 
  //myservo2.write(140);
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  int sensorval1 = digitalRead(3);
 
  
 
  if (sensorVal == LOW) {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 9 to the servo object
   
    for(pos = 140; pos>=40; pos-=1)     // goes from 140 degrees to 40 degrees
    {                                  // in steps of 1 degree
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      myservo2.write(pos);
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    
  }
      
    if (sensorval1 == LOW) {
      
    for(pos = 40; pos < 140; pos += 1)  // goes from 40 degrees to 140 degrees
    
    {                               
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      myservo2.write(pos);
      delay(25);                       // waits 20ms for the servo to reach the position
    }
    
   

    
      myservo1.detach();
      myservo2.detach();
}
}

the arms I have attached are resting on a stop with gravity to hold them in place.

If the servos are buzzing then they are quite likely pushing on the stops