What to use instead of Delay() function for Arduino sketch?

Hey guys,
I really don’t know if you have already covered this subject. I’m relatively new to Arduino world and really liked the idea of controlling the Arduino over a WiFi connection. As far as I know, using delay function in Arduino sketch will yield all kind of bugs as it blocks the communication between Blynk app and the Arduino it’s self. Yet, I need to have some sequential delay between certain actions.
I’m using a button from the Blynk app attached to D8 and another physical one attached to D3 to trigger D4, D5 & D6 with a delay of 2 sec. between D4 &D5 and 6 sec. between D5 & D6. I did try using the Blink LED without Delay sketch as a template to resolve the issue but with no success. What I’m getting is random delay timing.
Any suggestions? Your support is highly appreciated!

1 Like

Hello, you could use SimpleTimer library. Example here.

Or you could use custom code. Something like that (pseudo code, just idea) :

if (digitalRead(D3)) {
    pressMoment = millis();
if (pressMoment + DELAY_MILLIS  < millis()) {
   //do whatever you need
   pressMoment = MAX_VALUE;
1 Like

Thanks for your quick response Mr. Dmitriy! But I didn’t get the point of the pseudo code you provided. It seems to me the statement
if (pressMoment + DELAY_MILLIS < millis()) is always true!? I know I’m missing something. I’m assuming DELAY_MILLIS is the desired delaying time. But what do you mean by MAX_VALUE?
Thanks in advance!

I’ll explain with words.

When you press/click D3 button. You remember press time in variable ‘pressMoment’. Than - you have a desired delay - ‘DELAY_MILLIS’. You have a main loop that executes your code every time. So you add if statement - “does desired delay is already happen?”. If answer is - yes you go inside do some job ans set ‘pressMoment’ variable value to some huge value so this if statement will never executed again until you press D3 again.

I got it :slight_smile: Thank you very much Sr. for your quick response again!

1 Like