Sensor digital read data dont change

HI!
Im using Arduino Mega2560. I have sensor connected to my arduino board.
I need to read the sensor and check that I read request value for 200ms,
I wrote the following code. It works well, but I can't use while because it is blocking other things.
I couldn't have any Idea how to use this technique without while.
Please if can someone help me!!!
my code is:

uint8_t timedBoxSensorsDontChange(uint32_t ms, uint8_t value) {

  uint32_t start = millis();

  while (millis() - start < ms) { 
    if ( digitalRead(StationSensor) == value )
    {
      return 0;
    }
  }
  return 1;
}

Thank you!!

Let loop() do the looping and use if instead of while

if (condition == true)
{
  //do something
}
else
{
  //do something else (or nothing)
}

The function must be out of loop.
the loop read commands from Serial port and proccess the commands, If i get specific command then I need this check.
my loop:

void loop() {
      if (COMM_SERIAL.available()) {
        processCommInput(COMM_SERIAL.read());
      }
    }

void processCommInput (unsigned char c) {
    if (COMMAND_START_CHAR == c) {

      commandBufferIndex = 0;
      commandCollecting = 1;
    }

  } else {
    commandBuffer[commandBufferIndex++] = c;
    if (COMMAND_BUFFER_SIZE == commandBufferIndex) {
      processCommandBuffer();
      commandCollecting = 0;
    }
  }

proccessCommandBuffer() proccess incomming command and check sensors accoring to the command.

Your topic was MOVED to its current forum category as it is more suitable than the original as it is not a suggestion for the Arduino project

What other things is your while loop blocking?

reading the serial in the loop and doind the other commands. and responsing,
200ms is short, but I want to use the function for longer time.

So you want to read a stream of values from serial, ensuring that the same value is available for a number of millisecond, while simultaneously doing something else? Please supply more details on what it is you're trying to create.

too much "}"

My loop read commands from serial. then proccess the command and do something according to the command id:
'L'- lighting led on .
'D'- check if sensor is on for 1000ms.
whenevey I got the command I need to do the request proccess.
my problem is with the code above: It blocks the code and in those millisecond the arduino is not reading the other commands.
I need to read the sensor for X ms and get the same value, not iin the loop.

void processCommInput (unsigned char c) {
    if (COMMAND_START_CHAR == c) {

      commandBufferIndex = 0;
      commandCollecting = 1;
    }

 else {
    commandBuffer[commandBufferIndex++] = c;
    if (COMMAND_BUFFER_SIZE == commandBufferIndex) {
      processCommandBuffer();
      commandCollecting = 0;
    }
  }
}

bool  timedBoxSensorsDontChange( bool value) {
  return digitalRead(StationSensor) != value;
}

Thanks.
But how should I verify that I see the same data from 1000 ms??

It's separate logic, different question. It's useful to phrase "has been something for a period of time" to "has never not been something for a period of time". The way you do that is like:

if (now - timestamp > period)
  {
  code you do when sensor has been active for 1000
  }
if (sensor is not active)
  {
  time stamp = now
  }

but when you awaiting low level on sensor pin it is anyway nothing other executed.
or do you accept that readings will not whole 1000ms but sometimes in this period?

sometimes in this perdiod is perfect for me.

void loop() {
  if (COMM_SERIAL.available()) processCommInput(COMM_SERIAL.read());
  if (commando_to_sensing)isSensingEnable = true;
  timedBoxSensorsDontChange( _ms, _value );
}

bool is1000msStart = false;
bool WasSensorEven = false;
bool isSensingEnable = false;

void timedBoxSensorsDontChange(uint32_t ms, uint8_t value) {
  static uint32_t startMillis = 0;
  if (is1000msStart) {
    if (millis() - startMillis < ms) {
      if ( digitalRead(StationSensor) == value )WasSensorEven = true;
    } else {
      is1000msStart = false;
      isSensingEnable = false;
    }
  } else {
    if (isSensingEnable) {
      is1000msStart = true;
      startMillis = millis();
      WasSensorEven = false;
    }
  }
}

Thank you!
That works well.