File tree Expand file tree Collapse file tree 4 files changed +26
-27
lines changed
Expand file tree Collapse file tree 4 files changed +26
-27
lines changed Original file line number Diff line number Diff line change 1- ### Advantages of the VirtualDelay library:
2- - The delay is virtual, during the delay, the code execution is continued
3- - We can use multiple delays sequentially in a loop.
4- - We can use multiple delays simultaneously and independent of each other.
5- - The delay time can set in micro-seconds or milli-seconds.
6- - No hardware timers are used
7- - The library is timer-rollover safe
8-
9- See the article http://www.avdweb.nl/arduino/libraries/virtualdelay.html
1+ ### Advantages of the VirtualDelay library:
2+ - The delay is virtual, during the delay, the code execution is continued
3+ - We can use multiple delays sequentially in a loop.
4+ - We can use multiple delays simultaneously and independent of each other.
5+ - The delay time can set in micro-seconds or milli-seconds.
6+ - No hardware timers are used
7+ - The library is timer-rollover safe
8+
9+ See the article http://www.avdweb.nl/arduino/libraries/virtualdelay.html
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ set timeOut _____|_____________________
3232VirtualDelay::VirtualDelay (unsigned long (*timerFunctionPtr)())
3333 : timerFunctionPtr(timerFunctionPtr) {}
3434
35- void VirtualDelay::start (signed long delay) // 0...2^31
35+ void VirtualDelay::start (signed long delay) // 0...2^31
3636{
3737 if (!running) {
3838 running = 1 ;
@@ -41,15 +41,16 @@ void VirtualDelay::start(signed long delay) // 0...2^31
4141}
4242
4343bool VirtualDelay::elapsed () {
44- if (running) { // if((signed long)(*timerFunctionPtr)() >= timeOut) // bug,
45- // not equal to:
46- if ((signed long )((*timerFunctionPtr)() - timeOut) >=
47- 0 ) // fix rollover bug:
48- // https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
49- {
44+ if (running) {
45+ // bug
46+ // if((signed long)(*timerFunctionPtr)() >= timeOut)
47+ // not the same as
48+ if ((signed long )((*timerFunctionPtr)() - timeOut) >= 0 ) {
49+ // fix rollover bug:
50+ // https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
5051 running = 0 ;
51- return 1 ; // pulse = 1
52+ return 1 ; // timer is elapsed
5253 }
5354 }
54- return 0 ; // pulse = 0
55+ return 0 ; // still in delay timer
5556}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ avdweb_VirtualDelay KEYWORD1
1414
1515start KEYWORD2
1616elapsed KEYWORD2
17+ DO_ONCE KEYWORD2
1718
1819#######################################
1920# Instances (KEYWORD2)
Original file line number Diff line number Diff line change 44
55VirtualDelay delay1;
66
7- void setup ()
8- { Serial.begin (9600 );
9- Serial << " \n testOneShot 2s\n " ;
7+ void setup () {
8+ Serial.begin (9600 );
9+ Serial << " \n testOneShot 2s\n " ;
1010}
1111
12- void loop ()
13- { DO_ONCE (delay1.start (2000 )) // do only one time in the loop
14- if (delay1.elapsed ()) Serial << millis () << " ms" ;
12+ void loop () {
13+ DO_ONCE (delay1.start (2000 )) // do only one time in the loop
14+ if (delay1.elapsed ()) Serial << millis () << " ms" ;
1515}
16-
17-
18-
You can’t perform that action at this time.
0 commit comments