-
Notifications
You must be signed in to change notification settings - Fork 1
/
crepemaker.ino
214 lines (172 loc) · 5.33 KB
/
crepemaker.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
// The user.h file contains user-definable compiler options
// It must be located in the same folder as this file
#include "user.h"
// libraries to be installed from the library manager
#include "max6675.h"
#include "math.h"
// global variables and objects
MAX6675 thermocouple1(CLKPIN, CS1PIN, DOPIN);
MAX6675 thermocouple2(CLKPIN, CS2PIN, DOPIN);
// variables for the heating control
float previoustemp1 = 0;
float previoustemp2 = 0;
unsigned long lastControlUpdate = 0; // last time controls were updated
float control1 = 0; // how much (0..1) of the heating cycle is the 1st heater switched on
float control2 = 0;
byte on1 = 0; // whether the 1st heater is currently switched on
byte on2 = 0;
// variables for the moving averages on temperature readings
float tempReadings1[NUMREADINGS]; // the temperature readings from the analog input
float tempReadings2[NUMREADINGS];
int readIndex = 0; // the index of the current reading
unsigned long lastRead = 0; // last time readings were updated
float tempReadingsTotal1 = 0; // the running total
float tempReadingsTotal2 = 0;
float temp1; // the average
float temp2;
void seriallogger(float temp1, float temp2, float ror1, float ror2, float projectedtemp1, float projectedtemp2, float control1, float control2) {
Serial.print(temp1);
Serial.print(",");
Serial.print(temp2);
Serial.print(",");
Serial.print(ror1);
Serial.print(",");
Serial.print(ror2);
Serial.print(",");
Serial.print(projectedtemp1);
Serial.print(",");
Serial.print(projectedtemp2);
Serial.print(",");
Serial.print(control1);
Serial.print(",");
Serial.println(control2);
}
void setup() {
pinMode(SSR1PIN, OUTPUT);
pinMode(SSR2PIN, OUTPUT);
Serial.begin(BAUD);
// Make sure everything is off by default
stop1();
stop2();
// wait for MAX chips to stabilize
delay(500);
// initialize the readings array to zero
for (int i = 0; i < NUMREADINGS; i++) {
tempReadings1[i] = 0;
tempReadings2[i] = 0;
}
// initialize the temperature readings and averages with actual values
while (readIndex < NUMREADINGS - 1) {
updateTemperatureReadings();
delay(LOOP_PERIOD_MS);
}
}
void loop() {
updateTemperatureReadings();
updateControls();
delay(LOOP_PERIOD_MS);
}
void updateTemperatureReadings() {
if (millis() - lastRead < READING_PERIOD_MS) {
return;
}
lastRead += READING_PERIOD_MS;
// subtract the last reading
tempReadingsTotal1 = tempReadingsTotal1 - tempReadings1[readIndex];
tempReadingsTotal2 = tempReadingsTotal2 - tempReadings2[readIndex];
// read from the sensor
tempReadings1[readIndex] = thermocouple1.readCelsius();
tempReadings2[readIndex] = thermocouple2.readCelsius();
// add the reading to the total
tempReadingsTotal1 = tempReadingsTotal1 + tempReadings1[readIndex];
tempReadingsTotal2 = tempReadingsTotal2 + tempReadings2[readIndex];
// advance to the next position in the array
readIndex = readIndex + 1;
// if we are at the end of the array...
if (readIndex >= NUMREADINGS) {
// ...wrap around to the beginning
readIndex = 0;
}
// calculate the average
temp1 = tempReadingsTotal1 / NUMREADINGS;
temp2 = tempReadingsTotal2 / NUMREADINGS;
}
void updateControls()
{
unsigned long timeelapsed = 0;
float ror1;
float ror2;
float projectedtemp1;
float projectedtemp2;
float steps;
timeelapsed = (millis() - lastControlUpdate);
if (timeelapsed < CONTROL_UPDATE_PERIOD_MS) {
if (on1 == 1 && timeelapsed > control1 * CONTROL_UPDATE_PERIOD_MS) {
stop1();
if (control2 > 0) {
start2();
}
}
if (on2 == 1 && timeelapsed > (control1 + control2) * CONTROL_UPDATE_PERIOD_MS) {
stop2();
}
return;
}
lastControlUpdate += CONTROL_UPDATE_PERIOD_MS; // do not use timeelapsed here to avoid drift
ror1 = (((temp1 - previoustemp1) / timeelapsed) * 1000 * 60); //Raise of rise in C/min
ror2 = (((temp2 - previoustemp2) / timeelapsed) * 1000 * 60); //Raise of rise in C/min
previoustemp1 = temp1;
previoustemp2 = temp2;
if (ror1 >= 0) {
projectedtemp1 = (temp1 + (ror1 * RISEINERTIA));
}
else {
projectedtemp1 = (temp1 + (ror1 * FALLINERTIA));
}
if (ror2 >= 0) {
projectedtemp2 = (temp2 + (ror2 * RISEINERTIA));
}
else {
projectedtemp2 = (temp2 + (ror2 * FALLINERTIA));
}
// proportional controls
control1 = max(0, min(1, (SV1 - projectedtemp1) / PROPORTIONAL_TEMP_RANGE));
control2 = max(0, min(1, (SV2 - projectedtemp2) / PROPORTIONAL_TEMP_RANGE));
// 1st heater takes priority, 2nd heater takes what is left
control2 = min(control2, 1 - control1);
// use steps of MINIMAL_SSR_MS to avoid short switches
steps = round(control1 * CONTROL_UPDATE_PERIOD_MS / MINIMAL_SSR_MS);
control1 = steps * MINIMAL_SSR_MS / CONTROL_UPDATE_PERIOD_MS;
steps = round(control2 * CONTROL_UPDATE_PERIOD_MS / MINIMAL_SSR_MS);
control2 = steps * MINIMAL_SSR_MS / CONTROL_UPDATE_PERIOD_MS;
if (control1 > 0) {
stop2();
start1();
}
else {
stop1();
if (control2 > 0) {
start2();
}
else {
stop2();
}
}
seriallogger(temp1, temp2, ror1, ror2, projectedtemp1, projectedtemp2, control1, control2);
}
void start1() {
digitalWrite(SSR1PIN, HIGH);
on1 = 1;
}
void stop1() {
digitalWrite(SSR1PIN, LOW);
on1 = 0;
}
void start2() {
digitalWrite(SSR2PIN, HIGH);
on2 = 1;
}
void stop2() {
digitalWrite(SSR2PIN, LOW);
on2 = 0;
}