This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
BrewManiac.ino
4711 lines (3998 loc) · 99.4 KB
/
BrewManiac.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
BrewManiac
created by Vito Tai
Copyright (C) 2015 Vito Tai
This soft ware is provided as-is. Use at your own risks.
You are free to modify and distribute this software without removing
this statement.
BrewManiac by Vito Tai is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
***********************************************************************/
#include <EEPROM.h>
#include <OneWire.h>
#include <PID_v1.h>
// *************************
//* Configuration
// *************************
// *************************
//* Hardware Configuration
// *************************
//*****************************
/**** supported board ******/
// UNOTEST is UNO, uses I2C LCD
#define UNOTEST 1
// MRE168 is MEGA, HD44780s LCD
#define MRE168 2
// Setting from default Open ArdBir
#define Pcb_ArdBir_DanielXan 3
// BOARD definition.
#define BOARD Pcb_ArdBir_DanielXan
/**** I2C LCD ******/
// not usingI2C_LCD by default
#define I2C_LCD false
// modify LCD hardware connection following
#define LCD_I2C_ADDR 0x3F
/**** Wireless module connection ******/
/**** Bluetooth no longer works. ******/
// Wireless connection
#define UseSoftwareSerial false
#if UseSoftwareSerial == true
const byte SoftwareSerialRx = 10;
const byte SoftwareSerialTx = 11;
#endif
#if UseSoftwareSerial == true
#define WiFiSerialBaudRate 38400
#else
#define WiFiSerialBaudRate 115200
#endif
/* this option is not used if UseSoftwareSerial == true */
#define WirelessHardwarePort Serial
/**** PIN setup ******/
// PIN setup
const byte SensorPin=7;
#define PumpControlPin 6
#define BuzzControlPin 8
#define HeatControlPin 9
#if BOARD == MRE168
#define ButtonUpPin A3
#define ButtonDownPin A2
#define ButtonStartPin A1
#define ButtonEnterPin A0
// use Serial1
#define UseSoftwareSerial false
#define WirelessHardwarePort Serial1
#endif
#if BOARD == UNOTEST
// overwrite I2C_LCD setting
#define I2C_LCD true
#define ButtonUpPin A2
#define ButtonDownPin A3
#define ButtonStartPin A0
#define ButtonEnterPin A1
#endif
#if BOARD == Pcb_ArdBir_DanielXan
#define ButtonUpPin A2
#define ButtonDownPin A3
#define ButtonStartPin A0
#define ButtonEnterPin A1
#endif
// *************************
//* software Configuration
// *************************
/** WirelessConnection values */
#define WirelessNone 0
#define WirelessBluetooth 1
#define WirelessWiFi 2
//***********************************************************
/// !!!! NOTE. Bluetooth might not work normally
//***********************************************************
#define WirelessConnection WirelessWiFi
/** Functions */
#define SimpleMashStep true
// manual control over pump during mash
#define MANUAL_PUMP_MASH true
// DELAY start
#define NoDelayStart true
#define SupportAutoModeRecovery true
#define SupportManualModeCountDown true
#define NoWhirlpool true
#define ElectronicOnly true
// bluetooth related setting
#define BT_AutoBaudRate true
#define CHANG_BAUDRATE true
#define BT_MODULE_INITIALIZATION true
#define BT_Menu false
//debug setting
#define SerialDebug false
#define FakeHeating false
#define DEVELOP_SETTING_VALUE false
// *************************
//* advanced settings
// *************************
#define BT_TemperatureReportPeriod 10000
#define ButtonPressedDetectMinTime 125 // in ms
#define ButtonLongPressedDetectMinTime 1500 // in ms
#define ButtonContinuousPressedDetectMinTime 1000 // in ms
#define ButtonContinuousPressedTrigerTime 150 // in ms
#define ButtonFatFingerTolerance 50 // in ms
// *************************
//* end of software configuration
// *************************
#if WirelessConnection == WirelessNone
#define WiFiSupported false
#define BluetoothSupported false
#define WirelessSupported false
#endif
#if WirelessConnection == WirelessBluetooth
#define WiFiSupported false
#define BluetoothSupported true
#define WirelessSupported true
#endif
#if WirelessConnection == WirelessWiFi
#define WiFiSupported true
#define BluetoothSupported false
#define WirelessSupported true
#endif
#if UseSoftwareSerial != true
#define SerialDebug false
#endif
#if FakeHeating == true
#define USE_DS18020 false
#else
// must be true
#define USE_DS18020 true
#endif
// bluetooth related options
#define NoPrint true
#define BT_STRICT true
#if BT_MODULE_INITIALIZATION != true
#define BT_Menu false
#endif
#if I2C_LCD == true
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#else
#include <LiquidCrystal.h>
#endif
#if UseSoftwareSerial == true
#include <SoftwareSerial.h>
#endif
#if I2C_LCD == true
LiquidCrystal_I2C lcd(LCD_I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#else
LiquidCrystal lcd(A4, A5, 2, 3, 4, 5);
#endif
#if NoPrint == true
#include "mystrlib.h"
#endif
//{debug
//overrite definition to save some memory
#if BOARD == UNOTEST
#define FakeHeating true
#define USE_DS18020 false
#define SupportAutoModeRecovery false
#define SupportManualModeCountDown false
// 32k flash is limitted
#define BT_AutoBaudRate false
#define BT_MODULE_INITIALIZATION false
#define BT_Menu false
#endif //over write stting for test board
//}debug
// *************************
//* global variables
// *************************
unsigned long gSystemStartTime; // in mili seconds
unsigned long gCurrentTimeInMS; // in mili seconds
unsigned long gCurrentTimeInSec; // in seconds
double gCurrentTemperature;
double gSettingTemperature;
double gBoilStageTemperature;
byte gBoilHeatOutput;
// the pump/heat on/off is requested by user
// real pump/heat on/off depends on the temperature
// and parameter setting
boolean gIsHeatOn;
boolean gIsPumpOn;
boolean gIsUseFahrenheit;
boolean _isEnterPwm;
boolean gIsTemperatureReached;
boolean gIsPaused;
#if MANUAL_PUMP_MASH == true
boolean gManualPump;
#endif
// *************************
//* function declaration
// *************************
typedef void (*SetupFunc)(void);
typedef void (*EventHandlerFunc)(byte);
#if 0 //opt-code
void switchApplication(SetupFunc setup,EventHandlerFunc handler);
#else
void switchApplication(byte screenId);
#endif
void backToMain(void);
void setEventMask(byte);
// main screen
void mainSetup(void);
void mainEventHandler(byte);
// setup menu
void menuSetup(void);
void menuEventHandler(byte);
void settingPidSetup(void);
void settingPidEventHandler(byte);
void settingUnitSetup(void);
void settingUnitEventHandler(byte);
void settingAutoSetup(void);
void settingAutoEventHandler(byte);
// manual mode
void manualModeSetup(void);
void manualModeEventHandler(byte);
//auto mode
void autoModeSetup(void);
void autoModeEventHandler(byte);
#if BT_Menu == true
void btMenuSetup(void);
void btMenuEventHandler(byte);
#endif
#define ConvertF2C(d) (((d)-32)/1.8)
#define ConvertC2F(d) (((d)*1.8)+32)
#if WirelessSupported == true
//Stage
#define StageMashIn 0
// 1 -6 rest,
// 7 mashout
#define StageBoil 8
#define StageCooling 9
#define StageWhirlpool 10
#define StageDelayStart 11
#define StageManualMode 100
#define StageIdleScreen 101
#define StageSetting 102
//Event
// Timeup usually means another stage or notification, ignore it
#define RemoteEventTemperatureReached 1
#define RemoteEventAddMalt 2
#define RemoteEventRemoveMalt 3
#define RemoteEventIodineTest 4
#define RemoteEventPause 5
#define RemoteEventResume 6
#define RemoteEventAddHop 7
#define RemoteEventPwmOn 8
#define RemoteEventPwmOff 9
#define RemoteEventBoilFinished 10
#define RemoteEventBrewFinished 99
void btReportCurrentStage(byte stage);
void btReportEvent(byte event);
void btReportSettingTemperature(void);
void btReportPwm(void);
#endif
// *************************
//* Screens
// *************************
#define ButtonPressedEventMask 0x1
#define TemperatureEventMask (0x1 <<1)
#define TimeoutEventMask (0x1 <<2)
#define PumpRestEventMask (0x1 <<3)
typedef struct _CScreen{
SetupFunc setup;
EventHandlerFunc eventHandler;
}CScreen;
#define MAIN_SCREEN 0
#define SETUP_SCREEN 1
#define PID_SETTING_SCREEN 2
#define UNIT_SETTING_SCREEN 3
#define AUTO_SETTING_SCREEN 4
#define MANUAL_MODE_SCREEN 5
#define AUTO_MODE_SCREEN 6
#if BT_Menu == true
#define BT_MENU_SCREEN 7
#endif
const CScreen allScreens[] =
{{
&mainSetup,
&mainEventHandler,
},
{
&menuSetup,
&menuEventHandler,
},
{
&settingPidSetup,
&settingPidEventHandler,
},
{
&settingUnitSetup,
&settingUnitEventHandler,
},
{
&settingAutoSetup,
&settingAutoEventHandler,
},
{
&manualModeSetup,
&manualModeEventHandler,
},
{
&autoModeSetup,
&autoModeEventHandler,
},
#if BT_Menu == true
{
&btMenuSetup,
&btMenuEventHandler,
}
#endif
};
byte _currentEventMask;
//#define setEventMask(a) _currentEventMask=(a)
void setEventMask(byte mask)
{
_currentEventMask=mask;
}
// *************************
//* includes, follow Arduino conveniention
// *************************
#include "buzz.h"
// *************************
//* EEPROM map
// *************************
#include "string.h"
#include "ui.h"
#include "ps.h"
// *************************
//* Bluetooth and wireless
// *************************
#if BluetoothSupported == true
#include "bt.h"
#endif
#if WiFiSupported == true
#include "wi.h"
#endif
// *************************
//* Time related function
// *************************
unsigned long _gTimeout;
unsigned long _gAuxTimeout;
boolean _isAuxTimeout;
#define IsAuxTimeout _isAuxTimeout
/*
unsigned long getTimeLeft(void)
{
if(_gTimeout ==0) return 0;
return(_gTimeout - gCurrentTimeInMS);
}
*/
unsigned long tmGetRemainingTime(void)
{
if(_gTimeout ==0) return 0;
//else
unsigned long ret=(_gTimeout - gCurrentTimeInMS);
return ret;
}
unsigned long tmPauseTimer(void)
{
//[TODO:] there are chance that _gTimeout is ZERO
// after 50 days...
if(_gTimeout ==0) return 0;
//else
unsigned long ret=(_gTimeout - gCurrentTimeInMS);
_gTimeout=0;
_gAuxTimeout=0;
return ret;
}
void tmSetAuxTimeoutAfter(unsigned long duration)
{
_gAuxTimeout = gCurrentTimeInMS + duration;
}
void tmSetTimeoutAfter(unsigned long duration)
{
_gTimeout = gCurrentTimeInMS + duration;
#if 0
Serial.print(F("setTimeoutAfter:"));
Serial.print(duration);
Serial.print(F(",current="));
Serial.print(gCurrentTimeInMS);
Serial.print(F(",expires="));
Serial.println(_gTimeout);
#endif
}
void tmInitialize(void)
{
gSystemStartTime = millis();
}
boolean tmTiming(void)
{
gCurrentTimeInMS=millis();
gCurrentTimeInSec = (gCurrentTimeInMS - gSystemStartTime) / 1000;
if(_gTimeout
&& _gTimeout <= gCurrentTimeInMS)
{
_gTimeout = 0;
_isAuxTimeout=false;
return true;
}
if(_gAuxTimeout
&& _gAuxTimeout <= gCurrentTimeInMS)
{
_gAuxTimeout = 0;
_isAuxTimeout=true;
return true;
}
return false;
}
// *************************
//* button related function
// *************************
#define ButtonUpMask 0x01
#define ButtonDownMask (0x01 << 1)
#define ButtonStartMask (0x01 << 2)
#define ButtonEnterMask (0x01 << 3)
unsigned char _testButtunStatus;
unsigned long _buttonChangeTime;
unsigned long _continuousPressedDectedTime;
boolean _continuousPressedDetected;
boolean gLongPressed;
byte gButtonPressed=0;
unsigned long _oneFigerUp;
void btnInitialize(void)
{
pinMode (ButtonUpPin, INPUT_PULLUP);
pinMode (ButtonDownPin, INPUT_PULLUP);
pinMode (ButtonStartPin, INPUT_PULLUP);
pinMode (ButtonEnterPin, INPUT_PULLUP);
gButtonPressed=0;
_testButtunStatus=0;
}
#define btnIsUpPressed (gButtonPressed == ButtonUpMask)
#define btnIsDownPressed (gButtonPressed == ButtonDownMask)
#define btnIsEnterPressed (gButtonPressed == ButtonEnterMask)
#define btnIsStartPressed (gButtonPressed == ButtonStartMask)
#define btnIsStartLongPressed ((gButtonPressed == ButtonStartMask) && gLongPressed)
#define btnIsEnterLongPressed ((gButtonPressed == ButtonEnterMask) && gLongPressed)
#define btnIsUpContinuousPressed (gButtonPressed == (ButtonUpMask<<4))
#define btnIsDownContinuousPressed (gButtonPressed == (ButtonDownMask<<4))
#define isExactButtonsPressed(mask) ((mask) == gButtonPressed)
#define isButtonsPressed(mask) ((mask) & gButtonPressed)
#if SerialDebug != true
#define BUTTON_DEBUG false
#endif
#define BUTTON_DEBUG false
#if WirelessSupported == true
boolean _virtualButtonPressed=false;
void virtualButtonPress(byte mask,boolean longPressed)
{
gButtonPressed = mask;
gLongPressed = longPressed;
_virtualButtonPressed=true;
}
#endif
boolean btnReadButtons(void)
{
#if WirelessSupported == true
if(_virtualButtonPressed)
{
_virtualButtonPressed = false;
return true;
}
#endif
unsigned char buttons=0;
if (digitalRead(ButtonUpPin) == 0)
{
buttons |= ButtonUpMask;
}
if (digitalRead(ButtonDownPin) == 0)
{
buttons |= ButtonDownMask;
}
if (digitalRead(ButtonEnterPin) == 0)
{
buttons |= ButtonEnterMask;
}
if (digitalRead(ButtonStartPin) == 0)
{
buttons |= ButtonStartMask;
}
if(buttons==0)
{
if(_testButtunStatus ==0) return false;
unsigned long duration=gCurrentTimeInMS - _buttonChangeTime;
#if BUTTON_DEBUG == true
Serial.print(F("pressed:"));
Serial.print(_testButtunStatus);
Serial.print(F(","));
Serial.print(buttons);
Serial.print(F("for:"));
Serial.println(duration);
#endif
if(duration > ButtonPressedDetectMinTime)
{
if(duration > ButtonLongPressedDetectMinTime) gLongPressed=true;
else gLongPressed =false;
gButtonPressed = _testButtunStatus;
_testButtunStatus =0;
_continuousPressedDetected = false;
#if BUTTON_DEBUG == true
Serial.print(gButtonPressed);
if (gLongPressed) Serial.println(F(" -Long Pressed"));
else Serial.println(F(" -Pressed"));
#endif
return true;
}
#if BUTTON_DEBUG == true
Serial.println(F("Not Pressed"));
#endif
_testButtunStatus =0;
_continuousPressedDetected = false;
return false;
}
// buttons is not ZERO afterward
if(buttons == _testButtunStatus) // pressing persists
{
if(_continuousPressedDetected )
{
//if duration exceeds a trigger point
if( (gCurrentTimeInMS - _continuousPressedDectedTime) > ButtonContinuousPressedTrigerTime)
{
_continuousPressedDectedTime=gCurrentTimeInMS;
#if BUTTON_DEBUG == true
Serial.print(gButtonPressed);
Serial.print(F(" -Continues 2 pressed:"));
Serial.println(gCurrentTimeInMS);
#endif
return true;
}
}
else
{
unsigned long duration=gCurrentTimeInMS - _buttonChangeTime;
if(duration > ButtonContinuousPressedDetectMinTime)
{
_continuousPressedDetected=true;
_continuousPressedDectedTime=gCurrentTimeInMS;
// fir the first event
gButtonPressed = buttons << 4; // user upper 4bits for long pressed
#if BUTTON_DEBUG == true
Serial.print(gButtonPressed);
Serial.print(F(" -Continues detected pressed:"));
Serial.println(gCurrentTimeInMS);
#endif
return true;
}
}
}
else // if(buttons == _testButtunStatus)
{
// for TWO buttons event, it is very hard to press and depress
// two buttons at exactly the same time.
// so if new status is contains in OLD status.
// it might be the short period when two fingers are leaving, but one is detected
// first before the other
// the case might be like 01/10 -> 11 -> 01/10
// just handle the depressing case: 11-> 01/10
if((_testButtunStatus & buttons)
&& (_testButtunStatus > buttons))
{
if(_oneFigerUp ==0)
{
_oneFigerUp = gCurrentTimeInMS;
// skip this time
return false;
}
else
{
// one fat finger is dected
if( (gCurrentTimeInMS -_oneFigerUp) < ButtonFatFingerTolerance)
{
return false;
}
}
#if BUTTON_DEBUG == true
Serial.println(F("Failed fatfinger"));
#endif
}
// first detect, note time to check if presist for a duration.
_testButtunStatus = buttons;
_buttonChangeTime = gCurrentTimeInMS;
_oneFigerUp = 0;
#if BUTTON_DEBUG == true
Serial.println(F("Attempted"));
#endif
}
return false;
}
// *************************
//* tempture related function
// ****************************
// temperature event
#if USE_DS18020 == true
OneWire ds(SensorPin);
boolean _isConverting=false;
byte _sensorData[9];
void dsInizialize(void)
{
ds.reset();
ds.skip();
}
#endif
void tpInitialize(void)
{
gCurrentTemperature = 19.99;
gBoilStageTemperature=readSetting(PS_BoilTemp);
}
// the following code basically comes from Open ArdBir
#define DSCMD_CONVERT_T 0x44
#define DSCMD_READ_SCRATCHPAD 0xBE
void tpReadTemperature(void)
{
#if USE_DS18020 == true
dsInizialize();
if (_isConverting == false)
{
// start conversion and return
ds.write(DSCMD_CONVERT_T, 0);
_isConverting = true;
return;
}
// else if convert start
//if (_isConverting)
//{
// check for conversion if it isn't complete return if it is then convert to decimal
byte busy = ds.read_bit();
if (busy == 0) return;
// reset & "select" again
dsInizialize();
// request data
ds.write(DSCMD_READ_SCRATCHPAD);
for ( byte i = 0; i < 9; i++) { // with crc we need 9 bytes
_sensorData[i] = ds.read();
}
/* add this routine for crc version */
if ( OneWire::crc8(_sensorData, 8) != _sensorData[8]) { //if checksum fails start a new conversion right away
// reissue convert command
dsInizialize();
ds.write(DSCMD_CONVERT_T, 0);
_isConverting = true;
return;
}
// data got!
unsigned int raw = (_sensorData[1] << 8) + _sensorData[0];
gCurrentTemperature = (raw & 0xFFFC) * 0.0625;
//apply calibration
gCurrentTemperature += ((float)(readSetting(PS_Offset) - 50) / 10.0);
_isConverting = false;
//}
#endif
}
void pumpLoadParameters(void);
void heatLoadParameters(void);
void loadBrewParameters(void)
{
heatLoadParameters();
pumpLoadParameters();
}
// *************************
//* heating related function
// *************************
#if ElectronicOnly != true
boolean _isUseGas;
#endif
boolean _physicalHeattingOn;
byte _heatWindowSize;
unsigned long _windowStartTime;
//double pidInput;
//double pidSetpoint;
//pidInput=gCurrentTemperature;
//pidSetpoint=gSettingTemperature;
#define pidInput gCurrentTemperature
#define pidSetpoint gSettingTemperature
double pidOutput;
PID thePID(&pidInput,&pidOutput,&pidSetpoint,100,40,0,DIRECT);
void heatInitialize(void)
{
thePID.SetMode(AUTOMATIC);
_physicalHeattingOn=false;
gIsHeatOn=false;
}
// the should be call before REAL action instead of system startup
void heatLoadParameters(void)
{
thePID.SetTunings((double)readSetting(PS_kP)-100.0,
(double)((readSetting(PS_kI)-100.0) / 250.0),
(double)readSetting(PS_kD)-100.0);
_heatWindowSize = readSetting(PS_WindowSize);
thePID.SetSampleTime((int)readSetting(PS_SampleTime) * 250);
#if ElectronicOnly != true
_isUseGas =readSetting(PS_UseGas);
#endif
gBoilStageTemperature=(float)readSetting(PS_BoilTemp);
gBoilHeatOutput=readSetting(PS_BoilHeat);
#if 0 // SerialDebug == true
Serial.print("gBoilStageTemperature=");
Serial.println(gBoilStageTemperature);
#endif
}
#if FakeHeating == true
unsigned long lastTime;
#endif
void heatPhysicalOn(void)
{
if(!_physicalHeattingOn)
{
digitalWrite (HeatControlPin, HIGH);
_physicalHeattingOn=true;
#if FakeHeating == true
lastTime = gCurrentTimeInMS;
#endif
}
}
void heatPhysicalOff(void)
{
if(_physicalHeattingOn)
{
digitalWrite (HeatControlPin, LOW);
_physicalHeattingOn=false;
}
}
void heatOff(void)
{
gIsHeatOn = false;
uiHeatingStatus(HeatingStatus_Off);
heatPhysicalOff();
}
boolean _isPIDMode;
void heatOn(void)
{
gIsHeatOn = true;
_isPIDMode =true;
// should run through heating algorithm first
// so that the correct symbol can be shown
_windowStartTime=millis();
// uiHeatingStatus(HeatingStatus_On_PROGRAM_OFF);
heatPhysicalOn();
uiHeatingStatus(HeatingStatus_On);
}
void heatProgramOff(void)
{
heatOff();
uiHeatingStatus(HeatingStatus_On_PROGRAM_OFF);
}
float round025(float num)
{
int intPart=(int)num;
return (float)intPart + ((int)((num - (float)intPart)*100.0)/25)*0.25;
}
void heatThread(void)
{
if(! gIsHeatOn) return;
#if FakeHeating == true
if(_physicalHeattingOn){
gCurrentTemperature += (gCurrentTimeInMS - lastTime) * 0.0005;
lastTime = gCurrentTimeInMS;
}
#endif
//[TODO:] remove redaundancy?
//pidInput=gCurrentTemperature;
//pidSetpoint=gSettingTemperature;
// only when heat is on,
// do the code execute to determine
// if physical heat should be ON or OFF
// the following code comes directly from Open ArdBir
float DeltaPID;
float Rapporto, Delta, IsteresiProporzionale;
#if ElectronicOnly != true
if (_isUseGas)
{
DeltaPID =(float)readSetting(PS_Hysteresi) / 10.0;
IsteresiProporzionale = DeltaPID / pidInput;
thePID.SetSampleTime(8000);
_heatWindowSize = 160;
}
else
#endif
{
DeltaPID = 3.50;
if (pidInput >= gBoilStageTemperature - DeltaPID)
DeltaPID = 1;
IsteresiProporzionale = 0.0;