-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxa.ino
245 lines (207 loc) · 5.73 KB
/
oxa.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
/*
To control the sublimation of oxalic dihydrate.
title: OXA
version: 1.0
date: 19.12.2020
author: matthias.engelbracht
lizenz: MIT
Hardware:
- ARDUINO NANO
- LM2596 Dc12V->Dc5V power supply
- 22 Ohm resistance
- PT100
- 2 relays module for fan and heater output
- H4 car lamp for heating with 12V ~115W
- button (NO)
- buzzer
Setps:
- press button to start
- if temperature is higher than 100°C, cancel
- switch on fan
- wait for heat switch on delay
- heating up to ~170°C for ~3 minutes
- switch off the heater
- keep fan going for an other minute
- switch fan off and wait some time untill everything cooled down
- give 10 secound a sound
- stop at any time possible by button press
*/
#include <AutoPID.h>
#define cycleTime 25
#define HEATER_PIN 2
#define FAN_PIN 3
#define BUTTON_PIN 5
#define SIGNAL_PIN 4
#define TEMPERATURE_PIN A1 //5V -> 22ohm A1-Input -> PT100 0V
#define TEMPERATURE_ARRAY_LENGHT 100
#define TEMPERATURE_SWITCH_OF_VALUE 910
#define SWITCH_ON_DELAY 3000
#define TOTAL_HEATING_TIME 240000
#define FAN_SWITCH_OFF_DELAY 60000
#define COOL_DOWN_DELAY 30000
#define HEAT_PULS_WIDTH 3000
#define KP .10
#define KI .0003
#define KD 0
#define debug false //true
bool heatRelayState = false, relayOn = LOW, relayOff = HIGH, impuls1s = false, buttonImpuls = false, button = false, buttonTemp = false;
int oxaStep = 0, temperatureValuesIndex = 0;
double temperature, setPoint;
unsigned long timeAtCycleStart, timeAtCycleEnd, actualCycleTime, modeTimerStart, headTimerStart, mesureTimerStart, timerOneRun, lastTempUpdate, timeImpuls1s = 0;
float temperatureValues [TEMPERATURE_ARRAY_LENGHT] = {0}, temperatureValuesSum = 0.0;
AutoPIDRelay heatPidRelais(&temperature, &setPoint, &heatRelayState, HEAT_PULS_WIDTH, KP, KI, KD);
void pip(int pipTime = 100, int breakTime = 200, int pipCount = 1) {
for (int i = 0; i < pipCount; i++ )
{
digitalWrite(SIGNAL_PIN, HIGH);
delay(pipTime);
digitalWrite(SIGNAL_PIN, LOW);
delay(breakTime);
}
}
void readTemperatur() {
temperatureValues[temperatureValuesIndex] = analogRead(TEMPERATURE_PIN);
if (temperatureValuesIndex >= TEMPERATURE_ARRAY_LENGHT - 1) temperatureValuesIndex = 0;
else temperatureValuesIndex ++;
temperatureValuesSum = 0.0;
for (int idx = 0; idx < (TEMPERATURE_ARRAY_LENGHT); idx++ )
{
temperatureValuesSum += temperatureValues[idx];
}
temperature = temperatureValuesSum / float(TEMPERATURE_ARRAY_LENGHT);
}
void resetAll() {
oxaStep = 0;
buttonImpuls = false;
heatRelayState = false;
heatPidRelais.stop();
digitalWrite(HEATER_PIN, relayOff);
digitalWrite(FAN_PIN, relayOff);
pip(50, 100, 5);
}
void setup() {
Serial.begin(9600);
delay(10);
pinMode(HEATER_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(SIGNAL_PIN, OUTPUT);
digitalWrite(HEATER_PIN, relayOff);
digitalWrite(FAN_PIN, relayOff);
int initValue = analogRead(TEMPERATURE_PIN);
for (int t = 0; t < (TEMPERATURE_ARRAY_LENGHT); t++ )
{
temperatureValues[t] = initValue;
}
heatPidRelais.setBangBang(20, 1);
setPoint = TEMPERATURE_SWITCH_OF_VALUE;
buttonTemp = button = digitalRead(BUTTON_PIN);
resetAll();
}
void loop() {
timeAtCycleStart = millis();
if ((timeAtCycleStart - timeImpuls1s) > 1000)
{
impuls1s = true;
timeImpuls1s = timeAtCycleStart;
}
else
{
impuls1s = false;
}
// button: start/reset
button = digitalRead(BUTTON_PIN);
if ((button == false) and (buttonTemp == true))
{
buttonImpuls = true;
}
else
{
buttonImpuls = false;
}
buttonTemp = button;
if ((oxaStep > 0) and (buttonImpuls == true))
{
resetAll();
}
readTemperatur();
// oxa step chain
switch (oxaStep) {
case 0: // idle
if (buttonImpuls == true)
{
if (temperature > 885) //885 ~ 100°C
{
Serial.println(int(temperature));
resetAll();
break;
}
pip(500);
oxaStep++;
modeTimerStart = millis();
timerOneRun = modeTimerStart;
digitalWrite(FAN_PIN, relayOn);
}
break;
case 1: // fan on
if (millis() - modeTimerStart > SWITCH_ON_DELAY)
{
oxaStep++;
modeTimerStart = millis();
}
break;
case 2: // heat phase heating
if (debug and impuls1s)
{
Serial.print((millis() - modeTimerStart) / 1000);
Serial.print(";");
Serial.print(int(temperature));
Serial.print(";");
Serial.println(heatRelayState);
}
if (millis() - modeTimerStart > TOTAL_HEATING_TIME)
{
heatPidRelais.stop();
heatRelayState = false;
digitalWrite(HEATER_PIN, relayOff);
oxaStep++;
modeTimerStart = millis();
}
else {
heatPidRelais.run();
}
break;
case 3: // fan out
if (millis() - modeTimerStart > FAN_SWITCH_OFF_DELAY)
{
digitalWrite(FAN_PIN, relayOff);
oxaStep++;
modeTimerStart = millis();
}
else {
if (debug and impuls1s)
{
Serial.print((millis() - modeTimerStart) / 1000);
Serial.print(";");
Serial.println(int(temperature));
}
}
break;
case 4: // wait a moment
if (millis() - modeTimerStart > COOL_DOWN_DELAY)
{
oxaStep++;
}
break;
case 5: // ready signal
pip(100, 900, 10);
resetAll();
break;
}
digitalWrite(HEATER_PIN, !heatRelayState);
actualCycleTime = timeAtCycleEnd - timeAtCycleStart;
if (actualCycleTime < cycleTime)
{
delay(cycleTime - actualCycleTime);
}
}