RSeries astromech firmware
RelaySwitch.h
Go to the documentation of this file.
1 #ifndef RelaySwitch_h
2 #define RelaySwitch_h
3 
4 #include "core/SetupEvent.h"
5 #include "core/AnimatedEvent.h"
6 #include "core/CommandEvent.h"
7 
18 class RelaySwitch :
19  public AnimatedEvent
20 {
21 public:
25  RelaySwitch(byte relayPin, uint32_t pauseTime = 0) :
26  fRelayPin(relayPin),
27  fNextTime(0),
28  fCutOffTime(0),
29  fPauseTime(pauseTime)
30  {
31  // Smoke Machine Relay
32  pinMode(fRelayPin, OUTPUT);
33  digitalWrite(fRelayPin, LOW);
34  }
35 
40  void relayOn(uint32_t switchOffMS = 0)
41  {
42  uint32_t currentTime = millis();
43  if (fNextTime < currentTime)
44  {
45  digitalWrite(fRelayPin, HIGH);
46  fCutOffTime = (switchOffMS != 0) ? currentTime + switchOffMS : 0;
47  fNextTime = (fPauseTime != 0) ? currentTime + fPauseTime : 0;
48  }
49  else
50  {
51  /* Ignore any activation that happens before the minimum pause time */
52  }
53  }
54 
58  void relayOff()
59  {
60  digitalWrite(fRelayPin, LOW);
61  fCutOffTime = 0;
62  }
63 
67  virtual void animate() override
68  {
69  if (fCutOffTime == 0)
70  return;
71  if (fCutOffTime < millis())
72  {
73  relayOff();
74  }
75  }
76 
77 private:
78  static const uint32_t MAXIMUM_TIME = 6500L; /* 6.5 seconds */
79  byte fRelayPin;
80  uint32_t fNextTime;
81  uint32_t fCutOffTime;
82  uint32_t fPauseTime;
83 };
84 
86 
87 #endif
SetupEvent.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
AnimatedEvent.h
RelaySwitch
Controls a relay.
Definition: RelaySwitch.h:18
RelaySwitch::animate
virtual void animate() override
Check if smoke machine timer has expired.
Definition: RelaySwitch.h:67
RelaySwitch::relayOn
void relayOn(uint32_t switchOffMS=0)
Turn on the smoke machine.
Definition: RelaySwitch.h:40
RelaySwitch::relayOff
void relayOff()
Turn off the smoke machine.
Definition: RelaySwitch.h:58
RelaySwitch::RelaySwitch
RelaySwitch(byte relayPin, uint32_t pauseTime=0)
Constructor.
Definition: RelaySwitch.h:25
CommandEvent.h