How to make a loop run for a specific time then switch to another loop and then back and so on

Hi i am a beginner in arduino and i am adding a temperature and humidity sensor to my project but to display the percentage and temps and other stats on display i have to switch between data . for that how can you for example; show smoke intensity on screen for 10 seconds then switch to a screen showing temperature and humidity for 10 seconds then back and the loop continues forever

i am using an arduino uno R3, DHT11 temp and humidity sensor and an lcd 16 x2 display
also can u create multiple loops in a code and choose which loop works at this momment
i would be so thankfull if anybody helps me
thank you

Use a variable to indicate what you want to display.

E.g.

uint8_t toDisplay = 0;

In loop, you can check that variable and modify it. Below is the most basic version. Note that this is blocking code.

void loop()
{
  if (toDisplay == 0)
  {
    // for the next time
    toDisplay = 1;

    display temperature
  }
  else
  {
    // for the next time
    toDisplay = 0;

    display humidity
  }

  delay(10000);
}

in the more general case, you might benefit from studying state machines.

Here is a small introduction to the topic: Yet another Finite State Machine introduction

and you should look at dealing with millis() if you don't want to block your code. See

1 Like

Your topic does not indicate a problem with IDE 2.x and hence has been moved to a more suitable location on the forum.

As a follow-on to post 13, here's a basic state machine which uses timing to progress from one state to another for two of the four states.

1 Like

Hello 4ruben36

Post the current sketch to see how we can help.

you can use a timer and a state variable to sequence thru a # of different displays while doing other things

........... this
.......... that
........... other
.......... this
.......... that
.......... other
........... this
.......... that

const unsigned long MsecPeriod = 1000;
      unsigned long msec0;

int dispIdx;

void dispThis  () { Serial.println (" this"); }
void dispThat  () { Serial.println (" that"); }
void dispOther () { Serial.println (" other"); }

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();
    if (msec - msec0 >= MsecPeriod) {
        msec0 = msec;

        switch (dispIdx) {
        case 0:
            dispThis ();
            break;

        case 1:
            dispThat ();
            break;

        case 2:
            dispOther ();
            break;
        }

        if (2 < ++dispIdx)
            dispIdx = 0;
    }

    Serial.print (".");
    delay (100);
}

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

Instead of multiple loops we have one loop (void loop()) and inside of that multiple blocks of code that run when we want them to.
We can trigger a block inside of void loop()...

if (trigger expression)
{
do the thing
}

where the trigger may be time or a value in a variable.

Time on Arduino is best done with unsigned integers like

unsigned long or unsigned int

and we use either millis() or micros() to get the value of "now".

The one way that always works (even past rollover) is;

end time - start time = elapsed time

With end time usually being now; millis() or micros()

and the code usually being

if ( millis() - start >= desired_interval )
{
wait time has elapsed so do the thing like switch the led On/OFF
}

So if one block checks a sensor every so often it may save the read in one variable and set a byte variable to 1 to serve as a flag/trigger for a display-the-value block which displays the value then sets the flag=0.

if ( millis() - start_time >= read_interval )
{
variable = sensor.read();
start_time += read_interval; // is case the >= was more
flag = 1;
}

.... later inside loop()

if ( flag )
{
serial.print( variable );
flag = 0;
}

or you could display the read when and where it is made....

but these are different things inside of the one loop() and examples of making what needs to run, run.

If you want multiple loops, then you might want to take a look at this example.

Thank you this worked