Biduleohm

Timer arduino

Dec 4th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1.  
  2. /*
  3. ** Made by Biduleohm
  4. ** 2016/05
  5. */
  6.  
  7. typedef uint32_t    ulong;
  8.  
  9. class           Timer
  10. {
  11.     public:
  12.         ulong   time;
  13.  
  14.         Timer();
  15.         void    reset();
  16.         void    reset(ulong _duration);
  17.         void    update();
  18.         bool    hasEnded();
  19.         bool    hasEnded(ulong _duration);
  20.  
  21.     private:
  22.         ulong   start;
  23.         ulong   duration;
  24. };
  25.  
  26. Timer::Timer()
  27. {
  28.     this->reset();
  29. }
  30.  
  31. void    Timer::reset()
  32. {
  33.     this->start = millis();
  34.     this->time = 0;
  35.     this->duration = 0;
  36. }
  37.  
  38. void    Timer::reset(ulong _duration)
  39. {
  40.     this->start = millis();
  41.     this->time = 0;
  42.     this->duration = _duration;
  43. }
  44.  
  45. void    Timer::update()
  46. {
  47.     ulong   current = millis();
  48.  
  49.     if (current < this->start) { // takes care of the overflow after ~50 days
  50.         this->start = current;
  51.     }
  52.     this->time = current - this->start;
  53. }
  54.  
  55. bool    Timer::hasEnded()
  56. {
  57.     return (this->time >= this->duration);
  58. }
  59.  
  60. bool    Timer::hasEnded(ulong _duration)
  61. {
  62.     return (this->time >= _duration);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment