Skip to main content

Reply to "Arduino Lesson: Millisecond Timing"

Now it's time for us to create a Class Library to handle a generalized millisecond timer that we shall name MsTimer. If you need to review the makings of a Class Library, you can find the lesson on how to create an Arduino Library here.

Here's the main body of the class header file (MsTimer.h):

The ULONG data type is just a shorthand for unsigned long. It is defined in a file that I use often called Types.h (included in the attachments).

The private data members include the _startTime and the _interval variables. There are 2 constructors. The rest are operations to set/get the time interval, start the timer, and determine if the timer is running or has completed the elapsed time.

In the class implementation file (MsTimer.cpp) here is one version of the "start" function:

And here is the test to determine if the timer is done:

Note that when the timer completes, the _startTime variable is reset to 0. Subsequent calls to "isDone" will check this and return true; the timer is done. But if the time interval has not yet been completed, the function returns false.

The files are all provided in the attachments along with a test program to demonstrate its usage. It's all quite simple and direct. Good luck!

  -- Leo

Attachments

OGR Publishing, Inc., 1310 Eastside Centre Ct, Suite 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
×