-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeaterMainLogic.ino
138 lines (121 loc) · 3.65 KB
/
HeaterMainLogic.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**************************************************************
Home waterpool manager
designed for the ESP8266/Arduino platform (https://github.com/esp8266/Arduino)
Intelligent managing water heating and heater overheat protection
Built by Sergey Korostel
Licensed under MIT license
Main business logic which checks that the sensor values is in limits and allow relay switch heating or pumps.
**************************************************************/
// Variables
unsigned long timeWhenHeatOff = 0; // Time passed after heater was switched off, millis
// Check if the current sensor values is in limits. Then heater can be switched ON
bool HeaterCanBeON()
{
bool canBeON = false;
if (heaterEnable) // In case if heater device is ON, then check sensor limits...
{
int tempInCelsius[]{ 0, 0, 0, 0 };
for (byte t = 0; t <= lastSensorIndex; t++) {
tempInCelsius[t] = (tempData[t] + 8) >> 4; // In celsius, with math rounding
}
// Sensors is found and temperature less than max allowed
// or temp sensor limits is switched off (max is set to 0)
bool temperatureInRanges = lastSensorIndex >= 0;
for (byte t = 0; t <= lastSensorIndex; t++)
{
// Once temperature gone over the bounds, then wait till temperature goes down to some delta value (avoid switch bouncing).
int delta = tempInRange[t] ? 0 : maximumAllowedTempDelta;
tempInRange[t] = tempLimits[t] == 0 || (tempInCelsius[t] >= 0 && tempInCelsius[t] <= tempLimits[t] - delta);
temperatureInRanges = temperatureInRanges && tempInRange[t];
}
// If flow is more or equal of minimum alowed,
// this include zero (sensor limit is switched off).
bool flowInRange = (litersInMinute >= minimumAllowedFlow);
canBeON = (temperatureInRanges && flowInRange);
}
return canBeON;
}
void resetSensorLimitRanges()
{
for (byte t = 0; t < TSCOUNT; t++)
tempInRange[t] = true;
}
// Switch Heat relay ON or OFF depends on limit values
void SwitchHeatRelay()
{
heatingAllowed = HeaterCanBeON();
bool prevRelayHeatON = relayHeatON;
relayHeatON = heatingAllowed && heaterEnable;
bool relayStateChanged = relayHeatON != prevRelayHeatON;
if (relayStateChanged)
{
if (relayHeatON)
{
SwitchPumpRelay();
HeatRelayON();
}
else
{
HeatRelayOFF();
timeWhenHeatOff = currentDateTime.TotalSeconds();
if (!pumpEnable)
{
// ToDo: start timer to switch off pump relay after specified RELAY_PUMP_DELAY
}
}
}
if (relayStateChanged)
resetTimer();
}
// Switch Pump relay ON or OFF depends on heater relay status and pump user pereference
void SwitchPumpRelay()
{
bool prevRelayPumpON = relayPumpON;
relayPumpON = pumpEnable;
// Pump relay shpould always work when the heating is ON and some time after that.
if (relayHeatON)
relayPumpON = true;
else
{
uint32_t seconds = currentDateTime.TotalSeconds();
// If heating is OFF, then we check how long it was switched off. If just switched off, then we left pump ON for the specified time.
if (seconds - timeWhenHeatOff < RELAY_PUMP_DELAY)
{
relayPumpON = true;
}
}
bool relayStateChanged = relayPumpON != prevRelayPumpON;
if (relayStateChanged)
{
if (relayPumpON)
PumpRelayON();
else
PumpRelayOFF();
}
}
unsigned char GetDeviceStatus()
{
return pumpEnable | heaterEnable << 1 | relayPumpON << 2 | relayHeatON << 3 | heatingAllowed << 4 |
relay3ON << 5 | relay4ON << 6;
}
bool prevRelay3ON = !relay3ON;
bool prevRelay4ON = !relay4ON;
void SwitchAdditionalRelays()
{
if (prevRelay3ON != relay3ON)
{
if (relay3ON)
RelayOn(3 - 1);
else
RelayOff(3 - 1);
prevRelay3ON = relay3ON;
}
if (prevRelay4ON != relay4ON)
{
if (relay4ON)
RelayOn(4 - 1);
else
RelayOff(4 - 1);
prevRelay4ON = relay4ON;
}
}