-
Notifications
You must be signed in to change notification settings - Fork 2
/
Webasto_sample.ino
708 lines (611 loc) · 15.7 KB
/
Webasto_sample.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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include "Arduino.h"
#include <math.h> // needed to perform some calculations
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <avr/wdt.h>
byte fanChar[ ] = {
B00000,
B10011,
B10100,
B01110,
B00101,
B11001,
B00000,
B00000 };
byte fuelChar[ ] = {
B11110,
B10010,
B10011,
B11111,
B11111,
B11111,
B11110,
B00000 };
byte waterChar[ ] = {
B00000,
B00100,
B01110,
B01110,
B11111,
B11111,
B01110,
B00000 };
byte exhaustChar[ ] = {
B01110,
B01110,
B01110,
B11111,
B01110,
B00100,
B00000,
B00000 };
byte w_pumpChar[ ] = {
B00000,
B00100,
B11110,
B11111,
B11110,
B00100,
B00000,
B00000 };
int fuel_pump_pin = 5;
int glow_plug_pin = 6;
int burn_fan_pin = 9;
int water_pump_pin = 10;
//int water_temp_pin = A0;
//int exhaust_temp_pin = A1;
int water_input_index = 0;
int exhaust_input_index = 1;
int button_pin = 3;
int temperature_target = 70; // degres C
int run_pump_min = 30; // minimal temperature for run water pump
int fan_speed; // percent
int water_pump_speed; // percent
int fuel_need; // percent
int glow_time; // seconds
int water_temp; // degres C
int water_temp_sec[10];
int exhaust_temp; // degres C
int exhaust_temp_sec[10]; // array of last 10 sec water temp, degres C
int shower_timeout;
int percent_map = 0;
int ignit_fail;
int seconds;
bool shower;
bool burn;
bool webasto_fail;
bool lean_burn;
bool overheating;
int burn_mode = 0;
bool check_pause;
int old_water_temp;
int old_exhaust_temp;
int old_fan_speed;
int old_fuel_need;
int old_fan;
int blink_LED = 13;
int blink_LED_status = 0;
LiquidCrystal_I2C lcd(0x3f, 16, 2);
DeviceAddress deviceAddress;
OneWire ds18x20[ ] = { A0, A1 };
const int oneWireCount = sizeof(ds18x20) / sizeof(OneWire);
DallasTemperature sensor[oneWireCount];
void setup() {
webasto_fail = false;
for (int i = 0; i < oneWireCount; i++) {
sensor[i].setOneWire(&ds18x20[i]);
sensor[i].begin();
if (sensor[i].getAddress(deviceAddress, 0))
sensor[i].setResolution(deviceAddress, 9);
}
// lcd.createChar(0, waterChar);
// lcd.createChar(1, exhaustChar);
// lcd.createChar(2, fanChar);
// lcd.createChar(3, fuelChar);
// lcd.createChar(3, w_pumpChar);
TCCR1B = (TCCR1B & 0b11111000) | 0x01; // magic Fast PWM parameter
pinMode(glow_plug_pin, OUTPUT);
pinMode(fuel_pump_pin, OUTPUT);
pinMode(burn_fan_pin, OUTPUT);
pinMode(water_pump_pin, OUTPUT);
// pinMode(water_temp_pin, INPUT);
// pinMode(exhaust_temp_pin, INPUT);
pinMode(button_pin, INPUT_PULLUP); // important, this pulls de button pin to high, so when pressed it goes to low
pinMode(blink_LED, OUTPUT);
lcd.init();
lcd.home();
lcd.setCursor(0, 0);
lcd.backlight();
lcd.createChar(0, waterChar);
lcd.createChar(1, exhaustChar);
lcd.createChar(2, fanChar);
lcd.createChar(3, fuelChar);
lcd.createChar(4, w_pumpChar);
check_pause = false;
overheating = false;
Serial.begin(9600);
}
void temp_data() { // keeps the temp variables updated
static unsigned long timer;
// if (millis() < timer) {
// timer = millis();
// }
// call the get_temp function and smoothen the result
// water_temp = (9 * water_temp + get_temp(water_temp_pin)) / 10;
// exhaust_temp = (9 * exhaust_temp + get_temp(exhaust_temp_pin)) / 10;
water_temp = get_temp(water_input_index);
exhaust_temp = get_temp(exhaust_input_index);
if (millis() > timer + 1000) { // every sec
timer = millis();
for (int i = 9; i >= 1; i--) { // updating the exhaust temperature history
exhaust_temp_sec[i] = exhaust_temp_sec[i - 1];
water_temp_sec[i] = water_temp_sec[i - 1];
}
exhaust_temp_sec[0] = exhaust_temp; // add new temp value
water_temp_sec[0] = water_temp;
}
}
void control() {
static unsigned long timer;
if (millis() < timer) {
timer = millis();
}
static bool pushed;
static bool long_press;
bool push;
if (digitalRead(button_pin)) {
push = true;
} else {
push = false;
}
if (!push && !pushed) {
timer = millis();
}
if (push && !pushed) {
timer = millis();
pushed = true;
}
if (push && pushed && millis() > timer + 500) {
if (!long_press) { // when long press
long_press = true;
if (webasto_fail) { // reset webasto fail if there has been a failure
lcd.setCursor(0, 0);
lcd.print("RESET |");
shower_timeout = 0;
burn_mode = 0;
burn = 0;
glow_time = 0;
shower = false;
webasto_fail = false;
check_pause = false;
overheating = false;
}
if (!webasto_fail && burn_mode > 0) { // fast stop
lcd.setCursor(0, 0);
lcd.print("FAST STOP |");
burn_mode = 0;
shower_timeout = 0;
webasto_fail = false;
shower = false;
burn_mode = 0;
burn = 0;
glow_time = 0;
check_pause = false;
overheating = false;
}
}
}
if (pushed && !push) { // if shower is off, turn it on with a 60 seconds timeout
if (millis() > timer + 50 && !long_press) { // when short press
if (burn_mode > 0) { // if shower is on, turn it off with a 15 second timeout
lcd.setCursor(0, 0);
lcd.print("TURN OFF |");
shower_timeout = 15;
shower = false;
burn_mode = 3;
burn = 0;
glow_time = 0;
seconds = 0;
check_pause = false;
overheating = false;
}
if (burn_mode == 0) {
lcd.setCursor(0, 0);
lcd.print("START |");
shower = 1;
shower_timeout = 99;
check_pause = false;
overheating = false;
}
}
pushed = 0;
long_press = 0;
}
}
void shower_void() {
static unsigned long secs_timer;
if (millis() < secs_timer) {
secs_timer = millis();
}
if (shower) {
burn = 1;
if (shower_timeout == 99) {
shower_timeout = 60;
}
if (shower_timeout < 15) { // helps to burn all the remaining fuel in the combustion chamber
lean_burn = 1;
} else {
lean_burn = 0;
}
}
else { // reinitialize variables
burn = 0;
//seconds = 0;
shower_timeout = 0;
lean_burn = 0;
}
water_pump(); // calls the water_pump function
}
void printData(int time) {
Serial.println(
"fail|igniF|shower|burn|b_mode|waterT |exhaustT|fanSpeed|fuel |glowTime|Pump |Time");
Serial.print(" ");
Serial.print(webasto_fail);
Serial.print(" | ");
Serial.print(ignit_fail);
Serial.print(" | ");
Serial.print(shower);
Serial.print(" | ");
Serial.print(burn);
Serial.print(" | ");
Serial.print(burn_mode);
Serial.print(" | ");
Serial.print(water_temp);
Serial.print(" | ");
Serial.print(exhaust_temp);
Serial.print(" | ");
Serial.print(fan_speed);
Serial.print(" | ");
Serial.print(fuel_need);
Serial.print(" | ");
Serial.print(glow_time);
Serial.print(" | ");
Serial.print(water_pump_speed);
Serial.print(" | ");
Serial.print(time);
Serial.println();
}
void printLCD() {
if (water_temp != old_water_temp) {
lcd.setCursor(1, 1);
lcd.print(" ");
}
if (exhaust_temp != old_exhaust_temp) {
lcd.setCursor(5, 1);
lcd.print(" ");
}
if (fan_speed != old_fan_speed) {
lcd.setCursor(9, 1);
lcd.print(" ");
}
if (fuel_need != old_fuel_need) {
lcd.setCursor(13, 1);
lcd.print(" ");
}
lcd.setCursor(0, 1);
lcd.write(0);
lcd.print(water_temp);
lcd.setCursor(4, 1);
lcd.write(1);
lcd.print(exhaust_temp);
lcd.setCursor(8, 1);
lcd.write(2);
lcd.print(fan_speed);
lcd.setCursor(12, 1);
lcd.write(3);
lcd.print(fuel_need);
lcd.setCursor(11, 0);
if (check_pause) {
lcd.print(21 - seconds);
lcd.print(" ");
} else {
lcd.print(" ");
}
// lcd.write(0);
// lcd.write(4);
// lcd.print(seconds);
// lcd.print(" ");
if (webasto_fail) {
lcd.setCursor(0, 0);
lcd.print("FAIL FAIL |");
if (blink_LED_status)
lcd.backlight();
else
lcd.noBacklight();
} else {
lcd.backlight();
switch (burn_mode) {
case 0:
lcd.setCursor(0, 0);
lcd.print("IDLE |");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("BURNING UP|");
break;
case 2:
lcd.setCursor(0, 0);
if (overheating)
lcd.print("SLEEPING |");
else
lcd.print("WORKING |");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("BURN OFF |");
break;
}
}
old_water_temp = (int) water_temp;
old_exhaust_temp = (int) exhaust_temp;
old_fan_speed = (int) fan_speed;
old_fuel_need = (int) fuel_need;
}
void webasto_power() { //calculate webasto power and set fan and fuel
old_fan = fan_speed;
// fan_speed = 100 - mapf(water_temp, 40, temperature_target + 10, 30, 80); //water_temp,min water, max water, min fan, max fan
// fuel_need = constrain(fan_speed + 20, 30, 80);
// fan_speed = constrain(fan_speed + 10, 30, 80);
fan_speed = 80;
fuel_need = 50;
if (water_temp < 30) {
fan_speed = 60;
fuel_need = 30;
}
if (water_temp > 60) {
fan_speed = 70;
fuel_need = 40;
}
if (water_temp > 65) {
fan_speed = 60;
fuel_need = 30;
}
if (old_fan != fan_speed) {
seconds = 0;
check_pause = true;
}
}
void webasto() { // this will handle the combustion
static unsigned long timer;
if (millis() < timer) {
timer = millis();
}
static float temp_init_exhaust;
static float temp_init_water;
static int ignit_fail;
if (millis() > timer + 1000) { // every seconds, run this
timer = millis();
seconds++; // increment the seconds counter
//printData(seconds);
printLCD();
blink_LED_status = !blink_LED_status;
digitalWrite(blink_LED, blink_LED_status);
}
if (!webasto_fail) { // if everything's going fine
if ((burn_mode == 0 || burn_mode == 3) && burn) { // if the user wants a shower
// initiate the start sequence
burn_mode = 1;
seconds = 0;
temp_init_exhaust = exhaust_temp;// store the exhaust temperature before trying to start the fire
temp_init_water = water_temp;
}
if ((burn_mode == 1 || burn_mode == 2) && !burn) { // if the shower has ended
burn_mode = 3;
seconds = 0;
ignit_fail = 0;
}
if (ignit_fail > 3) { // if there was more than 3 attempts to start fire but all failed
webasto_fail = true;
burn_mode = 3;
shower = 0;
burn = 0;
fan_speed = 50;
water_pump_speed = HIGH;
}
} else { // if there has been a major failure, stop everything
shower = 0;
burn = 0;
ignit_fail = 0;
}
switch (burn_mode) {
case 0: { // everything is turned off in this mode
fan_speed = 0;
fuel_need = 0;
glow_time = 0;
lean_burn = 0;
}
break;
case 1: { // the fire starting sequence
if (seconds < 5) {
fuel_need = 0;
fan_speed = 60; //vent a chamber
}
if (seconds == 6) {
fuel_need = 90;
fan_speed = 40;
}
if (seconds == 8) {
fuel_need = 0; // stop and wait for it to spread all over the glow plug (important)
// don't forget that the fan is still spinning as it wasn't turned off
}
if (seconds == 10) {
glow_time = 12; // switch on the glow plug for 12 seconds so it ignites the fuel
}
if (seconds == 15) {
fuel_need = 30;
}
if (seconds > 17) { // the glow plug has just been turn of (7+12=19)
fan_speed = 50; // get some more air and restart pumping fuel slowly
fuel_need = 30;
}
if (seconds > 18) {
if (exhaust_temp - temp_init_exhaust > 3) { // exhaust temp raised a bit meaning fire has started
burn_mode = 2; // go to main burning mode and initialize variables
seconds = 0;
glow_time = 0;
ignit_fail = 0;
}
if (water_temp - temp_init_water > 2) { // water temp raised a bit meaning fire has started
burn_mode = 2; // go to main burning mode and initialize variables
seconds = 0;
glow_time = 0;
ignit_fail = 0;
}
if (seconds > 30) {
// the fire sequence didn't work, give it an other try
burn_mode = 0;
ignit_fail++;
}
}
break;
case 2:
{
webasto_power(); //calculate and set webasto power
if (water_temp >= temperature_target) { //if we reach max temp. make a sleep for webasto
overheating = true;
}
if (overheating && (water_temp < temperature_target - 5)) { //if temp is lower use start sequence
overheating = false;
burn_mode = 1;
}
if (overheating) {
fan_speed = 20;
fuel_need = 0;
}
if (!check_pause) {
if (!overheating && (exhaust_temp - exhaust_temp_sec[9]) > 10) { // if flame died
burn_mode = 1;
seconds = 0;
}
} else {
if (seconds > 20) { // wait 20 sec to establish exhaust temp after we change power
check_pause = false;
}
}
}
if (lean_burn) { // burn the remaining fuel in the chamber
fuel_need = 0;
fan_speed = 50;
}
}
break;
case 3: { // snuff out the fire, with just a little air to avoid fumes and backfire
fan_speed = 50;
fuel_need = 0;
glow_time = 0;
// if (seconds > 30) {
// fan_speed = 100;
// }
if (water_temp > water_temp_sec[9]) { // if water temp is raising, slow down the fan
fan_speed = fan_speed - 10;
if (fan_speed < 20) {
fan_speed = 20;
}
} else {
fan_speed = fan_speed - 10;
if (fan_speed > 100) {
fan_speed = 100;
}
}
if (seconds > 120 && exhaust_temp < exhaust_temp_sec[9]) {
// turn everything off if temperature is not raising
burn_mode = 0;
}
}
break;
}
// call every output function
fuel_pump();
burn_fan();
glow_plug();
}
void fuel_pump() {
static unsigned long timer;
if (millis() < timer) {
timer = millis();
}
static bool pulsing;
int pulse_lenght = 11;
if (fuel_need > 0) {
int impulse_delay = mapf(fuel_need, 1, 100, 1000, 90);
if (pulsing == 0 && millis() > timer + impulse_delay) {
timer = millis();
pulsing = 1;
}
if (pulsing == 1 && millis() > timer + pulse_lenght) {
timer = millis();
pulsing = 0;
}
} else {
pulsing = 0;
}
analogWrite(fuel_pump_pin, 255 * pulsing);
}
void burn_fan() {
// the webasto fan runs on 10v so we need to that into account : pwm average of 14v for 10v = 0.7, 255 * 0.7 = 179
int percent_map = mapf(fan_speed, 0, 100, 0, 179); // pwm average of 14v for 10v = 0.7, 255 * 0.7 = 179
analogWrite(burn_fan_pin, percent_map);
}
void glow_plug() { // just turn the plug on if glow_time > 0, and decrement glow_time every second
static unsigned long timer;
if (millis() < timer || glow_time == 0) {
timer = millis();
}
if (millis() < timer + glow_time * 1000) {
digitalWrite(glow_plug_pin, HIGH);
} else {
digitalWrite(glow_plug_pin, LOW);
glow_time = 0;
}
}
void water_pump() {
water_pump_speed = LOW;
if (water_temp > 20) {
water_pump_speed = 100;
}
if (water_temp >= run_pump_min) {
water_pump_speed = HIGH;
}
digitalWrite(water_pump_pin, water_pump_speed);
}
int get_temp(int index) {
int t_temp;
// DANGER. Dont wait for conversion makes sometimes wrong reads
sensor[index].setWaitForConversion(false);
sensor[index].requestTemperatures();
sensor[index].setWaitForConversion(true);
t_temp = sensor[index].getTempCByIndex(0);
if (t_temp > 200 || t_temp < -100) { //we check here a wrong read, and make a "correction"
if (index == water_input_index) {
t_temp = water_temp;
} else {
t_temp = exhaust_temp;
}
}
return t_temp;
}
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
// the perfect map fonction, with constraining and float handling
x = constrain(x, in_min, in_max);
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void loop() { // runs over and over again, calling the functions one by one
wdt_enable(WDTO_1S); //watchdog
temp_data();
control();
shower_void();
webasto();
wdt_reset();
}