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);
}
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.