Three buttons to control a servo

HI. I am new to arduino. I have found most sample with 1 and 2 push buttons to contol a servo on the net. i have tried and it work. However when i try adding one more button to do some task, combining the codes, it did not work fully. What i have are 3 momentary push button. Button 1 when pressed will move servo to 90 and back to 5 when released. Pressing button 2 will move servo to 90 and stop. button 3 when pressed will bring back servo to 5 from 90. i tried for button 1 alone and it worked. tried on button 2 and 3together it worked. Combining the three then it not work fully. Button 2 and 3 are alright except button 1. I am not sure where it’s wrong . Help and advised needed. Thanks. The codes all i got from the net. here the code:

 #include <Servo.h>
Servo myservo;
int pos =5;

void setup() {
  // put your setup code here, to run once:
   pinMode(13, OUTPUT);
   pinMode(3, INPUT);
    pinMode(4, INPUT);
   pinMode(7, INPUT);
   myservo.attach(9);
}

void loop() {
 
  if (digitalRead(3) == 1) // to make servo move to 90 when button is pressed and 
  { digitalWrite(13, LOW);//back to 5 when released
    myservo.write(90);}

  if (digitalRead(4) == 1) // to make servo move back to 5 from 90
  {digitalWrite(13, HIGH);
    myservo.write(5);
   pos = 5;}
   
  if (digitalRead(7) == 1) // to make servo move to 90 and stop
  {digitalWrite(13,LOW);
    myservo.write(90);
    pos = 90;}
    
    else 
    
  {myservo.write(pos);}


}

changed an if to a while loop and some other changes…

#include <Servo.h>
Servo myservo;
int pos = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(7, INPUT);
  myservo.attach(9);
}

void loop() {

  while (digitalRead(3) == 1) // to make servo move to 90 when button is pressed and //back to 5 when released
  {
    digitalWrite(13, LOW);
    myservo.write(90);
    pos = 5;
  }

  if (digitalRead(4) == 1) // to make servo move back to 5 from 90
  {
    digitalWrite(13, HIGH);
    pos = 5;
  }

  if (digitalRead(7) == 1) // to make servo move to 90 and stop
  {
    digitalWrite(13, LOW);
    pos = 90;
  }
  myservo.write(pos);
}

I would approach this sort of problem differently.

I would have a function to read the button and save a value in a variable. For example, save 1 for button 1, 2 for button 2 etc.

Then I would have another function to move the servo depending on the saved value. It would be something simple like

void moveServo() {
   if (buttonVal == 3) {
      servoPos = 90;
   }
   if (buttonVal == 2 || buttonVal == 7 ) { // I am assuming 7 is special
      servoPos = 5;
   }
   if (buttonVal == 1) {
      servoPos = 90;
      buttonVal = 7; // to make it return
   }
   myServo.write(servoPos);
   delay(100); // crude way to allow servo time to move
}

The code in loop() would just be

void loop() {
    readButtons();
    moveServo();
}

...R

Ohhh...the use of while loop come across my mind but not sure where to put it. Now I know better when to use if and while. Now the servo runs as what I want.

Robin2, your approach looks quite simple. I think I understand it and would give a try in my other project. Thanks guys.