Two pushbutton servo, open and close

I am trying to use a servo to open and close a small door with two buttons. One opens and the other closes. It works, however there are some bugs.

If I press open, and then close while the “open” function is running, the servo spazzes and quickly loops back and forth for a time or two. Can I interrupt the function and just make it close?

If I shorten the delay after the servo is engaged, it seems like it cuts off. Is there a better way to do this?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;    // variable to store the servo position
int openButton = 2; //names pin 2 as the openButton pin
int closeButton = 3; //names pin 3 as the closeButton pin

void setup() {
  pinMode(openButton, INPUT); // sets pin 2 as an input
  pinMode(closeButton, INPUT); //sets pin 3 as an input

}



void loop() {
  // put your main code here, to run repeatedly:
 
if (digitalRead(openButton) == HIGH) { //Opens the door when the 'open' button is pressed
 myservo.attach(9);                     //Gives the servo power
 myservo.write(0);                    //Tells servo to move to 0 degree mark
    delay(1000);                       // waits 1000ms for the servo to reach the position
    myservo.detach();               //kills power to servo, to save battery
  }
if(digitalRead(closeButton) == HIGH) {//Close the door when the close button is pressed
  myservo.attach(9);
  myservo.write(170);
  delay(1000);
  myservo.detach();
}
}

1.) Call all of your servo.attach() functions in your Setup(). That way you only call them ONCE. 2.) Don't use detach() 3.) You need to debounce your buttons.

Do you have pull down resistors on the switch inputs?

groundFungus: Do you have pull down resistors on the switch inputs?

You can also use the internal pullups and then trigger on a logic low instead of logic high - two less external resistors to worry about.

Power_Broker: You can also use the internal pullups and then trigger on a logic low instead of logic high - two less external resistors to worry about.

I am pretty new to this. From what Ive read, the internal pullups will flip the logic of a pushbutton in order to give a more consistent reading, is that correct?

Also, I would like to use this on a 9V battery over a long period of time, and the detach() would save battery. Is using the detach() causing bugs?

zowen22: From what Ive read, the internal pullups will flip the logic of a pushbutton in order to give a more consistent reading, is that correct?

No, but I use them to reduce the amount of hardware required. Either way is just as consistent.

zowen22: Is using the detach() causing bugs?

Yes. Detaching and reattaching servos cause them to jitter and jolt for a split second. Also, if you detach them without giving enough time for them to reach their intended position, they will never get there (or move at all)!

zowen22: Also, I would like to use this on a 9V battery over a long period of time, and the detach() would save battery.

Detaching the servos only stops the Arduino from sending the servos the control signals, not powering them off. In order to save battery, you'd need to completely disconnect them from their power supplies.

zowen22:
Also, I would like to use this on a 9V battery over a long period of time

9 volt batter and long period of time for a project involving motors or servos are mutually exclusive. Why the 9V? Why does everyone want to use a 9V? There’s just not enough juice in one of those batteries. If you are building a smoke detector then go ahead with the 9V. If you want to run servos or motors or displays or anything that consumes power like that then the 9V is the LAST type of battery you would choose. It is literally the worst possible choice. Get a LiPO or even a handful of AA or AAA batteries and give your project a chance to run for a while.

Power_Broker: Yes. Detaching and reattaching servos cause them to jitter and jolt for a split second. Also, if you detach them without giving enough time for them to reach their intended position, they will never get there (or move at all)!

Thanks, this is what I needed!