Using a servo as a switch with different sensors

Hey guys, so I need help with my code. I'm trying to make a switch for my light switch where i can use an IR remote, a photo sensor (light), and a switch(push button) to control a servo to turn on or off my lights. I'm stuck on the part of the code where i coded the push button in to control the rotation of the servo, but where I'm stuck is that I cant quite figure out how to make a loop so that the servo can be used by the other switches as well. Right now because the pushbutton is always giving a reading, the servo is stuck on one angle and won't respond to another switch (IR remote). THANKS IN ADVANCE!!!

// C++ code
//
#include <Servo.h>
#include <IRremote.hpp>
int servoPin=6;
int pos1=180;
int pos2=0;

int buttonPin=12;
int buttonNew;
int buttonOld=1;

int photoPin=A4;
int photoVal;

int IRpin=3;

int lastswing;
int test;

int dt=1500;
Servo myServo;

void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IRpin);
  myServo.attach(servoPin); 
  pinMode(buttonPin,INPUT);
  pinMode(photoPin,INPUT);
  
}

void loop()
{
  myServo.write=test;
  
 photoVal=analogRead(photoPin); //photo resistor
  Serial.print("light = ");
  Serial.println(photoVal);    //290 min 
  

  while (IrReceiver.decode()){
    Serial.println(IrReceiver.decodedIRData.decodedRawData,HEX);
   switch(IrReceiver.decodedIRData.decodedRawData){
      case 0xBC43FF00:
      Serial.println("works");
      myServo.write(pos1);  //Servo

    }
    switch(IrReceiver.decodedIRData.decodedRawData){
      case 0xBB44FF00:
      Serial.println("Also works");
      myServo.write(pos2);  //Servo
    }
    IrReceiver.resume();
  }
  
 buttonNew=digitalRead(buttonPin);  //Push button

if (buttonOld==0 && buttonNew==1){
  myServo.write(pos1);
  }
else{
  myServo.write(pos2);
}
delay(250);
}

Welcome to the forum

You have taken the right approach by attempting to detect when the button becomes pressed rather than when it is pressed

However, your sketch never updates the value of buttonOld so the comparison with buttonNew is a waste of time. Moreover, as the value of buttonOld never changes from its original value of 1 then testing it for a value of 0 is a waste of time

In addition to that problem, do you have a resistor keeping the button pin in a known state at all times or is it floating at an unknown value, maybe HIGH, maybe LOW. maybe changing because it is picking up stray electrical interference ?

Use a toggle paradigm. On the first press move servo to 180, on second press move it back to 0.

Also I want it to be where whenever I press the button once, the servo rotates in the opposite direction. In this way, i can use the IR remote to turn off the light then i can use a single press to turn back on the light with the pushbutton.

Seeing as though I am relatively new, I understand what you are saying but implementing it is a different case. Also I want it to be where whenever I press the button once, the servo rotates in the opposite direction. In this way, i can use the IR remote to turn off the light then i can use a single press to turn back on the light with the pushbutton.

Have you looked at the StateChangeDetection example in the IDE ?

haven't yet, ill take a look when i get the chance later. THanks.