help with the millis function

Good night.

I wrote a code with the following characteristics:

  • if condition 1 is satisfied
  • counts a preset time interval 1
  • if condition 2 is satisfied
  • counts a preset time interval 2
  • if condition 3 is satisfied
  • counts a preset time interval 3 and executes a routine

Note: the time until the execution of this routine, if it satisfies the above 3 conditions, must be the sum of time 1 + time 2 + time 3.

void loop() {


if (condition1){

unsigned long previous_millis = millis();
while(millis() - previous_millis <= interval1);


if (condition2){


unsigned long previous_millis = millis();
while(millis() - previous_millis <= interval2);

if (condition3){


unsigned long previous_millis = millis();
while(millis() - previous_millis <= interval3);


if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("tempo: ");
unsigned long tempo = millis();
lcd.println(tempo);
digitalWrite(saida_1, ledState);

}

}

}
}

For the case of interval1 = interval2 = interval3 = 2000, the delay interval was 6000 (it’s right).

I’m using the millis () function along with while cycles. Does this lock the processor?

I wish it would not crash that way. I wanted it to be like the blink without delay example.

Thanks any help.

The way you are using WHILE with millis() gives the exact same behaviour as using the delay() function. Is that what you want?

It would be easier to understand the problem if you explain what the different conditions are.

If you want non-blocking timing then have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

And if you use the AutoFormat tool to indent your code it will make it much easier to read.

...R