Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Endurance.h
- #pragma once
- #ifndef _ENDURANCE_h
- #define _ENDURANCE_h
- #include <arduino.h>
- class Endurance
- {
- unsigned ms;
- unsigned long duration;
- bool state;
- public:
- operator bool () const { return state; }
- public:
- Endurance (unsigned ms, bool state = false)
- : ms (ms), duration (millis()+ms), state (state)
- {
- }
- void reset ()
- {
- duration = millis () + ms;
- this->state = false;
- }
- void trigger ()
- {
- trigger (true);
- }
- void trigger (bool state)
- {
- // Set state if duration is reached.
- if (state)
- {
- if (millis () > duration)
- this->state = true;
- }
- // Reset on false
- else
- {
- this->state = false;
- duration = millis () + ms;
- }
- }
- bool getState () const
- {
- return state;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement