Sometimes classes are only introduced to make the code more readable. The TimeDelta
class is one of these. It actually just provides a convenience interface to get the elapsed time since a start point.
class TimeDelta { public: /// Create a new time delta. /// TimeDelta() : _start(0) { } /// dtor /// ~TimeDelta() { } /// Start the time delta with the given time. /// void start(const unsigned long time) { _start = time; } /// Check how much time elapsed since the start. /// unsigned long elapsed(const unsigned long time) const { return time - _start; } private: unsigned long _start; /// The start time. };
You use the class in this way:
TimeDelta delta; // ... delta.start(currentTime); // things happen if (delta.elapsed(currentTime) > 500) { // ... }
Continue here: Controlling the LED