Advertisement
ind03

Endurance

Apr 8th, 2021
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. // Endurance.h
  2. #pragma once
  3.  
  4. #ifndef _ENDURANCE_h
  5. #define _ENDURANCE_h
  6.  
  7. #include <arduino.h>
  8.  
  9. class Endurance
  10. {
  11.     unsigned ms;
  12.     unsigned long duration;
  13.     bool state;
  14.    
  15.  
  16.  
  17. public:
  18.     operator bool () const { return state; }
  19.    
  20. public:
  21.     Endurance (unsigned ms, bool state = false)
  22.         : ms (ms), duration (millis()+ms), state (state)
  23.     {
  24.     }
  25.  
  26.     void reset ()
  27.     {
  28.         duration = millis () + ms;
  29.         this->state = false;
  30.     }
  31.    
  32.     void trigger ()
  33.     {
  34.         trigger (true);
  35.     }
  36.  
  37.     void trigger (bool state)
  38.     {
  39.         // Set state if duration is reached.
  40.         if (state)
  41.         {
  42.             if (millis () > duration)
  43.                 this->state = true;
  44.         }
  45.  
  46.         // Reset on false
  47.         else
  48.         {
  49.             this->state = false;
  50.             duration = millis () + ms;
  51.         }
  52.     }
  53.  
  54.     bool getState () const
  55.     {
  56.         return state;
  57.     }
  58. };
  59.  
  60.  
  61. #endif
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement