arduino-softtimer icon indicating copy to clipboard operation
arduino-softtimer copied to clipboard

Simple Delay

Open Eddiiie opened this issue 6 years ago • 1 comments

Hello, My code has a lot of Delay()'s in it and looking to remove them so other callbacks can fire when needed. Your library looks perfect for everything but my mental power cannot seem to figure out how to make a simple 3 second timer that does nothing. A callback is not really needed but created one that prints to the serial line, timer 1 callback fired...

For example, the Arduino needs to wait for devices to come online, 3 seconds is a good time. However, during that time the devices can be coming online and saying hello through a callback. The Delay() will miss these hello messages, correct?

I don't need/want this to fire every 3 seconds, only once. Later make it a 10ms timer, I can picture using 1 timer and changing its parameters when I need to use it.

There seems to be 2 ways to do it:

  1. Using the public variables provided by Task. - lastCallTimeMicros and periodMicros, nowMicros, and somewhere I read nowMicros set to mills() will reset it?

  2. Create 2 timers, using SoftTimer.add(task) and SoftTimer.remove(task) and a global variable that would be changed in the callback, TimerDone = true; for example, use a While loop until the global variable is set, clear it, reset timer?

I must be clearly missing something obvious here and have done too much reading. :)

Eddiiie avatar May 26 '18 22:05 Eddiiie

Sorry, I don't really understand the starting point, but the solutions you provided here both can be correct. You are free to modify the lastCallTimeMicros variable. (But use micros() instead of millis!) Also it is very encouraged to remove and add same tasks multiply times to the timer manager. You might also want to check the DelayTask, that can be a direct replacement of a delay() call.

prampec avatar May 27 '18 20:05 prampec