Trying to configure Servo Motor

I'm new to arduino, i'm trying to use 2 push button to rotate the servo in 0 and 180 ang, but i want to know if it's possible to only push and not to hold the button until the servo complited the rotation. This is my code

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int angle =90;    // initial angle  for servo

int angleStep =50;

#define LEFT 3   // pin 12 is connected to left button

#define RIGHT  2  // pin 2 is connected to right button

void setup() {

  // Servo button demo by Robojax.com

  Serial.begin(9600);          //  setup serial

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button

  pinMode(RIGHT,INPUT_PULLUP);// assing pin 2 as input for right button

  myservo.write(angle);// send servo to the middle at 90 degrees

 Serial.println("Robojax Servo Button ");

}

void loop() {

  // Servo button demo by Robojax.com

  while(digitalRead(RIGHT) == LOW){

    if (angle > 0 && angle <= 180) {

      angle = angle - angleStep;

       if(angle < 0){

        angle = 0;

       }else{

      myservo.write(angle); // move the servo to desired angle

      Serial.print("Moved to: ");

      Serial.print(angle);   // print the angle

      Serial.println(" degree");

       }

    }

  delay(100); // waits for the servo to get there

  }// while

 // Servo button demo by Robojax.com

  while(digitalRead(LEFT) == LOW){

    // Servo button demo by Robojax.com

    if (angle >= 0 && angle <= 180) {

      angle = angle + angleStep;

      if(angle >180){

        angle =180;

       }else{

      myservo.write(angle); // move the servo to desired angle

      Serial.print("Moved to: ");

      Serial.print(angle);   // print the angle

      Serial.println(" degree");

      }

    }

 delay(100); // waits for the servo to get there

  }// 

}

Welcome to the forum

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

The answer is yes

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

This code does exactly that. A quick press of either button sends the servo to the programmed location in steps of 50 degrees and prints those steps to the Serial Monitor. Perhaps your buttons are not wired correctly? Show a wiring diagram and describe what you observe.

@xdoggo are you aware that you can't read the angle of the servo when it is in motion? Therefore you can't measure when exactly the servo has finished its movement.

All the reading of the angle does is to return the last target value the servo was set to go to.