How to interface this no nc c switch with arduino

It worked for me when i write the code in a new file and run it..


int buttonState = 0;

void setup() {
  pinMode(7, INPUT_PULLUP);
  pinMode(12, OUTPUT);
}

void loop() {
  buttonState = digitalRead(7);
  if (buttonState == HIGH) {
      digitalWrite(12, HIGH);
  } else if (buttonState == LOW) {
    digitalWrite(12, LOW);
  }
}

but when i add the same code in my project code it didn't work please can u tell me why
?

#include <Servo.h>


Servo myservo; 
int pos = 0;  
int buttonState = 0;


void setup() {
  myservo.attach(9);
  pinMode(12, OUTPUT); //led pin
  pinMode(7, INPUT_PULLUP); // switch pin
 }

void loop() {   

 buttonState = digitalRead(7);
  if (buttonState == HIGH) {
      digitalWrite(12, HIGH);
  } else if (buttonState == LOW) {
    digitalWrite(12, LOW);
  }


   
 for (pos = 0; pos <= 165; pos += 1) { /* goes from 0 degrees to 165 degrees in steps of 1 degree */
 
 myservo.write(pos);  
      
    delay(25); 
  }

delay(3000);

for (pos = 165; pos >= 0; pos -= 1) { /* goes from 165 degrees to 0 degrees */
 
    myservo.write(pos);           
    delay(25);    
}
delay(3000);

    }