Servo won't return to 0

Hello everyone. This one has really got me stumped. I hope someone can help. I added two programs.

The first, short one, works fine. Servo turns to 180 degrees and returns to 0.

When I use it in the longer program, in case #4, everything in the sketch works BUT the servo motor won't return to 0, just stays at 180. Any help is appreciated.

#include <Servo.h>
Servo myservo2;
int pos2 = 0;
int i = 0;

void setup() {
  pinMode(6, OUTPUT);

void loop() {
  myservo2.attach(6);
  ;
  for (pos2 = 0; pos2 <= 180; pos2 += 1) {  // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree
    myservo2.write(pos2);                    // tell servo to go to position in variable 'pos'
    delay(20);                            // go slowly in this directin
  }
  delay (300);
  for (pos2 = 180; pos2 >= 0; pos2 -= 1) {     // return to 0 degrees
    myservo2.write(pos2);                     // tell servo to go to position in variable 'pos'

    delay(20);                               // waits 15ms for the servo to reach the position
  }
  
  myservo2.detach();

}

// Haunted Book Sketch
#include <Servo.h>            //Library
Servo servo;
Servo servo2;                  //name servo motors

int pos = 0;
int pos2 = 0;
int i = 0;
int val = 0;
int ranNum;                   //to choose a random case (0,1,2,3,4,5)
int ranDel;

const int KnockSensor = A4;   //knock sensor
const int THRESHOLD = 300;    //How hard a Knock to start  Cases
const int pwmLED = 5;         //Green Fadeing Led



void setup() {

  Serial.begin  (9600);
  pinMode(A0, INPUT);         // Randon Seed for Case Choice
  pinMode(A4, INPUT);         // Knocksensor Pin
  pinMode(2, OUTPUT);         // Evil Laugh
  pinMode(3, OUTPUT);         // Open book
  pinMode(4, OUTPUT);         // Rumble
  pinMode(6, OUTPUT);         // Move Book
  pinMode(7, OUTPUT);         // blue light
  pinMode(pwmLED, OUTPUT);    // Fade (pin 5)

}

void loop() {

  sensor();                         // check the sensor
  resume();                         // go to start randon cases
}
void sensor() {
  val = analogRead(KnockSensor);     // read the knock sensor
  if (val < THRESHOLD) {             // if sensor not activated, keep checking
    sensor();                        // keep checking
  }
  else  {                            // if the sensor is disturbed
    resume();                      //  go to choose a case action
  }
}

void resume() {

  delay(3000);                       // How long between actions, will replace with 60000 (one minute)

  ranNum = random(0, 6);             //Generate random number between 0 and 5
  ranDel = random(100, 150);         // random delay time
  int randChoose = random(7);        // Number of cases         //randomly choose a case
  switch (randChoose) {

    //-----------------CASES-------------------------------------------------

    // ------------------------------Evil Laughter Sound-----------------------
    case 0 :
      digitalWrite(2, HIGH);             // recording plays
      delay(3000);                       // time for recording to play
      digitalWrite(2, LOW);              // stop

      resume();                           //go to choices again

    //-------------------------------Book open and close-->WITH BLUE LIGHT--------------------------------------
    case 1 :
      servo.attach(3);
      for (pos = 0; pos <= 35; pos += 1) {   // goes from 0 degrees to 35 degrees  Steps in steps of 1 degree
        servo.write(pos);                    // tell servo to go to position in variable 'pos'
        digitalWrite(7, HIGH);
        delay(100);                            // go slowly in this directin
      }
      delay(500);                            //wait 1.5 sec
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        servo.write(pos);
        digitalWrite (7, LOW);                  // tell servo to go to position in variable 'pos'
        delay(15);                               // waits 15ms for the servo to reach the position
      }
      servo.detach();                          //stop right there before you go any further

      resume();

    //----------------------------------Green Fade---------------------
    case 2:

      for (int i = 0; i < 255; i++) {
        analogWrite(pwmLED, i);                //write the i value to pin 5
        delay(5);                             //wait 5 ms then do the for loop again
        digitalWrite (pwmLED,  HIGH);

      }
      for (int i = 255; i > 0; i--) {           //descrease i with 1
        analogWrite(pwmLED, i);
        delay(5);
        analogWrite(pwmLED, LOW);
      }

      resume();                           //go to choices again

    //-------------------------------- Rumble Motor-------------------------------------------
    case 3:

      digitalWrite(4, HIGH);   //turn on the rumble motor
      delay(150);
      digitalWrite(4, LOW);

      resume();

    //-----------------------------Book Move------------------------
    case 4:
    
      servo2.attach(6);                                    // load the servo motor
      for (pos2 = 0; pos2 <= 180; pos2 += 1) {               // goes from 0 degrees to 35 degrees  Stepsin steps of 1 degree
        servo2.write(pos2);                                    // tell servo to go to position in variable 'pos'---book cover opens slightly
        delay(20);
      }
      //delay(300);                                           //wait 1.5 sec
      for (pos2 = 180; pos2 >= 0; pos2 -= 1) {                 // return to 0 degrees
        servo.write(pos2);
        delay(40);                                         // waits 15ms for the servo to reach the position
        servo2.detach();
      }
      
      resume();

    //------------------Book Open ----------------------------
    case 5 :  servo.attach(3);               //turn the servo on
      for (pos = 0; pos <= 35; pos += 1) {     // goes from 0 degrees to 35 degrees in steps of 1 degree

        servo.write(pos);                    // tell servo to go to position in variable 'pos'
        delay(100);
      }
      for (pos = 35; pos >= 0; pos -= 1) {     // return to 0 degrees
        servo.write(pos);
        digitalWrite (7, LOW);                  // tell servo to go to position in variable 'pos'
        delay(15);
      }
      servo.detach();                         // turn the servo off

      resume();

    //--------------------------Rumble motor and green light---------------------------------
    case 6 :
      digitalWrite(4, HIGH);   //turn on the rumble motor
      digitalWrite (pwmLED, HIGH);  //green light on too
      delay(250);
      digitalWrite(4, LOW);
      digitalWrite(pwmLED, LOW);
  }
  resume();
}
      for (pos2 = 180; pos2 >= 0; pos2 -= 1)                   // return to 0 degrees
      {
        servo.write(pos2);
        delay(40);                                         // waits 15ms for the servo to reach the position
        servo2.detach();
      }

You have got detach() inside the for loop

Now I feel really stupid. I should have left it until tomorrow> Thank you UKHeliBob

Now I feel really stupid

We have all been there and will be there again

Reviewing your own code is very difficult

      for (pos2 = 180; pos2 >= 0; pos2 -= 1) {                 // return to 0 degrees
        servo.write(pos2);
        delay(40);                                         // waits 15ms for the servo to reach the position
        servo2.detach();
      }

AND you are moving 'servo', not 'servo2'.
AND your '15ms' delay is 40ms.