I want a standard servo to rotate 180 degrees when button 1 is pressed and released. But I want the servo to rotate back to the initial position only when button 2 is being held down (& to stop rotating once button 2 is released). The arduino code I have now correctly allows the servo to make a full rotation 180 degrees with a press and release of button 1, but the servo also incorrectly rotates back to the initial position with a press and release of button 2 instead of stopping once released. Any help with the following code would be much appreciated:
#include <Servo.h>
Servo myservo;
int pos;
const int buttonPin = 2;
const int buttonPin2 = 3;
int buttonState = 0;
int buttonState2 = 0;
void setup()
{
myservo.attach(9);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2,INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState == HIGH) {
pos=180;
myservo.write(180);
}
if (buttonState2 == HIGH) {
pos-=1;
myservo.write(pos);
}
}