One thing that seems to trip up beginners to the Arduino is the idea of implementing delays without using delay(). Hence the "blink without delay" example sketch. But that sketch is a bit confusing. You have to set up a "start" variable, assign the time to it, check it by doing subtraction rather than addition, and so on. And it gets more confusing if you want two time intervals.
The "elapsed" class below tries to simplify that. The class has a couple of member variables (startus and startms) which remember when you last "reset the timer". Then you simply call intervalMs (for milliseconds) or intervalUs (for microseconds) to see if your required time period is up. When it is, you can do something, like flash a LED. Then you can reset the timer if you want to.
Example snippet:
The Arduino library can be downloaded from here:
http://www.gammon.com.au/Arduino/Elapsed.zip (4 Kb)
Unzip the file into your Arduino sketch "libraries" folder.
To use it just:
Example code that flashes two LEDs at different rates:
The example code flashes two LEDs at different intervals. The variables t2 (for pin 2) and t3 (for pin 3) are used to see if the time is up to toggle the LED state.
Note that the millisecond interval wraps around after about 50 days, and the microsecond interval wraps around after about 70 minutes. The wrap-around itself is handled correctly, but if you need to time an interval longer than 70 minutes you would need to check intervalMs rather than intervalUs.
As with millis() and micros() if you turn off interrupts the times may be out.
Warning: If you try to move the variables inside a function you will probably need to make them static (or they will reset each time the function is entered), like this:
The "elapsed" class below tries to simplify that. The class has a couple of member variables (startus and startms) which remember when you last "reset the timer". Then you simply call intervalMs (for milliseconds) or intervalUs (for microseconds) to see if your required time period is up. When it is, you can do something, like flash a LED. Then you can reset the timer if you want to.
Example snippet:
if (t2.intervalMs () > 200) // time up?
{
digitalWrite(2, !digitalRead (2)); // toggle the LED
t2.reset (); // reset the timer
} // end if time up
The Arduino library can be downloaded from here:
http://www.gammon.com.au/Arduino/Elapsed.zip (4 Kb)
Unzip the file into your Arduino sketch "libraries" folder.
To use it just:
#include <Elapsed.h>
Example code that flashes two LEDs at different rates:
#include <Elapsed.h>
void setup()
{
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
} // end of setup
static Elapsed t2, t3;
void loop() {
if (t2.intervalMs () > 200)
{
digitalWrite(2, !digitalRead (2));
t2.reset ();
}
if (t3.intervalMs () > 500)
{
digitalWrite(3, !digitalRead (3));
t3.reset ();
}
} // end of loop
The example code flashes two LEDs at different intervals. The variables t2 (for pin 2) and t3 (for pin 3) are used to see if the time is up to toggle the LED state.
Note that the millisecond interval wraps around after about 50 days, and the microsecond interval wraps around after about 70 minutes. The wrap-around itself is handled correctly, but if you need to time an interval longer than 70 minutes you would need to check intervalMs rather than intervalUs.
As with millis() and micros() if you turn off interrupts the times may be out.
Warning: If you try to move the variables inside a function you will probably need to make them static (or they will reset each time the function is entered), like this:
void loop() {
static Elapsed t2, t3; // must have static here
// ...
}