Servo not complying with 'if' conditions. Using millis() to control servo.

I want the servo to rotate for 5 seconds, then wait for 10 seconds. During this waiting period I want the servo to be able to take input from the two pushbuttons that rotate the servo in either direction.

This is the program I have so far:

#include <Servo.h>

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

int pos = 0;  // variable to store the servo position
unsigned long currentMillis = 0;  //  stores the value of millis()
unsigned long runtime = 5000;  // stores time for the servo to run
unsigned long waittime = 10000;  // stores time for the servo to wait

void setup() {
  // put your setup code here, to run once:
  pinMode (2, INPUT);  // pushbutton input 1
  pinMode (3, INPUT);  // pushbutton input 2
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  unsigned long currentMillis = millis(); // grab current time

  if ((unsigned long)currentMillis <= runtime) {  // rotates the servo till currentMillis is < 5000ms
    for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 1ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 1ms for the servo to reach the position 
  }
  }

  if ((unsigned long)currentMillis <=waittime+runtime) {
    myservo.write(0);                                 //  sends the servo back to 0 degrees
    
    if (digitalRead(2) == HIGH && pos < 180) {        // checking if pushbutton is pressed and servo position is less than 180
    pos++;  //  increment position
    myservo.write(pos);                               // tell servo to go to position in variable 'pos'
    delay(1);                                         // waits 1ms for the servo to reach the position
  }
    if (digitalRead(3) == HIGH && pos > 0) {          // checking if pushbutton is pressed and servo position is more than 0
    pos--;                                            // decrement position
    myservo.write(pos);                               // tell servo to go to position in variable 'pos'
    delay(1);                                         // waits 1ms for the servo to reach the position
  }
  }
  runtime = currentMillis + 5000;                     // adding 5000ms to 'runtime' variable for the next loop
}

When I upload this sketch, rather than rotating for 5 seconds (in accordance with first 'if' condition) and the returning to 0 degrees and waiting for pushbutton input (in accordance with the second 'if' condition), the servo just keeps on indefinitely rotating in clockwise and anticlockwise direction. Also, pressing the pushbuttons doesn't seem to affect the continuous clockwise and anti-clockwise rotation. Is the program getting stuck in the first 'if' condition?

Can someone please point out the corrections I need to make? If there is anything else you would like to know that would help you solve this issue better then I'll update the post.

Servo is connected to digital pin 9. Positive pin of the servo is connected to +5v on arduino, negative pin to GND on arduino. This is how the pushbuttons are connected:

You are not using millis() properly. Have a look at the demo Several Things at a Time. Also look at how it move the servo without FOR. Using delay() and FOR and WHILE can make a program unresponsive because they all block until they complete.

And you should really give the servo its own power supply with a common GND with the Arduino. Servos can draw more current than the Arduino 5v pin can provide.

...R

Is the program getting stuck in the first 'if' condition?

Add some Serial.print()s so that you know which parts of the program are running.

Are you sure that the switches are inserted into the breadboard the right way round so that they make contact when you assume they do when pressed ? Try using a jumper wire as a switch between 0V and 5V instead of the switch.

Consider wiring the inputs to be taken LOW when the switch is pressed, remove the external resistors and use INPUT_PULLUP in the pinMode()s. This reduces wiring and components and is easier to manage.

It is generally not a good idea to power servos from the Arduino due to the current they require.

UKHeliBob:
Are you sure that the switches are inserted into the breadboard the right way round so that they make contact when you assume they do when pressed ? Try using a jumper wire as a switch between 0V and 5V instead of the switch.

The pushbutton program runs fine independently.

#include<Servo.h>
int pos = 0;

Servo servo;
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  servo.attach(9);
}
void loop() {
  if (digitalRead(2) == HIGH && pos < 180) {
    pos++;
    servo.write(pos);
    delay(15);
  }
  if (digitalRead(3) == HIGH && pos > 0) {
    pos--;
    servo.write(pos);
    delay(15);
  }
}

But when I combine this code with the other one, it does not respond to the pushbutton presses.

The servo is a small one SG90 9gm. I guess it does put some load on the arduino because the leds on the arduino fade a bit when the servo rotates. Will give it a dedicated supply when I am done with the program.

Currently working on what @Robin2 suggested.