A LITTLE HELP FOR A FUNCTION !!!

Hello.Ι thought the below function ,so I have 1 question and I want to help me,please…
Does the below function works like a delay or like millis() {I ask because I confused because of the do while condition}
To wit,that function delay all the program or only one part of it???

void callMillis(unsigned long x)
{
unsigned long presentMILLIS = millis();
do
{

}
while (millis() - presentMILLIS <= x);
}

For example:
void loop()
{
digitalWrite(led,HIGH);
callMillis(1000);
digitalWrite(led,LOW);
callMillis(1000);
}

I hope it works like millis() …

Thanks.

Do while means do this and nothing else until the condition changes. Does that help?

Paul

what do you mean with

I hope it works like millis() .....

it uses millis() to create a delay. During that delay, nothing happens, your arduino is stuck in the do {} while () loop

so if you hope this will magically work to do things like blink without delay() then you'll be disappointed...

Yes ,but if in loop-function for example call AND another function (that blink a led every 0.1 sec. with millis()) does the function that blink the led affects from the other one(by calling callMillis) ?

Am I clear?

Is there a chance to use that function(callMillis()) and not delay ALL THE PROGRAM ??
Can anyone help?

void callMillis(unsigned long x)
{
unsigned long presentMILLIS = millis();
while (millis() - presentMILLIS <= x);
}

Does the above function works like millis() {and NOT delay } ??

Reinventing delay does not make it better/less blocking.

Learn to use millis/micros or use delay and an operating system on a bigger platform like an ESP32..

Your busy waiting blocking loop will be worse on systems like the ESP32 where delay merely lets the task sleep.

arduiNICK:
Yes ,but if in loop-function for example call AND another function (that blink a led every 0.1 sec. with millis()) does the function that blink the led affects from the other one(by calling callMillis) ?

Am I clear?

You are not on a multi-processor / multi-core architecture so when the processor is busy doing something, other parts of your code are not being executed... so how would you have a function that blinks the led and callMillis() executing at the same time.

as Whandall say, understand how to do those things in the right way.there is a tutorial at the top of the forum called Using millis() for timing. A beginners guide

My problem is:
I want to delay(with millis) only one part of the program for only one time and then to continue below, but I did many attempts and nothing.Can you help about it?

for example:

void some()
{
//I do something here…
Here I want to delay this function for one time (like delay(200))
//continue to do something else…
}

void loop()
{
some();
}

But when I do it ,millis() doesn’t work and the program starts from the beginning…

if you want to delay (ie a blocking wait, nothing else happens in your code) then use delay().. don't reinvent the wheel...

if you want to do something once at start, put it in the setup()

if you need to have that in the loop, you need a variable that will remember this has been executed so that you don't do it again

void loop()
{
  static bool firstTime = true;
  // this block with get executed only once
  if (firstTime) {
    delay(2000);
    firstTime = false;
  } 

...

}

Yes ,but I want to do it for one time without delay ,because at the same time I want to run and another function
and delay will stop anything ...
Have you understand my problem?

arduiNICK:
Have you understand my problem?

No.

if you want to do multiple things relatively in parallel (ie user perception) then you need to read the tutorial about millis().

You don’t want to accept the obvious solution.

Besides learning how to use millis and if instead of while, you should have a look at state machines.
Or look at the “doing several things at a time” tutorial.

try something like this (typed here, not even sure it compiles)

void doThisForAWhile(unsigned long howLong)
{
  static bool doOnce = true;
  static bool firstCall = true;
  static unsigned long startChrono;

  if (doOnce) {
    if (firstCall) {
      startChrono = millis();
      firstCall = false;
    }

    if (millis() - startChrono <= howLong) {
      Serial.println(millis());
    } else {
      doOnce = false;
    }
  }
}

void doSomethingElseAlways()
{
  static unsigned long startChrono = millis();
  if (millis() - startChrono <= 1000) {
    startChrono += 1000;
    Serial.println(F("next second"));
  }
}


void setup()
{
  Serial.begin(115200);
  Serial.println(F("STARTING IN 3s"));
  delay(3000);
}

void loop()
{
  doThisForAWhile(2000ul);
  doSomethingElseAlways();
}

if I did not mess it up, the program will wait 3 seconds in the setup, then you’ll see millis() being blasted to the console very quickly with every second the message “next second” being inserted and after 2 seconds then only the “next second” should be displayed

Thank you.I wiil see it...