Multiple loop or different delays


Greetings, There is some way to make multiples loops? That is to say, you can execute two loops with two differents delays?


Hello, there is only one loop function allowed.

The usual way to deal with this with Arduino-style programming is to put a time variable in the loop to track when a specific operation should happen. For example:

unsigned nextOperationA;
unsigned nextOperationB;

void setup() {
  nextOperationA = millis();
  nextOperationB = millis();

void loop() {
  if(nextOperationA < millis()) {
      nextOperationA = millis() + 50; // perform this operation every 50ms
  if(nextOperationB < millis()) {
      nextOperationB = millis() + 75; // perform this operation every 75ms

Hope this makes sense. I’d recommend checking out some Arduino tutorials online for more information.


Nice! I’m using this function and works perfect. But Is it a consumption problem? Thanks Reuben


What do you mean by consumption problem?


Oh, as far as power usage? Yes, that could use a little more power though the delay function doesn’t really save much power as compared with the different sleep functions. You’ll have to be creative about which intervals you wake up at and things like that.


I understand then, that if I play with the functions of energy I can get better use of energy?


Yes, take a look at the docs here:

You’ll have to do some experimenting.


I suggest you consider the following to ‘mimic’ multi-tasking in Arduino:

  1. Use the Arduino clock (millis). This is a good starting point.
  2. Use arduino interrupt if your usecase and external hardware/sensors is capable of doing so.