Lucky Resistor
Menu
  • Home
  • Learn
    • Learn C++
    • Product Photography for Electronics
      • Required Equipment and Software
    • Soldering for Show
  • Projects
  • Libraries
  • Applications
  • Shop
  • About
    • About Me
    • Contact
    • Stay Informed
  •  
Menu

Measure Elapsing Time

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

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay Updated

Join me on Mastodon!

Top Posts & Pages

  • Storage Boxes System for 3D Print
  • Event-based Firmware (Part 1/2)
  • Build a 3D Printer Enclosure
  • Yet Another Filament Filter
  • Circle Pattern Generator
  • Circle Pattern Generator
  • Real Time Counter and Integer Overflow
  • Projects
  • Logic Gates Puzzle 11
  • Units of Measurements for Safe C++ Variables

Latest Posts

  • Better Bridging with Slicer Guides2023-02-04
  • Stronger 3D Printed Parts with Vertical Perimeter Linking2023-02-02
  • Logic Gates Puzzle 1012023-02-02
  • Candlelight Emulation – Complexity with Layering2023-02-01
  • Three Ways to Integrate LED Light Into the Modular Lantern2023-01-29
  • The 3D Printed Modular Lantern2023-01-17
  • Rail Grid Alternatives and More Interesting Updates2022-12-09
  • Large Update to the Circle Pattern Generator2022-11-10

Categories

  • 3D Printing
  • Build
  • Common
  • Fail
  • Fun
  • Learn
  • Projects
  • Puzzle
  • Recommendations
  • Request for Comments
  • Review
  • Software
Copyright (c)2022 by Lucky Resistor. All rights reserved.