Replace delay function

Hello folks,

I have read that the delay functions work in the way that it stop all the reading(execution) of the code during the delay time .

I would like to know if there is another function doing the same as delay() but still execute the rest of the code or a way to isolate the delay() in the part of the program ?

I don't know if that is clear....

You have to use millis().

Take a look at the blink without delay example in the IDE.

Save the time from millis() that an event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.

Flammeur:
I would like to know if there is another function doing the same as delay() but still execute the rest of the code or a way to isolate the delay() in the part of the program ?

If you were posting in an automobile forum, your question would probably read like that:

"I would like to know if there is another pedal in my car doing the same as the brake pedal, but still I want the car going at full speed without slowing down?"

:roll_eyes:

I'd say:

If you don't want to slow down your car, don't step on the brake pedal!

If you don't want to slow down your Arduino, don't use the delay() function!

Thanks for the answers

Sorry UKHeliBob i didn't everything you said , So if i get it right if i just need to replace delay() by millis()?

Edit:ahah make sense jurs but i didn't get my arduino license yet so i need some help :wink:

Flammeur:
So if i get it right if i just need to replace delay() by millis()?

If you just replace delay() by millis() to avoid your Arduino slowing down is the same as replacing a step on the brake pedal of your car by a step on the foot of the co-driver to avoid slowing down the car.

What you have to replace is "the programming logic of your sketch" and not a single function call.

Did you already have a look at the example sketch you can load from the IDE menu:
File - Examples - 02.Digital - BlinkWithoutDelay

If not - please do so!

Okay gonna check that thanks !

unsigned long startTime = 0;
const long interval = 1000;  // 1 second = 1000 millis
bool timeFlag = false;

void setup()
{
}

void loop()
{
  if (something you want to Time)
  {
    timeFlag = true;
    startTime = millis(); // Begin timing sequence
    // do stuff here when timing sequence starts
  }

  unsigned long currentTime = millis();
  if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
  {
    timeFlag = false;
    // do stuff here when time is up
  }
}

Thanks for the code last questions

So now i have one part of the code using milli() but i have another section in the programs using the setTime(0) i would like to know if this variable will affect the part with milli() ?

What is setTime(0) ?

I use a light sensor, if the light for 10s >500 the light turn on and then I use the setTime(0) to restart the counter I think it's depending of the time.h library not sure.

That why I would like to know if it restart all the counter or if the milli() work independently

http://www.gammon.com.au/blink

Flammeur:
I use a light sensor, if the light for 10s >500 the light turn on and then I use the setTime(0) to restart the counter I think it's depending of the time.h library not sure.

That why I would like to know if it restart all the counter or if the milli() work independently

you don't need to restart a counter. Just take a sample of millis() when the light goes > 500, and use that number as your starting point to count 10 seconds also using millis().

Thanks for the answer I was asking this questions because I didn't want to change my code again ...(laziness) If that is the only options I will .

laziness

I'm lazy too. Probably lazier than you. :slight_smile:

However if the lazy way doesn't work, and the other way does, well ...

you can try Ufficial Scheduler the tutorial.
For now not work with Mega