-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathard-temp-control.ino
286 lines (240 loc) · 5.67 KB
/
ard-temp-control.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <AutoPID.h>
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DS18B20.h>
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define TARGET 23 // °C
#define SS_PIN 10
#define RST_PIN 9
#define FAN_OFFSET 25
#define SCAN_TIME 15 // Seconds
#define SCAN_TIME_OFFSET 5
#define DOOR_TIMEOUT 5
#define BANG 5
#define SET_OFFSET_AS_MIN true
#define MILLIS_MULTIPLIER 1000
/* Sensor Pins */
int positive1 = A0;
int negative1 = A2; // utility
int signalPin = A1; // utility
int pinOutput1 = 3;
/* Rele pins */
int rele1 = 2;
int rele2 = 4;
/* Door Sensor */
int door1 = 5;
/* Time vars */
unsigned long actTime = 0;
unsigned long readTempTimeout1 = 0;
unsigned long openDoorTimeout = 0;
/* Temp vars */
unsigned long iterations1 = 0;
float poolValues1 = 0.0;
float temp1 = 0;
String inputString = "";
bool stringComplete = false;
const int maxTemp = 60;
bool isEEPROMEmpty = false;
/* PID */
double Setpoint1, Input1 = 0, Output1 = 0;
/* Define the Tuning Parameters */
double consKp1=4, consKi1=0.2, consKd1=1;
/* Dark Mode */
bool dark = true;
/* Specify the links and initial tuning parameters */
AutoPID PID1(&Input1, &Setpoint1, &Output1, OUTPUT_MIN, OUTPUT_MAX, consKp1, consKi1, consKd1);
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
OneWire oneWire(signalPin);
DS18B20 sensor(&oneWire);
byte* nuidPICC;
void setup() {
Serial.begin(115200);
Serial.println(__FILE__);
analogReference(INTERNAL);
pinMode(positive1, OUTPUT);
pinMode(negative1, OUTPUT);
pinMode(rele1, OUTPUT);
pinMode(rele2, OUTPUT);
pinMode(door1, INPUT);
digitalWrite(rele1, HIGH);
digitalWrite(rele2, HIGH);
digitalWrite(positive1, HIGH);
digitalWrite(negative1, LOW);
pinMode(signalPin, INPUT);
PID1.setBangBang(BANG);
PID1.setTimeStep(MILLIS_MULTIPLIER * (SCAN_TIME + SCAN_TIME_OFFSET));
SPI.begin();
sensor.begin();
rfid.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Setpoint1 = maxTemp - TARGET;
inputString.reserve(255);
}
void loop() {
actTime = millis();
ploter1();
openDoor();
scanTemp1();
pid1();
readCard();
scanDoor();
scanSerial();
}
void reset() {
poolValues1 = 0;
iterations1 = 0;
}
void scanTemp1() {
sensor.requestTemperatures();
while (!sensor.isConversionComplete()); // wait until sensor is ready
poolValues1 += sensor.getTempC();
iterations1++;
}
void scanDoor() {
if(digitalRead(door1) && !dark) {
releOn(rele2);
} else {
releOff(rele2);
}
}
void scanSerial() {
if (stringComplete) {
commands(inputString);
inputString = "";
stringComplete = false;
}
}
void commands(String command) {
if (inputString.equals("loff\r\n")) {
dark = true;
}
if (inputString.equals("lon\r\n")) {
dark = false;
}
if (inputString.equals("open\r\n")) {
setDoorTimeout();
}
}
void pid1() {
if (temp1 != 0) {
Input1 = maxTemp - temp1;
double gap = abs(Setpoint1 - Input1);
PID1.run();
if (Output1 > FAN_OFFSET) {
analogWrite(pinOutput1, Output1);
} else {
if (SET_OFFSET_AS_MIN) {
analogWrite(pinOutput1, FAN_OFFSET);
} else {
analogWrite(pinOutput1, 0);
}
}
}
}
void ploter1() {
if (actTime >= readTempTimeout1) {
temp1 = (poolValues1/iterations1);
float output = Output1;
if (output < FAN_OFFSET) {
output = FAN_OFFSET;
}
reset();
if(!isnan(temp1)) {
Serial.print ("T");
Serial.print (temp1, 2);
Serial.print (",B");
Serial.println (output, 2);
Serial.print (temp1, 2);
Serial.print (",");
Serial.println (output / 10, 2);
}
readTempTimeout1 = actTime + (MILLIS_MULTIPLIER * SCAN_TIME);
}
}
void readCard() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
return;
}
if (compareData(rfid.uid.uidByte, rfid.uid.size)) {
setDoorTimeout();
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
void setDoorTimeout() {
openDoorTimeout = actTime + (DOOR_TIMEOUT * MILLIS_MULTIPLIER);
}
void openDoor() {
if (actTime >= openDoorTimeout) {
releOff(rele1);
} else {
releOn(rele1);
}
}
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void releOn(int rele) {
digitalWrite(rele, LOW);
}
void releOff(int rele) {
digitalWrite(rele, HIGH);
}
void saveEEPROM(uint8_t* data) {
for (byte i = 0; i < 4; i++) {
EEPROM.put(i, data[i]);
}
}
bool compareData(uint8_t* inputData, byte bufferSize) {
isEEPROMEmpty = false;
byte emptyCounter = 0;
for (uint8_t i = 0; i < 6; i++) {
if (EEPROM.read(i) != inputData[i]) {
if (EEPROM.read(i) == 0) {
emptyCounter++;
} else {
return false;
}
}
}
if(emptyCounter == 4) {
saveEEPROM(inputData);
}
return true;
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}