Multiple loop or different delays


#1

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


#2

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()) {
      doOperationA();
      nextOperationA = millis() + 50; // perform this operation every 50ms
   }
  if(nextOperationB < millis()) {
      doOperationB();
      nextOperationB = millis() + 75; // perform this operation every 75ms
   }
delay(5);
}

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


#3

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


#4

What do you mean by consumption problem?


#5

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.


#6

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


#7

Yes, take a look at the docs here: https://hologram.io/docs/reference/dash/api/#power-management

You’ll have to do some experimenting.


#8

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.