Multitask using millis(), interrupts, timealarm() in combination with for() loops

Hi, i have a for() loop within the general arduino void_loop in which I need to control the frequency of each particular passage through it. On the other hand I would like Arduino to perform, in the time between passages of the for() loops, another task given that a certain amount of time passes since the start of the general void_loop. So far i wasn´t able to run these multitask processes including millis(), interrupts or using the timealarm library in either sub-loop. Irrespective of any of these strategies what happens is that once the process enters the for() loop it does not performs acknowledging the conditional multitask under, for example, the millis function.
Is there any way to control the time of each passage through a for() loop without using the delay function, and at the same time allowing multitasking given by a conditional millis(), etc ?? which would be the structure for this code ??..

Since the function named loop() loops very rapidly, there may be no reason to use a for loop within it.

In that context, you can solve the timing problem for one action using millis(). Whether that is possible for more than one action depends on your requirements.

1 Like

The key is notnto trap ececution in a long for loop. Break it up and let the loop function do the looping.

normally used to apply identical action on some amount of elements, one element may be skipped with continue;, escape out whole for() loop with break; or when changing value is not in range:

for(int i = 0; i < 100; i++){
...
  if(someCondition) i=100;
}

so, if for() not pass to your needs then don't use it.
make flow chart diagram first. then coding is straightforward

Get rid of the for loops unless they are for processing an array.

1 Like

Check ANTIRTOS library

Yes, probably, it's hard to be sure without knowing more about your timing requirements or seeing your code.

Again, it's difficult to talk about a suitable code structure without knowing more.

As others have suggested a for loop does not sound like the tool for this job.

Please give some examples of what you are trying to do.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

Since you didn't post example code to play with, consider the difference between running this program with HOBBLE=true and HOBBLE=false

bool HOBBLE = true;
int NUM = 20;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  if (HOBBLE) {
    for (int ii = 0; ii < NUM ; ++ii) {
      Serial.print(ii);
      Serial.print(" ");
      delay(100);
    }
    Serial.println();
  }
  else // un-HOBBLED
  {
    replacement();
  }
  Serial.print("."); // or other things
}

void replacement() {
  static int ii;
  uint32_t now = millis();
  const uint32_t interval = 100;
  static uint32_t last = 0; // remember for next time
  if (now - last >= interval) {
    last = now;
    Serial.print(ii);
    Serial.print(" ");
    ii += 1;
    if (ii >= NUM) {
      Serial.println();
      ii = 0;
    }
  }
}

and consider these:

1 Like

HOBBLE :sob:

1 Like

Aw, I thought HOBBLE was nicer than using KNEECAP, but both suck.

Also, I had to go back and edit in a extra task to do in loop().

In this "timing" simulation, one LED cycles through colors at one interval per loop() while a second LED changes on/off states at an asymmetric, programmed, rate (50 on, 200 off, 50 on, 750 off).

This "timing" simulation shows each LED with their symmetric state change...

Hello ureta_a

Welcome to the best Arduino forum ever :slight_smile:

Take a view to get some ideas:

hth

1 Like

check chained functions approach: ANTIRTOS: chained functions - Wokwi ESP32, STM32, Arduino Simulator