Alternative to delay() function Library

Hi guys! I know few of us have been using the delay() function and encountered some problems especially when using Interrupts.

I could not say that I’m really good at Arduino. But from my experience, using the delay() function while expecting for an input from a sensor or interrupt buttons should be avoided.

For my codes, i have been using this Library that I altered from the Blinking-without-delay library that utilizes the millis() function.

If you wanted to try this out. Just create a ‘TimeInterval’ folder in your Libraries and copy the TimeInterval.cpp, TimeInterval.h and keywords.txt files.

I hope this may help some people out there! Expecting for a good feedback. Thanks

Sample_TimeInterval.ino (843 Bytes)

TimeInterval.cpp (444 Bytes)

TimeInterval.h (399 Bytes)

keywords.txt (40 Bytes)

Your programs are short so it is much easier for everyone if you include them in your Post like this

Sample_TimeInterval.ino

/*Alternative to delay() function Library

 by Kriz Jade C. Garino, ECE
    MSU-GSC Graduate 2018
    
*/

#include <TimeInterval.h>

//Create your delay function
TimeInterval myDelay;

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

void setup() {
   // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  // if the LED is off turn it on and vice-versa:
  
    if (ledState == LOW) {
      ledState = HIGH;} 
    else {
      ledState = LOW;}

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);

    //set the desired delay in millis
    myDelay.duration(1000);
}
/*
	TimeInterval.h - Library replacement for delay function
	Created by Kriz Jade C. Garino, September 9, 2017.
*/

#ifndef TimeInterval_h
#define TimeInterval_h
#include "Arduino.h"

class TimeInterval{
	public:
		TimeInterval();
		void duration(unsigned long interval);
	private:
		unsigned long _prevMillis;
		unsigned long _currentMillis;
		unsigned long _interval;
};

#endif
/*
	TimeInterval.cpp - Library replacement for delay function
	Created by Kriz Jade C. Garino, September 9, 2017.
*/

#include "Arduino.h"
#include "TimeInterval.h"
TimeInterval::TimeInterval(){
	_currentMillis=0;
	_prevMillis=0;
}
	
void TimeInterval::duration(unsigned long interval){

	_interval=interval;
	while(_currentMillis - _prevMillis<_interval){ 
		_currentMillis = millis();
 	}
	_prevMillis = _currentMillis;
}

…R

This line

while(_currentMillis - _prevMillis<_interval){

gives me the strong impression that your code also blocks the Arduino just the same as the delay() function. And using the delay() function is much simpler.

This subject was discussed extensively in this recent Thread.

…R

spykidx:
You could try it with a push button that turns on an LED upon interruption. Use a long duration of delay. Let say delay(10000) and compare it with TimeInterval.duration(10000). This will show the difference between the two.

I tried your code with an interval of 5000 and using digitalRead() for a switch on pin 8 and with a red led on pin 12. When i closed the switch the red LED did not update until the 5000 millisecs had expired.

That behaviour is exactly the same as delay().

If you have a demo program that shows otherwise please post it.

…R

spykidx:
Let me say this again. You should use this if you are expecting for an "Interrupt", so you have to attach an interrupt button to see the result.

It is very rare that it is necessary to use an interrupt to detect a button press if the rest of the program is properly written.

And if you are using an interrupt it will work as well with delay() as with your code.

I must recommend to other readers that the code in this Thread does not provide an "Alternative to delay() function Library" .

I am tempted to ask the Moderator to move this Thread out of the Exhibition / Gallery section.

...R

Fyi, the lib largely resembles FireTimer.h which is both installable via the Arduino IDE’s Libraries Manager and handles millis/micros overflow.

Power_Broker:
Fyi, the lib largely resembles FireTimer.h which is both installable via the Arduino IDE’s Libraries Manager and handles millis/micros overflow.

Does the FireTimer library do non-blocking timing?

…R

Robin2:
Does the FireTimer library do non-blocking timing?

...R

Power_Broker:
...and handles millis/micros overflow.

You make it seem as handling the overflow is something good, but it is not.

The Blink Without Delay works perfect, even during a rollover: https://www.arduino.cc/en/tutorial/BlinkWithoutDelay.
The rule of thumb is not to try to "handle" the rollover, because that does not make things better.