Issue with Arduino UNO remembering previous state.

I'm currently trying to get my Arduino to turn on my stereo when it senses that the projector is on.

(also making it so that it lights different LED's to indicate a temperature range, this part works just fine but I felt needed to be explained from the script)

I want to have the photoresistor senses a value that indicates the projector is on, then turn my Stereo on with an IR LED.
After this, if the Arduino senses that the projector is off, rememebring it had turned the stereo on before, it turns the stereo off.

From my script is there any way I can make this better/more efficient?
and
What am I doing wrong?

Thanks for any help

#include <IRremote.h>
#include <IRremoteInt.h>
#define photoresistor_pin A0

IRsend irsend;
int RECV_PIN = 5;
int ledPinB = 10; //blue led
int ledPinW = 9;  //white led
int ledPinR = 8;  //red led
IRrecv irrecv(RECV_PIN);
decode_results results;

int sensor_analog = A1;
int sensor_analogP = A0;
unsigned long t;
unsigned long R;
int threshold_value=28; // value below which it gets triggered
bool flag=0;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(ledPinB,OUTPUT);
  pinMode(ledPinW,OUTPUT);
  pinMode(ledPinR,OUTPUT);
  pinMode(sensor_analog,INPUT);
  pinMode(sensor_analogP,INPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  int sensorValueA = analogRead(sensor_analog);
  int sensorValueB = analogRead(sensor_analogP);
  // print out the value you read:
  t = millis( );
  float voltageA = sensorValueA * (5.0/1023.0);
  float voltageB = sensorValueB * (5.0/1023.0);
  R = (50000/voltageA) - 10000;
  float tempC = ((1/(0.001129148 + 0.000234125 * log(R) + 0.0000000876741 * pow(log(R), 3))) - 273.15)-3;
  float tempF= (9*tempC/5+32);  //calebrated from existing digital thermometor
  //Serial.print(t);
  //Serial.print(",");
  //Serial.print(sensorValueB);
  Serial.print(flag);
  Serial.print(", ");
  Serial.print(analogRead(photoresistor_pin));
  //Serial.print(voltageB);
  Serial.print(", ");
  Serial.print(tempC);
  Serial.print(" C");
  Serial.print(", ");
  Serial.print(tempF);
  Serial.print(" F");
  //Serial.print((9*tempC/5+32)-2);
  Serial.println("\t");
  delay(1500);        // debouncing
  

  //Red LED
  if(tempF >73){
    digitalWrite(ledPinR,HIGH);
    
  }
  if (tempF < 73){
    digitalWrite(ledPinR,LOW);
  }
  
  //White LED
  if((tempF<=73) && (tempF>=71)){
    digitalWrite(ledPinW,HIGH);
    
  }
  if ((tempF>=73) or (tempF<=71)){
    digitalWrite(ledPinW,LOW);
  }

  //blue LED
  if(tempF < 71){
    digitalWrite(ledPinB,HIGH);
    
  }
  if (tempF > 71){
    digitalWrite(ledPinB,LOW);
  }

  //stereo sensig projector on
  //if (voltageB > 0.25){
    //irsend.sendNEC(0x81CE728D,32); //turn on stereo
    //Serial.print("Stereo on/off triggered");
    //flag=1;
  //}
  //if (sensorValueB > 29){
    //irsend.sendNEC(0x81CE728D,32); //turn on stereo
    //Serial.print("Stereo on/off triggered");
  //}

  
  if(analogRead(photoresistor_pin)>threshold_value&&flag==0){

   Serial.println("turning on the stereo");
   irsend.sendNEC(0x81CE728D,32);//turn on the stereo
   delay(50);
   flag=1;
  if(analogRead(photoresistor_pin)<threshold_value&&flag==1){
   //if(analogRead(photoresistor_pin)<threshold_value&&flag==1){
   Serial.println("turning off stereo");
   irsend.sendNEC(0x81CE728D,32); //Turn off the stereo
   flag=0;
  }
  }
}