while loop for defined time

Hey all,
I am quite new to the Arduino environment and connected 2 sensors and a LCD to my Arduino
I am able to read both sensor values and print them on the lcd and want to switch every 5 sec between the sensor input. I tried with delay but then during this time span the sensor values do not update

Next I hoped to get things done with a while loop where I do Operation 1 for a defined amount of time and then switch to operation 2.
I set up a very simple sketch to slowly build it into my other sketch
But somehow I never end the while loop to proceed to "operation 2"

Any suggestions?!
Thanks
M

//*********************************
unsigned long time;
const float screentime = 5000;

void setup() {
Serial.begin(9600);
}
void loop() {

time = millis();

while (time - screentime <= 0)
{
Serial.println("option 1");
}
Serial.println("option 2");

}

  while (time - screentime <= 0)

You're stuck in the while() loop because 'time' never gets updated so your while condition always stays true.

Try

while (millis() - screentime <= 0)

You don't need the 'time' variable because you're only interested in the current millis() time and there's no need to save it.

And, screentime should be a type long.

Please explain how you think it works, since there are no comment lines.

Read How to use this forum and post your sketch using code tags.

Timing should always be handled with subtraction to find intervals.

You should avoid while() in loop(). Use if() instead. While() is blocking code and you should try to avoid using blocking code in the loop().

Here's why.

I rewrote your code:

unsigned long timeNow;
unsigned long screenTime = 1000;

void setup() {
  Serial.begin(115200);
}
void loop() {
  timeNow = millis();
  while (millis() - timeNow < screenTime ) {
    Serial.println(millis()-timeNow);
  }
  Serial.println("option 2");
}

The console output prints 0 to 999 then "option 2".

You don't say what board you are using. If it's a standard Arduino, Uno, Due, Megs, Nano, etc, the code will work. However, if sometime in the future you put this code on an ESP8266- based board, it will surely fail.

Arduino compatible boards that use the ESP processors will fail if the screenTime is > 3 seconds. Why? The ESP is a little different than the standard Arduino boards in that it has the watchdog(WDT) turned on by default. If the watchdog timer isn't periodically reset then it will automatically reset your ESP. The watchdog is reset every time loop() runs or you call delay() or yield() but if you have blocking code like the above then the watchdog may time out, resulting in a reset. This is an example of why blocking code should be avoided in your loop().

Adding a yield(); to the while loop "fixes" the WDT timeout, but the correct fix is to not use blocking code in the first pace.

unsigned long timeNow;
unsigned long screenTime = 5000;

void setup() {
  Serial.begin(115200);
  timeNow = millis();
}
void loop() {
  if (millis() - timeNow < screenTime ) {
    Serial.println(millis() - timeNow);
  } else {
    Serial.println("option 2");
    timeNow = millis();
  }
}