I am looking for a Timer library or a timer library function that duplicates the delay function without blocking the sketch. I am aware of the Pause without Blocking sketch. I have looked at Simon Monks Timer library but it seems to more allow the turning on and off of pins without blocking the sketch but does not replicate the delay function. All I want to do is add a delay to a sketch, using a library, that does not block the sketch. ie timer library function = delay() without blocking. I have also looked at Timer1 and the SimpleTimer library. Possibly / likely one of these library's has what I want but I have not identified it. Also I would like it to be able to "roll over" when the arduino timer reaches its limit without losing its settings.
Save the time 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.
What you are requesting is not logically consistent.
Delay() is a blocking delay, which means that execution stops until the delay is up. Simple - After the delay, it just picks up where it was and continues.
For a non-blocking delay, things get more complicated - because you want to delay doing some things.... but not delay doing other things. A drop-in replacement for delay() is impossible, because the code would have no way to know what to delay and what not to delay. The solution is setting global timer variables, and then checking in loop to see whether it's time to execute the "delayed" code (as is done in blinkwithoutdelay and all the other solutions).
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
boolean delay_without_delaying(unsigned long &since, unsigned long time) {
// return false if we're still "delaying", true if time ms has passed.
// this should look a lot like "blink without delay"
unsigned long currentmillis = millis();
if (currentmillis - since >= time) {
since = currentmillis;
return true;
}
return false;
}
unsigned long ledtime = 0;
unsigned long atime, btime, ctime, nltime;
void loop() {
static int ledstate = false;
if (delay_without_delaying(ledtime, 500)) {
ledstate = !ledstate;
digitalWrite(13, ledstate);
}
if (delay_without_delaying(atime, 100)) {
Serial.print("A");
}
if (delay_without_delaying(btime, 200)) {
Serial.print("B");
}
if (delay_without_delaying(ctime, 30)) {
Serial.print("C");
}
if (delay_without_delaying(nltime, 1000)) {
Serial.print("\n");
}
}
BTW, "timer library" usually means something to do stuff with the hardware timers, which are capable of causing interrupts and/or signal changes with microsecond-scale accuracy. If you're looking to do things on the same scale as typical delay() usage, it's probably not the correct search term...
Thank you for taking the time to reply to my search.
PaulS - Thank you for your concise reply.
UKHeliBob - I have configured a timer using millis which looks like it should work quite nicely. Thanks for the advice.
DrAzzy - Thanks for explaining the logic of why not a generic delay without pausing. I had not considered that each instance to pause without blocking is unique to the the situation while a delay just stops everything.
Westfw - Thank you very much for taking the time to code up an answer to me. I am still very much a learner of arduino C so will think about and play with the code you have created.
LarryD - Thanks for your reply. I learn the most by doing however I was looking for an easy to implement solution to what I find I want to do repeatedly. In retrospect I think using millis and suiting the pause to each situation is the solution however.