Looping with multiple tasks

Hi there,

first of all, hello to everyone; I am the newbie to this forum and to arduino.

I guess my question has been discussed, but I am not able to find a suitable answer for me.
To be honest, I not sure, what to search for.

So here comes my first question:

Which possible solutions are there for handle multiple task in the loop at different times?

To explain:
Currently I have a LCD Display, a SD and some buttons connected to the arduino.
And I would like to connect a MPU-6050, but this just by the way....

I am working with Modulo in my loop, but I have the feeling, that there could be a better way.

For example:

  • Check for a toggeled switch/button
  • Update the display often
  • Write on SD from time to time

Would be nice if you guys share some ideas or docu-links on howto handle the loop effective.
(I'm working with c++ on PC and there you have SIGNAL/SLOT mechanism, I have to readjust to the concept)

Thanks in advance,
Rene

What I'm doing now is:

long loopCounter=0;
void loop()
{
 check_if_switch_interupt_was_toggled();

    if ( loopCounter % 5000 == 0)
    {
      updateLCD();
    }

    if ( loopCounter % 20000 == 0)
    {
      if (newData)
      {
         writeSD();
       }
    }

    loopCounter++;
    //reset to prevend overflow
    if (loopCounter>1000000)
    {
      loopCounter=0;
    }
}

Read this

demo code for doing many things at the same time

EastCoastKiter:
For example:

  • Check for a toggeled switch/button
  • Update the display often
  • Write on SD from time to time

And (in addition to what @holmes4 said) the way you have described things here is very similar to the approach in planning and implementing a program.

...R

Hi Holmes,

working with milles() seems to be a good idea.

But to be honest, I thought there is a more elegant way to a "task scheduler" rather the defining each step again and again in every function.

Thanks,
Rene

EastCoastKiter:
I thought there is a more elegant way to a "task scheduler" rather the defining each step again and again in every function.

The Arduino is a very small device. There are several scheduling libraries. Under the hood they must do much the same stuff, just using more code.

And if they don't work, or you don't know exactly how to get them to work, you spend hours on the library problem rather than on your own problem.

If something isn't doing what you want using millis() you can see all the workings and easily alter it.

...R