-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatching-relay.ino
119 lines (104 loc) · 2.26 KB
/
latching-relay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// outputs
#define CHARGE_ON_PIN 3
#define CHARGE_OFF_PIN 4
#define LOAD_ON_PIN 5
#define LOAD_OFF_PIN 6
// inputs
#define ENABLE_CHARGE_PIN 7
#define ENABLE_LOAD_PIN 8
class DualCoilLatchingRelay {
private:
byte pinOn;
byte pinOff;
bool stateOn;
public:
DualCoilLatchingRelay(byte pinOn, byte pinOff) {
this->pinOn = pinOn;
this->pinOff = pinOff;
init();
}
void init() {
pinMode(pinOn, OUTPUT);
pinMode(pinOff, OUTPUT);
digitalWrite(pinOn, LOW);
digitalWrite(pinOff, LOW);
bool stateOn = true;
off();
}
void on() {
if(!stateOn) {
pulse(pinOn);
}
stateOn = true;
}
void off() {
if(stateOn) {
pulse(pinOff);
}
stateOn = false;
}
void pulse(byte pin) {
Serial.print("pulse: ");
Serial.println(pin);
digitalWrite(pin, LOW);
delay(10);
digitalWrite(pin, HIGH);
delay(100);
digitalWrite(pin, LOW);
}
};
class InputEnable {
private:
byte pin;
byte state;
byte lastReading;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200;
public:
InputEnable(byte pin) {
this->pin = pin;
lastReading = HIGH;
init();
}
void init() {
pinMode(pin, INPUT_PULLUP);
update();
}
void update() {
byte newReading = digitalRead(pin);
if (newReading != lastReading) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime > debounceDelay) {
// Update the 'state' attribute only if debounce is checked
state = newReading;
}
lastReading = newReading;
}
byte getState() {
update();
return state;
}
bool isEnabled() {
return (getState() == LOW);
}
};
DualCoilLatchingRelay chargeRelay(CHARGE_ON_PIN, CHARGE_OFF_PIN);
DualCoilLatchingRelay loadRelay(LOAD_ON_PIN, LOAD_OFF_PIN);
InputEnable enableCharge(ENABLE_CHARGE_PIN);
InputEnable enableLoad(ENABLE_LOAD_PIN);
void setup() {
Serial.begin(9600);
}
void loop() {
if (enableCharge.isEnabled()) {
chargeRelay.on();
} else {
chargeRelay.off();
}
if (enableLoad.isEnabled()) {
loadRelay.on();
} else {
loadRelay.off();
}
}