Skip to content

Commit c80ee6e

Browse files
Merge pull request #6 from TADT1909/master
Format code with Webkit coding-style & Support older version of Arduino IDE & Improve elapsed() function
2 parents 43466e1 + 29af7d3 commit c80ee6e

File tree

10 files changed

+179
-166
lines changed

10 files changed

+179
-166
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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

Test_6_delays/Test_6_delays.ino

+34-33
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Arduino.h>
23
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
44

5-
void setup()
6-
{ Serial.begin(9600);
7-
}
5+
void setup() { Serial.begin(9600); }
86

9-
void loop()
10-
{ static VirtualDelay delay1, delay2, delay3, delay4, delay5, delay6;
11-
DO_ONCE
12-
( Serial << "\nDO_ONCE 1";
13-
delay1.start(200); // start sequence delay1 delay2 delay3
14-
delay4.start(550); // start one-shot delay4
15-
delay5.start(1250); // start one-shot delay5
16-
)
17-
if(delay4.elapsed()) Serial << "\nONE-SHOT 550ms " << millis();
18-
if(delay5.elapsed()) Serial << "\nONE-SHOT 1250ms " << millis();
7+
void loop()
8+
{
9+
static VirtualDelay delay1, delay2, delay3, delay4, delay5, delay6;
10+
DO_ONCE(Serial << "\nDO_ONCE 1";
11+
delay1.start(200); // start sequence delay1 delay2 delay3
12+
delay4.start(550); // start one-shot delay4
13+
delay5.start(1250); // start one-shot delay5
14+
)
15+
if (delay4.elapsed())
16+
Serial << "\nONE-SHOT 550ms " << millis();
17+
if (delay5.elapsed())
18+
Serial << "\nONE-SHOT 1250ms " << millis();
1919

20-
if(millis()>2250) DO_ONCE(Serial << "\nDO_ONCE 2 2250ms " << millis()) // test a second DO_ONCE
21-
22-
delay6.start(750);
23-
if(delay6.elapsed()) Serial << "\n Repeat delay6 750ms " << millis();
24-
25-
if(delay1.elapsed()) // sequence with deadlock
26-
{ Serial << "\nsequence delay1 200ms " << millis();
27-
delay2.start(100);
28-
}
29-
if(delay2.elapsed())
30-
{ Serial << "\nsequence delay2 100ms " << millis();
31-
delay3.start(400);
32-
}
33-
if(delay3.elapsed())
34-
{ Serial << "\nsequence delay3 400ms " << millis();
35-
delay1.start(200);
36-
}
37-
}
20+
if (millis() > 2250)
21+
DO_ONCE(Serial << "\nDO_ONCE 2 2250ms "
22+
<< millis()) // test a second DO_ONCE
3823

24+
delay6.start(750);
25+
if (delay6.elapsed())
26+
Serial << "\n Repeat delay6 750ms " << millis();
3927

40-
28+
if (delay1.elapsed()) // sequence with deadlock
29+
{
30+
Serial << "\nsequence delay1 200ms " << millis();
31+
delay2.start(100);
32+
}
33+
if (delay2.elapsed()) {
34+
Serial << "\nsequence delay2 100ms " << millis();
35+
delay3.start(400);
36+
}
37+
if (delay3.elapsed()) {
38+
Serial << "\nsequence delay3 400ms " << millis();
39+
delay1.start(200);
40+
}
41+
}

Test_micros/Test_micros.ino

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
VirtualDelay delay_us(micros);
44

5-
void setup()
6-
{ Serial.begin(9600);
7-
}
5+
void setup() { Serial.begin(9600); }
86

9-
void loop()
10-
{ DO_ONCE ( delay_us.start(1000000)) // 1s
11-
if(delay_us.elapsed()) Serial.print(micros());
7+
void loop()
8+
{
9+
DO_ONCE(delay_us.start(1000000)) // 1s
10+
if (delay_us.elapsed())
11+
Serial.print(micros());
1212
}
13-
14-
15-
16-

Test_rollover/Test_rollover.ino

+28-30
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1+
#include "avdweb_VirtualDelay.h"
2+
#include <Albert.h>
13
#include <Arduino.h>
24
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
45
#include <util/atomic.h>
5-
#include <Albert.h>
66

77
VirtualDelay delay_ms;
88
VirtualDelay delay_us(micros);
9-
unsigned long t0_ms, t0_us;
9+
unsigned long t0_ms, t0_us;
1010

11-
long ms=1000;
12-
long us=2000000;
11+
long ms = 1000;
12+
long us = 2000000;
1313

1414
void setMillis(unsigned long ms)
15-
{ extern unsigned long timer0_millis;
16-
ATOMIC_BLOCK (ATOMIC_RESTORESTATE)
17-
{ timer0_millis = ms;
18-
}
15+
{
16+
extern unsigned long timer0_millis;
17+
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer0_millis = ms; }
1918
}
2019

21-
void setup()
22-
{ Serial.begin(9600);
20+
void setup()
21+
{
22+
Serial.begin(9600);
2323

24-
setMillis(-ms); // rollover bug values: -x ... -1
25-
//setMillis(-1); // rollover bug values: -x ... -1
26-
27-
t0_ms=millis();
28-
t0_us=micros();
29-
Serial << "\nTest VirtualDelay ms " << ms << "ms";
30-
Serial << "\nTest VirtualDelay us " << us << "us";
31-
Serial << "\nChanged t0_ms=" << t0_ms << "ms";
32-
}
24+
setMillis(-ms); // rollover bug values: -x ... -1
25+
// setMillis(-1); // rollover bug values: -x ... -1
3326

34-
void loop()
35-
{ DO_ONCE // do only one time in the loop
36-
( delay_ms.start(ms);
37-
delay_us.start(us);
38-
)
39-
if(delay_ms.elapsed()) Serial << "\n" << millis()-t0_ms << "ms"; // result is ~ 1021ms
40-
if(delay_us.elapsed()) Serial << "\n" << micros()-t0_us << "us"; // result is ~ 2021884us
27+
t0_ms = millis();
28+
t0_us = micros();
29+
Serial << "\nTest VirtualDelay ms " << ms << "ms";
30+
Serial << "\nTest VirtualDelay us " << us << "us";
31+
Serial << "\nChanged t0_ms=" << t0_ms << "ms";
4132
}
4233

43-
44-
45-
34+
void loop()
35+
{
36+
DO_ONCE // do only one time in the loop
37+
(delay_ms.start(ms); delay_us.start(us);) if (delay_ms.elapsed()) Serial
38+
<< "\n"
39+
<< millis() - t0_ms << "ms"; // result is ~ 1021ms
40+
if (delay_us.elapsed())
41+
Serial << "\n"
42+
<< micros() - t0_us << "us"; // result is ~ 2021884us
43+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#include <Arduino.h>
21
#include "avdweb_VirtualDelay.h"
2+
#include <Arduino.h>
33

44
const byte ledPin = 13;
55
bool b;
66
VirtualDelay singleDelay; // default = millis
77

8-
void setup()
9-
{ pinMode(ledPin, OUTPUT);
10-
}
8+
void setup() { pinMode(ledPin, OUTPUT); }
119

12-
void loop()
13-
{ singleDelay.start(400); // calls while running are ignored
14-
if(singleDelay.elapsed()) digitalWrite(ledPin, b=!b); // blink the onboard LED 400ms, 400ms off
10+
void loop()
11+
{
12+
singleDelay.start(400); // calls while running are ignored
13+
if (singleDelay.elapsed())
14+
digitalWrite(ledPin, b = !b); // blink the onboard LED 400ms, 400ms off
1515
}
16-

avdweb_VirtualDelay.cpp

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*
22
Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl
3-
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
4-
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5-
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
6-
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses .
3+
This program is free software: you can redistribute it and/or modify it under
4+
the terms of the GNU General Public License as published by the Free Software
5+
Foundation, either version 3 of the License, or (at your option) any later
6+
version. This program is distributed in the hope that it will be useful, but
7+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at
9+
http://www.gnu.org/licenses .
710
811
AUTHOR: Albert van Dalen
912
WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html
@@ -20,40 +23,38 @@ running _____| |__________
2023
2124
elapsed() ________________|__________
2225
23-
set timeOut _____|_____________________
26+
set timeOut _____|_____________________
2427
2528
*/
2629

27-
#include <Arduino.h>
2830
#include "avdweb_VirtualDelay.h"
2931

30-
VirtualDelay::VirtualDelay(unsigned long (*timerFunctionPtr)()):
31-
timerFunctionPtr(timerFunctionPtr)
32-
{
32+
VirtualDelay::VirtualDelay(
33+
unsigned long (*timerFunctionPtr)())
34+
: timerFunctionPtr(timerFunctionPtr)
35+
{
3336
}
3437

3538
void VirtualDelay::start(signed long delay) // 0...2^31
36-
{ if(!running)
37-
{ running = 1;
38-
timeOut = (*timerFunctionPtr)() + abs(delay);
39-
}
39+
{
40+
if (!running) {
41+
running = 1;
42+
timeOut = (*timerFunctionPtr)() + abs(delay);
43+
}
4044
}
4145

42-
bool VirtualDelay::elapsed()
43-
{ bool pulse = 0;
44-
if(running)
45-
{ //if((signed long)(*timerFunctionPtr)() >= timeOut) // bug, not equal to:
46-
if((signed long)((*timerFunctionPtr)() - timeOut) >= 0) // fix rollover bug: https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
47-
{ running = 0;
48-
pulse = 1; // return 1 just one time
49-
}
50-
}
51-
return pulse;
52-
}
53-
54-
55-
56-
57-
58-
59-
46+
bool VirtualDelay::elapsed()
47+
{
48+
if (running) {
49+
// bug
50+
// if((signed long)(*timerFunctionPtr)() >= timeOut)
51+
// not the same as
52+
if ((signed long)((*timerFunctionPtr)() - timeOut) >= 0) {
53+
// fix rollover bug:
54+
// https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
55+
running = 0;
56+
return 1; // timer is elapsed
57+
}
58+
}
59+
return 0; // still in delay timer
60+
}

avdweb_VirtualDelay.h

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
/*
22
Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl
3-
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
4-
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5-
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
6-
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses .
3+
This program is free software: you can redistribute it and/or modify it under
4+
the terms of the GNU General Public License as published by the Free Software
5+
Foundation, either version 3 of the License, or (at your option) any later
6+
version. This program is distributed in the hope that it will be useful, but
7+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at
9+
http://www.gnu.org/licenses .
710
811
AUTHOR: Albert van Dalen
912
WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html
@@ -12,17 +15,30 @@ WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html
1215
#ifndef VirtualDelay_H
1316
#define VirtualDelay_H
1417

15-
class VirtualDelay
16-
{
18+
#if ARDUINO >= 100
19+
#include <Arduino.h>
20+
#else
21+
#include <WProgram.h>
22+
#include <wiring.h>
23+
#endif
24+
25+
class VirtualDelay {
1726
public:
18-
VirtualDelay(unsigned long (*timerFunctionPtr)() = millis);
19-
void start(signed long delay);
20-
bool elapsed();
21-
22-
bool running=0;
23-
unsigned long timeOut, (*timerFunctionPtr)();
27+
VirtualDelay(unsigned long (*timerFunctionPtr)() = millis);
28+
void start(signed long delay);
29+
bool elapsed();
30+
31+
bool running = 0;
32+
unsigned long timeOut, (*timerFunctionPtr)();
2433
};
2534

26-
#define DO_ONCE(x) {static bool b; if(!b) {b=1; x;}}
35+
#define DO_ONCE(x) \
36+
{ \
37+
static bool b; \
38+
if (!b) { \
39+
b = 1; \
40+
x; \
41+
} \
42+
}
2743

28-
#endif
44+
#endif

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ avdweb_VirtualDelay KEYWORD1
1414

1515
start KEYWORD2
1616
elapsed KEYWORD2
17+
DO_ONCE KEYWORD2
1718

1819
#######################################
1920
# Instances (KEYWORD2)

testOneShot/testOneShot.ino

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
#include "avdweb_VirtualDelay.h"
12
#include <Arduino.h>
23
#include <Streaming.h>
3-
#include "avdweb_VirtualDelay.h"
44

55
VirtualDelay delay1;
66

7-
void setup()
8-
{ Serial.begin(9600);
9-
Serial << "\ntestOneShot 2s\n";
7+
void setup()
8+
{
9+
Serial.begin(9600);
10+
Serial << "\ntestOneShot 2s\n";
1011
}
1112

12-
void loop()
13-
{ DO_ONCE(delay1.start(2000)) // do only one time in the loop
14-
if(delay1.elapsed()) Serial << millis() << "ms" ;
13+
void loop()
14+
{
15+
DO_ONCE(delay1.start(2000)) // do only one time in the loop
16+
if (delay1.elapsed())
17+
Serial << millis() << "ms";
1518
}
16-
17-
18-

0 commit comments

Comments
 (0)