-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathColdEND_1.9.ino
More file actions
529 lines (463 loc) · 14.1 KB
/
ColdEND_1.9.ino
File metadata and controls
529 lines (463 loc) · 14.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
ColdEND v1.8 Minimum Quantity Lubrication
https://www.end-cnc-shop.de/geloetetes/3/pumpen-steuerung-1.5-bauteile-set
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Based on firmware by:
Sebastian End / END-CNC
Daniel Seoane / SeoGeo
Rewritten by Tilman, 2020-09-08
New motor control, fast screen refresh rate and pump icon added by Talla83.de, 2020-09-21
Last edited by Tilman: 2020-10-20
*/
#include <TimerOne.h> // Required library: https://github.com/PaulStoffregen/TimerOne
#include "config.h"
// Mist and spit variables
long mist_flow_rate; // Coolant flow rate value
int spit_time; // Spit mode time
byte spit_stat = 0; // Spit mode state
int mist_pot; // Raw mist potentiometer value
int mist_prev; // Previous smoothed mist potentiometer value
int mist_smooth; // Smoothed mist potentiometer value
int spit_pot; // Raw spit potentiometer value
byte air_valve = 0; // Air valve state (on/off)
byte mist_valve = 0; // Mist valve state (on/off)
unsigned long spit_start; // Spit mode start time in millis
unsigned long spit_stop; // Spit mode stop time in millis
byte mist_stat = HIGH; // Mist switch status
byte air_stat = HIGH; // Air switch status
byte ext_stat = HIGH; // External mist switch status
byte old_stat; // Previous status of external mist switch
// Display variables
float mist_pot_val; // Mist value in milliliter
int spit_pot_val; // Spit value in seconds
byte spit_mode; // Spit mode state
unsigned long prev_refresh = 0; // Previous refresh time in millis
unsigned long curr_refresh; // Current refresh time in millis
float mist_pot_old; // Previous values to check for changes
int spit_pot_old;
byte mist_valve_old;
byte air_valve_old;
float exp_scale = log(max_ml_per_hour/min_ml_per_hour); // Exponential flow scaling
// Pump icon
const float pi = 3.14159267;
const int icon_center_x=75;
const int icon_center_y=58;
int iconvalue=1;
int iconcnt = 1;
int x;
int y;
void setup() {
// Set output pins
pinMode(outStep, OUTPUT);
pinMode(outDir, OUTPUT);
pinMode(outEna, OUTPUT);
pinMode(outMistValve, OUTPUT);
pinMode(outAirValve, OUTPUT);
pinMode(outSpitLED, OUTPUT);
// Set input pins
pinMode(inMist, INPUT_PULLUP);
pinMode(inFast, INPUT_PULLUP);
pinMode(inAir, INPUT_PULLUP);
// Set stepper pins
digitalWrite(outDir, LOW); // Change direction with HIGH and LOW
digitalWrite(outEna, HIGH); // Disable stepper
// Initialize Display
#ifdef SSD1306
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADD);
#endif
#ifdef SH1106
Serial.begin(9600);
display.begin(SH1106_SWITCHCAPVCC, OLED_ADD);
#endif
#ifdef HT16K33
mistDisplay.begin(LED1_ADD);
mistDisplay.setBrightness(BRIGHTNESS);
spitDisplay.begin(LED2_ADD);
spitDisplay.setBrightness(BRIGHTNESS);
#endif
#ifdef LCD
lcd.begin();
lcd.backlight();
#endif
Timer1.initialize(1000000); // Initialize timer1
Timer1.attachInterrupt(callback); // Attaches callback() as a timer overflow interrupt
}
void loop() {
readSpitPot(); // Read values from spit pot
readMistPot(); // Read values from mist pot
#ifdef momentary_switch // If momentary switches, check for toggling
checkMistStat();
#ifdef ext_mist_ctrl
air_stat = HIGH;
#else
checkAirStat();
#endif
#else // Else if normal switches, read switches
mist_stat = digitalRead(inMist);
air_stat = digitalRead(inAir);
#endif
if (spit_time > spit_min_time) { // Activate spit LED when spit time > 1s
digitalWrite(outSpitLED, HIGH);
}
else {
digitalWrite(outSpitLED, LOW);
}
if (air_stat == LOW) { // Switching air valve
openAirValve();
}
else {
closeAirValve();
}
if (digitalRead(inFast) == LOW) { // Switch to fast mode (overrides normal operation)
#ifdef drain_system
if (mist_pot_val == 0) {
digitalWrite(outDir, HIGH);
}
else {
digitalWrite(outDir, LOW);
}
#endif
moveStepper(fast_flow_rate);
}
else if (mist_stat == LOW) { // Switch air mode or mist mode, depending on coolant output
openAirValve();
if (mist_pot_val > 1) { // If coolant > 1 ml/h, , execute spit mode and then switch to normal operation
spitMode();
moveStepper(mist_flow_rate);
}
else {
stopStepper();
}
}
else { // Stops normal operation (including spit mode)
stopStepper();
spit_stat = 0;
}
#if defined OLED || defined LED || defined LCD
prepareDisplay(); // Refresh display if necessary
#endif
}
void spitMode() {
// Spit mode, check for conditions and run for a predefined time
if (spit_stat == 0 && spit_time > spit_min_time) {
delay(switch_debounce);
spit_start = millis();
spit_stop = spit_start + spit_time;
while (millis() < spit_stop) { // Spit loop
moveStepper(spit_flow_rate); // Move stepper with spit flow rate
if (millis() > spit_start + 1000){ // Spit countdown in seconds
spit_start = millis();
spit_pot_val--;
}
#if defined LCD || defined LED || defined OLED
readMistPot();
prepareDisplay(); // Force display refresh
#endif
#ifdef momentary_switch // Force check if mist mode is still active
checkMistStat();
#else
mist_stat = digitalRead(inMist);
#endif
if (mist_stat == HIGH) { // Interrupt loop if mist mode has been stopped
break;
}
}
spit_mode = 0; // Reset active spit mode flag
}
spit_stat = 1; // Flag indicating that spit mode has been executed
}
void moveStepper(long delay) {
// Open mist valve and run stepper at desired speed
#ifdef PROBOARD
digitalWrite(outMistValve, HIGH);
#else
digitalWrite(outMistValve, LOW);
#endif
digitalWrite(outEna, LOW);
Timer1.pwm(outStep, 512, delay);
if (!spit_mode){
mist_valve = 1;
}
}
void stopStepper() {
digitalWrite(outEna, HIGH);
#ifdef PROBOARD
digitalWrite(outMistValve, LOW);
#else
digitalWrite(outMistValve, HIGH);
#endif
mist_valve = 0;
}
void callback() {
digitalWrite(outStep, digitalRead(outStep) ^ 1);
iconcnt++;
if (iconcnt > 80) {
iconcnt = 1;
if (iconvalue > 60) {
iconvalue = 1;
}
else{
iconvalue++;
}
}
}
void openAirValve() {
#ifdef PROBOARD
digitalWrite(outAirValve, HIGH);
#else
digitalWrite(outAirValve, LOW);
#endif
air_valve = 1;
}
void closeAirValve() {
#ifdef PROBOARD
digitalWrite(outAirValve, LOW);
#else
digitalWrite(outAirValve, HIGH);
#endif
air_valve = 0;
}
void readMistPot() {
// Read mist pot, smooth the value and convert it for stepper speed and display output
mist_prev = mist_smooth;
mist_pot = analogRead(potMist);
mist_smooth = smooth_filter*mist_pot + (1-smooth_filter)*mist_prev;
#ifdef linear_control
mist_pot_val = int(max(map(mist_smooth, 0, 1000, max_ml_per_hour, min_ml_per_hour), min_ml_per_hour));
#else
mist_pot_val = exp(max(0, 1000-mist_smooth)/1000.0*exp_scale)*min_ml_per_hour;
if (mist_pot_val < 10) {
mist_pot_val = int(mist_pot_val*10)/10.0;
}
else {
mist_pot_val = int(mist_pot_val);
}
#endif
if (mist_pot_val < 1.1) {
mist_pot_val = 0;
}
mist_flow_rate = round(max_flow_rate * max_ml_per_hour / mist_pot_val);
}
void readSpitPot() {
// Read spit pot and convert value for spit time and display output
spit_pot = analogRead(potSpit);
spit_time = map(spit_pot, 0, 1000, spit_max_time, 0);
spit_pot_val = spit_time/1000;
}
#ifdef momentary_switch
void checkMistStat() {
// Read external mist control
#ifdef ext_mist_ctrl
ext_stat = digitalRead(inAir);
if (ext_stat != old_stat) {
if (ext_stat == LOW) {
mist_stat = LOW;
}
else {
mist_stat = HIGH;
}
old_stat = ext_stat;
}
#endif
// Read momentary mist switch
btnMist.update();
if(btnMist.fell()) {
if (mist_stat == HIGH) {
mist_stat = LOW;
}
else {
mist_stat = HIGH;
}
}
}
#ifndef ext_mist_ctrl
void checkAirStat() {
// Read momentary air switch
btnAir.update();
if(btnAir.fell()) {
if (air_stat == HIGH) {
air_stat = LOW;
}
else {
air_stat = HIGH;
}
}
}
#endif
#endif
#if defined LCD || defined LED || defined OLED
void prepareDisplay() {
curr_refresh = millis();
if (curr_refresh - prev_refresh >= screen_delay) {
refreshDisplay();
prev_refresh = curr_refresh;
}
}
#ifdef LCD
void refreshDisplay() {
if (mist_pot_val != mist_pot_old || spit_pot_val != spit_pot_old || mist_valve != mist_valve_old || air_valve != air_valve_old) {
lcd.clear();
lcd.print("Coolant: ");
#ifndef linear_control
if (mist_pot_val < 10) {
char s[4];
dtostrf(mist_pot_val, 3, 1, s);
lcd.print(s);
}
else {
if (mist_pot_val < 100) {
lcd.setCursor(10, 0);
}
lcd.print(int(mist_pot_val));
}
#else
if (mist_pot_val < 10) {
lcd.setCursor(11, 0);
}
else if (mist_pot_val < 100) {
lcd.setCursor(10, 0);
}
else {
lcd.setCursor(9, 0);
}
lcd.print(int(mist_pot_val));
#endif
lcd.print("ml/h");
lcd.setCursor(0, 1);
lcd.print("Spit T.:");
lcd.setCursor(14, 1);
lcd.print(spit_pot_val);
lcd.print("s");
#ifdef LCD16X4
lcd.setCursor(-4, 2);
lcd.print("C. Valve: ");
if (mist_valve == 1) {
lcd.setCursor(10, 2);
lcd.print("On");
}
else {
lcd.setCursor(11, 2);
lcd.print("-");
}
lcd.setCursor(-4, 3);
lcd.print("A. Valve: ");
if (air_valve == 1) {
lcd.setCursor(10, 3);
lcd.print("On");
}
else {
lcd.setCursor(11, 3);
lcd.print("-");
}
#endif
mist_pot_old = mist_pot_val;
spit_pot_old = spit_pot_val;
mist_valve_old = mist_valve;
air_valve_old = air_valve;
}
}
#endif
#ifdef LED
void refreshDisplay() {
if (mist_pot_val != mist_pot_old || spit_pot_val != spit_pot_old || mist_valve != mist_valve_old || air_valve != air_valve_old) {
mistDisplay.print(mist_pot_val);
mistDisplay.writeDisplay();
spitDisplay.clear();
if (mist_valve == 1) {
spitDisplay.writeDigitRaw(0, 0b10111001);
}
if (air_valve == 1) {
spitDisplay.writeDigitRaw(1, 0b11110111);
}
spitDisplay.writeDigitNum(4, spit_pot_val);
spitDisplay.writeDisplay();
mist_pot_old = mist_pot_val;
spit_pot_old = spit_pot_val;
mist_valve_old = mist_valve;
air_valve_old = air_valve;
}
}
#endif
#ifdef OLED
void refreshDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Coolant");
display.setCursor(101, 0);
display.print("Spit");
display.drawLine(0, 12, 128, 12, WHITE);
display.setFont(&FreeSans18pt7b);
display.setCursor(0, 42);
#ifndef linear_control
if (mist_pot_val < 10) {
char s[4];
dtostrf(mist_pot_val, 3, 1, s);
display.print(s);
}
else {
display.print(int(mist_pot_val));
}
#else
display.print(int(mist_pot_val));
#endif
display.setFont();
display.setTextSize(1);
if (mist_pot_val < 10) {
#ifndef linear_control
display.setCursor(51, 36);
#else
display.setCursor(22, 36);
#endif
}
else if (mist_pot_val < 100) {
display.setCursor(41, 36);
}
else {
display.setCursor(60, 36);
}
display.print("ml/h");
display.setFont(&FreeSans18pt7b);
display.setCursor(101, 42);
display.print(spit_pot_val);
display.setFont();
display.setTextSize(1);
display.setCursor(122, 36);
display.print("s");
display.drawLine(0, 48, 128, 48, WHITE);
display.setTextColor(BLACK);
if (mist_valve == 1) {
display.fillRect(0, 54, 63, 10, WHITE);
display.setCursor(2, 55);
display.print("Coolant On");
drawPump(iconvalue, 1);
}
else if (spit_mode == 1) {
display.fillRect(0, 54, 57, 10, WHITE);
display.setCursor(2, 55);
display.print("Spit Mode");
drawPump(iconvalue, 1);
}
if (air_valve == 1) {
display.fillRect(89, 54, 39, 10, WHITE);
display.setCursor(91, 55);
display.print("Air On");
}
display.display();
}
void drawPump(int pump, int mode) {
// Draw spinning pump icon
display.drawCircle(icon_center_x, icon_center_y, 5, WHITE);
y = (3*cos(pi-(2*pi)/60*pump))+icon_center_y;
x = (3*sin(pi-(2*pi)/60*pump))+icon_center_x;
if (mode == 1) {
display.fillCircle(x, y, 2, WHITE);
}
else {
display.drawCircle(x, y, 2, BLACK);
}
}
#endif
#endif