Examples - Electronic Bricks - Electronic Brick Alternative Delay

Alternative Delay - Keeps away the "busy waiting"!

That program is about, how to program and set up a alternative delay function that allows executing (functions or other tasks) while waiting for the delay to stop.



/*
Delay Example without delay function / and busy waiting
Example: Alternative delay() function without busy waiting
*/

// Init global variables
long prevMillis = 0;                  // milliseconds counter
int interval = 1000;                  // delay interval


void setup()
{
        Serial.begin(9600);
}


void loop()
{

        // Check if it is time for loop2 execution
        if(millis() >= (prevMillis+interval) )
        {
                prevMillis =  millis();
                loop2();
        }
}


void loop2()
{
        Serial.print("Loop 2 exec: ");
        Serial.println(millis());
}