Using a button to control a servo's position.

I am very new to Arduino, and I am working on a project where I would like to use a button to change the position of a servo. The trouble I am having is I don't know how to make the servo hold the position I stopped pressing the button at. Any help would be greatly appreciated. Here is my code.

#include <Servo.h>

Servo myservo;
const int buttonPin = 5;
const int buttonPin1 = 4;
int buttonState = 0;
int button1State = 0;

void setup() 
{
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  myservo.attach(10  );
}

void loop() 
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    myservo.write(180);
  }
  else
  {
    myservo.write(0);
  }
  delay(15);
                            
}

Take the else{} out.

Feistlyone,
It looks to me like your current code moves the servo to 180 but when you release it it moves back to zero. Can you confirm that this is what it does now?

But it sounds like you want different behavior. You want the servo to stay where it is, either at zero or 180, unless you push the button. When the button is pressed, the servo should move to the other position and stay there.
Is this what you are looking for?

Reading it again, I think he wants it to start moving on a button press, and stop where it is on the button being released.

To do that you just need when the button is pressed for a variable say "pos" to increment by 1 (or 2 or 3 or whatever step you want) and do a servo.write(pos). If the button is held or pressed repeatedly, pos will take it to the end of the travel. So you look for pos being over say 175, change its sign to -1, -2 or whatever so it starts counting down. Change the sign back at about 5 degrees.

Do that all inside the button press code, so when you let go, the servo stops where it is.

Thank you for the help, but I have been trying to implement your solution and I think there is some part of coding I have not yet learned that I might need to know for this. Could you possibly write me a short example of what you mean, or point me in the direction of an example?

It's difficult to give more clues without providing the whole code so here's a working example.

(You should have posted your attempts and asked for specific help but wth.)

//http://forum.arduino.cc/index.php?topic=464276
//ardy_guy March 2017

byte buttonPin = 2;
bool buttonVal;
byte myServoPin = 3;
byte myServoPos = 90;
int myServoStep = 1;
byte myServoMax = 175;
byte myServoMin = 5;

#include <Servo.h>
Servo myServo;

void setup()
{
  Serial.begin(9600);
  Serial.print("Servo starting at ");
  Serial.println(myServoPos);
  myServo.write(myServoPos); //which is 90 for now
  myServo.attach(myServoPin);
  pinMode(buttonPin, INPUT_PULLUP); //button from pin to ground
  Serial.println("Press button to move servo; release to stop");
}//setup

void loop()
{

  buttonVal = digitalRead(buttonPin);

  if (buttonVal == LOW) //with input_pullups, pressed is low
  {
    myServoPos = myServoPos + myServoStep;
    Serial.println(myServoPos);
    myServo.write(myServoPos);

    //reverse direction at the end of travel
    // || means OR
    // mult by -1 changes sign of step
    if (myServoPos >= myServoMax || myServoPos <= myServoMin)
    {
      Serial.print("Servo changing direction to ");
      myServoStep = -1 * myServoStep;
      Serial.println(myServoStep);
    }
  }//button pressed
}//loop