ind03

Endurance

Apr 8th, 2021
395
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×