A StopWatch class for Arduino.

StopWatch library

One of the main applications for the Arduino board is monitoring. And monitoring means measuring. Here I introduce a simple StopWatch class to measure elapsed time.

The StopWatch class is in fact a wrapper around millis() and micros() with a few variables and an internal state. The internal state only holds running or not.

The methods of the class are:

StopWatch(res); // constructor, resolution is MICROS, MILLIS
// or SECONDS; MILLIS is the default..
void start(); // start / continue the measurement
void stop(); // stops the measurement
long reset(); // resets the measurement
// (you can't continue)
unsigned long value(); // elapsed time since start;
// depreciated in the future
unsigned long elapsed(); // elapsed time since start
bool isRunning(); // for compatibility with previous version
enum State state(); // current state of the stopwatch
enum Resolution resolution(); // chosen time-ticks.
- see .h file for all details.

To start the measurement one calls start() and internally the starttime is stored. The stopwatch is now running. At any time one can fetch the (lap)time by calling elapsed() giving the milliseconds [or micros or seconds] since start().
To stop the StopWatch one calls stop() - obvious - and now a call to value() will give the elapsed time between start() and stop(). Calling start() again and the counting will continue were it left. If one does not want that and want to start over again one needs to call reset() first.

As it is a Class one can also make arrays of stopwatches that measure time in parallel.

There are two global vars, one "standalone" StopWatch and an array of five StopWatches (numbered 0..4 of course). In setup() the serial port is initialized to high speed so the printstatements take not too long. The version number of the library is printed and the first Stopwatch [0] from the array is started to measure the duration of loop(). Note this latter is never stopped. In loop() the standalone Stopwatch is started and stopped and the value displayed to get a feeling how it is used. Also a minimal use of state() is shown

Above sketches are just dummy showcases. Far more interesting sketches can be made. e.g. the start and stop can be steered by interrupt or based upon state of digital IO lines.

Notes

The StopWatch Class is based upon millis() [ micros()] so it inherits all of its behavior including overflow after a number of days [minutes in case of MICROS]. Be aware of this if you want to use the class for measurements that take days. For more information see Millis in the reference section.

To use this library, make a folder in your SKETCHBOOKPATH\libaries with the name StopWatch and put the .h and .cpp there. Close all your IDE's and then it is ready to use.

Todo

Create a sketch with real switches (START STOP RESET) and an LCD screen

Create a sketch using ISR()'s to measure time.

Move all code to .h file for inline version
(tip coding badly)

History

2011-06-15 added state() and #defines => 0.1.02
=> to differentiate between RESET and STOPPED state