Voice Recognition Module V2

Hello
Iam working on voice switched lights. It works as intended but i would like to make some improvements and iam sensing light intensity in room with LDR and depending on it make LED brighter or darker. Problem is with function LDR_check, I would like to work it like that when i say “lights on”, LED turns on and adjusts intensity as long as I dont say “lights off”. Here is my code:

int LED = 3;
int LDR = 0;
int LDRvalue = 0;

byte com = 0; //reply from voice recognition

unsigned long LDR_time = millis() + 5000;

void LDR_check() {
  
  if (LDRvalue <= 170) {
      //if (time >= LDR_time ) {
        analogWrite(LED, 255);
        //LDR_time = time + 5000;      
      //}
    }
        
    else if (LDRvalue > 170 and LDRvalue <= 340 ) {
      //if (time >= LDR_time ) {
        analogWrite(LED, 170);
        //LDR_time = time + 5000;      
      //}
    }
      
    else if (LDRvalue > 340 and LDRvalue <= 511 ) {
      //if (time >= LDR_time ) {
        analogWrite(LED, 85);
       //LDR_time = time + 5000;      
     //}
    }
      
    else {
      //if (time >= LDR_time ) {
        analogWrite(LED, 0);
        //LDR_time = time + 5000;      
      //}
    }
}

void setup() { 

  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(LDR, INPUT);
  
  delay(2000);
  Serial.write(0xAA);
  Serial.write(0x37);

  delay(2000);
  Serial.write(0xAA);
  Serial.write(0x21);
  
}

void loop() {
  
  while(Serial.available())
  
  {
    
  com = Serial.read();
  LDRvalue = analogRead(LDR);
  unsigned long time = millis();
  
  if (com == 0x11) {
    LDR_check();
  }
  
  else if (com == 0x12) {
    analogWrite(LED, 0);

  }
 } 
}

Problem is with function LDR_check, I would like to work it like that when i say "lights on", LED turns on and adjusts intensity as long as I dont say "lights off".

Then LDR_check() is a lousy name for the function.

I presume that 0x11 is what the Arduino reads from the serial port when you says "lights on" and 0x12 is what is received when you say "lights off". What should happen when you says lights on, and the light gets to full intensity?

You should separate the "get some idea what to do" part of the code from the "do that" part of the code. On every pass through loop, you might get something that defines what to do. On every pass through you loop, you should do what needs to be done.

In other words, the renamed LDR_check() function should be called on every pass through loop(). What it does depends on some arguments that you pass it, based on what you said, or on the values in some global variables (set based on what you said to do).

Yeah 0x11/0x12 Arduino gets via serial port from voice module when i say voice instruction. When i say “lights on” it should get intensity dependent on check in LDR_check function (in other worlds to intensity dependent on LDR value).