Repository navigation
Expand file tree
/
Copy pathSerial_003.ino
More file actions
870 lines (725 loc) · 20.4 KB
/
Copy pathSerial_003.ino
File metadata and controls
870 lines (725 loc) · 20.4 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
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
/*
* Miriam control
*
* Miriam is an isothermal amplification unit capable of real time detection that supports a 96 well PCR plate
* This firmware allows the control over serial port string messages
*
* The basic configuration of the board is:
*
* - Temperature Sensors pins: 23,24,25,26 and sensor 5 not using but connect to 19
* - Heater pins: 9,10,1,13 and heat 5 not using but connect to 14
* - LEDS: 22,2,12,11
*
* (c) 2016 Juho Terrijarvi juho@miroculus.com, Miroculus Inc.
* (c) 2014 Juanjo Tara j.tara@arduino.cc, Arduino Verkstad AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <PID_v1.h>
#include <math.h>
#include "MyStatusLed.h"
#define FIRMWARE_VERSION "2.0.0" // first release 2.0.0
//Selector ping
#define SEL_1 46 // Selector ping 1
#define SEL_2 47 // Selector ping 2
#define SEL_3 48 // Selector ping 3
#define SEL_4 49 // Selector ping 4
//Temperature Sensors pin
#define TH_BOX A11 // Temperature Sensor 2
#define TH_MIDDLEBED_2 A9 // Temperature Sensor 3
#define TH_UPPERBED A10 // Temperature Sensor 1
#define TH_EXTRA A8 // Temperature Sensor ex
#define OPTIMAL_MIDDLE_TEMP 63
#define OPTIMAL_UPPER_TEMP 80
#define OPTIMAL_EXTRA_TEMP 63
#define IDDLE_MIDDLE_TEMP 30
#define IDDLE_UPPER_TEMP 20
//#define Threshold_temp 0.5
//Heater pins
#define HEAT_MIDDLE 2 // Heat 1
#define HEAT_UPPER 3 // Heat 2
#define HEAT_EXTRA 5 // Heat 3
//LED Sensors pin
#define PANEL_LED 22
//Speaker pin
#define SOUND 7
#define STATUS_LED_PIN 6
// manufacturer data for episco NCP18XH103F03RB 10k thermistor
// simply delete this if you don't need it
// or use this idea to define your own thermistors
#define NCP18XH103F03RB 3380.0f, 298.15f, 10000.0f // B,T0,R0
// temperature calculation correction - linear fit coefficients. Applied only for t in Celsius
#define LR_CORR_SLOPE 0.96 //0.8433
#define LR_CORR_INTERCEPT 2.6 //4.2213
// STATES FOR STATE MACHINE
#define INIT 'I'
#define CANCEL 'C'
#define HEAT_BOARDS 'H'
#define INFO 'i'
#define SET_TEMP_UPPER 'U'
#define SET_TEMP_MIDDLE 'M'
#define SET_TEMP_EXTRA 'E'
#define READ_ASSAY 'R'
#define PLAY_SOUND 's'
#define STATUS_LED_ON 'l'
#define STATUS_LED_OFF 'o'
#define SET_BOX_THR 'T'
#define MELT_HEAT 'W'
#define MELT_INIT 'w'
#define VERSION 'V'
int MUL[6] = {
A0, A1, A2, A3, A4, A5}; // These are the 6 analog inputs. One per multiplexer.
byte controlLPins[] = {
B00000000,
B00000001,
B00000010,
B00000011,
B00000100,
B00000101,
B00000110,
B00000111,
B00001000,
B00001001,
B00001010,
B00001011,
B00001100,
B00001101,
B00001110,
B00001111};
// Holds incoming values from Multiplexer. This is our 96 well plate.
int sensorValues[8][12] = {};
//Define the aggressive and conservative Tuning Parameters
double aggKp = 4, aggKi = 0.4, aggKd = 4; // d2
double consKp = 1, consKi = 0.05, consKd = 0.25; //1, 0.25
double Setpoint;
//Define Variables we'll be connecting to PID's
double Input_MIDDLE, Output_MIDDLE, Setpoint_MIDDLE, Input_UPPER, Input_BOX, Setpoint_UPPER, Output_UPPER, Input_MIDDLE_2, Input_EXTRA, Output_EXTRA, Setpoint_EXTRA;
double threshold_BOX = 60;
//PID Controller for Heater
PID PID_MIDDLE(&Input_MIDDLE_2, &Output_MIDDLE, &Setpoint_MIDDLE, consKp, consKi, consKd, DIRECT);
PID PID_UPPER(&Input_UPPER, &Output_UPPER, &Setpoint_UPPER, consKp, consKi, consKd, DIRECT);
PID PID_EXTRA(&Input_EXTRA, &Output_EXTRA, &Setpoint_EXTRA, consKp, consKi, consKd, DIRECT);
// enumerating 3 major temperature scales
enum
{
T_KELVIN = 0,
T_CELSIUS,
T_FAHRENHEIT
};
byte states[] = {
INIT, CANCEL, HEAT_BOARDS, INFO, SET_TEMP_UPPER, SET_TEMP_MIDDLE, SET_TEMP_EXTRA, READ_ASSAY, PLAY_SOUND, STATUS_LED_ON, STATUS_LED_OFF, SET_BOX_THR, MELT_HEAT, MELT_INIT, VERSION};
// serial data
String inputString = " "; // a string to hold incoming data
String parameters = " ";
String dataString = " ";
boolean stringComplete = false; // whether the string is complete
boolean LED = false;
boolean HEAT_ON = false;
boolean MELT_ON = false;
int state = INIT;
char previous_state = INIT;
int defaultState = INIT;
bool heat_alarm = false;
//bool status_led_state_on=false;
//bool status_led_busy=false;
MyStatusLed status_led(STATUS_LED_PIN, 500, 500);
void setup()
{
DDRL = DDRL | B00001111; // this sets pins D3 to D7 as outputs
// Temperature Sensors pin
pinMode(TH_BOX, INPUT);
pinMode(TH_MIDDLEBED_2, INPUT);
pinMode(TH_UPPERBED, INPUT);
pinMode(TH_EXTRA, INPUT);
// Heater pins
pinMode(HEAT_MIDDLE, OUTPUT);
pinMode(HEAT_UPPER, OUTPUT);
pinMode(HEAT_EXTRA, OUTPUT);
//Panel LEDS
pinMode(PANEL_LED, OUTPUT);
// pinMode(STATUS_LED,OUTPUT);
// Read temperature sensors
ReaderSensorTemp();
// Set degrees for PID algorithm
Setpoint_MIDDLE = OPTIMAL_MIDDLE_TEMP;
Setpoint_UPPER = OPTIMAL_UPPER_TEMP;
Setpoint_EXTRA = OPTIMAL_EXTRA_TEMP;
// input string needs space
inputString.reserve(30);
inputString = "";
parameters = "";
Serial.begin(9600); // Begin the serial monitor at 9600bps
Serial.println(F("Hope your day is nice and shiny!"));
}
void loop()
{
// print the string when a newline arrives:
if (stringComplete)
{
// remove carriage returns and new lines
inputString.replace("\n", "");
inputString.replace("\r", "");
Serial.println(inputString);
// it there is anything to do with it, do it!
if (isState(inputString[0]))
{
previous_state = state;
state = inputString[0];
parameters = inputString.substring(2);
}
// clear the string:
inputString = "";
stringComplete = false;
}
//Calculate PID for heaters
if (HEAT_ON)
Calculate_Heat();
if (MELT_ON)
Calculate_Melt();
//Calculate_Heat();
status_led.Update();
// if (status_led_on)
// state machine
switch (state)
{
case INIT:
previous_state = state;
state = 'I';
break;
case MELT_INIT:
HEAT_ON = false;
MELT_ON = false;
Output_MIDDLE = 100;
Output_UPPER = 100;
Output_EXTRA = 100;
computePIDs();
Serial.println(F("INIT MELT$"));
//turn the PID's off
// PID_MIDDLE.SetMode(MANUAL);
// PID_UPPER.SetMode(MANUAL);
// PID_EXTRA.SetMode(MANUAL);
state = defaultState;
break;
case CANCEL:
HEAT_ON = false;
MELT_ON = false;
Output_MIDDLE = 0;
Output_UPPER = 0;
Output_EXTRA = 0;
Serial.println(F("Cancel$"));
//delay(4000);
//turn the PID's off
PID_MIDDLE.SetMode(MANUAL);
PID_UPPER.SetMode(MANUAL);
PID_EXTRA.SetMode(MANUAL);
//Setpoint_MIDDLE = IDDLE_MIDDLE_TEMP;
//Setpoint_UPPER = IDDLE_UPPER_TEMP;
status_led.SwitchMode(LED_OFF);
state = defaultState;
break;
case HEAT_BOARDS:
HEAT_ON = true;
heat_alarm = true;
status_led.SwitchMode(LED_BLINK);
Serial.println(F("HEATING BOARDS$"));
//turn the PID's on
PID_MIDDLE.SetMode(AUTOMATIC);
PID_UPPER.SetMode(AUTOMATIC);
PID_EXTRA.SetMode(AUTOMATIC);
state = defaultState;
break;
case MELT_HEAT:
MELT_ON = true;
// heat_alarm = true;
heat_alarm = false;
Serial.println(F("MELTING BOARDS$"));
//turn the PID's on
PID_MIDDLE.SetMode(AUTOMATIC);
PID_UPPER.SetMode(AUTOMATIC);
PID_EXTRA.SetMode(AUTOMATIC);
state = defaultState;
break;
case VERSION:
// Serial.print(F("firmware_version: "));
Serial.print(FIRMWARE_VERSION);
Serial.println("$");
state = defaultState;
break;
case INFO:
Serial.println(String(Output_MIDDLE) + "," + String(Output_UPPER) + "," +
String(analogRead(TH_UPPERBED)) + "," + String((analogRead(TH_MIDDLEBED_2))) + "," +
String(Temperature(TH_UPPERBED, T_CELSIUS, NCP18XH103F03RB, 10000.0f)) + "," +
String(Temperature(TH_MIDDLEBED_2, T_CELSIUS, NCP18XH103F03RB, 10000.0f)) + "," +
String(Output_EXTRA) + "," +
String((analogRead(TH_EXTRA))) + "," + String((analogRead(TH_BOX))) + "," +
// String(Temperature(TH_EXTRA,T_CELSIUS,NCP18XH103F03RB,10000.0f)) + "," +
String(Temperature(TH_EXTRA, T_CELSIUS, NCP18XH103F03RB, 10000.0f)) + "," +
String(Temperature(TH_BOX, T_CELSIUS, NCP18XH103F03RB, 10000.0f)) + "$");
// 0: output_M,
// 1: output_U
// 2: T_U
// 3: T_M
// 4: T_U_C
// 5: T_M_C
// 6: output_E
// 7: T_E
// 8: T_Box
// 9: T_E_C
// 10: T_Box_C
state = defaultState;
break;
case SET_TEMP_UPPER:
Setpoint_UPPER = parameters.toFloat();
Serial.print(F("NEW UPPER TEMP:"));
Serial.print(Setpoint_UPPER);
Serial.println("$");
state = defaultState;
break;
case SET_TEMP_MIDDLE:
Setpoint_MIDDLE = parameters.toFloat();
Serial.print(F("NEW MIDDLE TEMP:"));
Serial.print(Setpoint_MIDDLE);
Serial.println("$");
state = defaultState;
break;
case SET_TEMP_EXTRA:
Setpoint_EXTRA = parameters.toFloat();
Serial.print(F("NEW EXTRA TEMP:"));
Serial.print(Setpoint_EXTRA);
Serial.println("$");
state = defaultState;
break;
case READ_ASSAY:
Serial.print(F("READ ASSAY"));
Read_Assay();
state = defaultState;
// Read_Assay calls display_data() which eventually prints "$\n"
break;
case PLAY_SOUND:
play_sound();
state = defaultState;
Serial.println(F("$"));
break;
case STATUS_LED_ON:
status_led.SwitchMode(LED_ON);
Serial.println(F("Status LED on$"));
state = defaultState;
break;
case STATUS_LED_OFF:
status_led.SwitchMode(LED_OFF);
Serial.println(F("Status LED off$"));
state = defaultState;
break;
case SET_BOX_THR:
threshold_BOX = parameters.toFloat();
Serial.print(F("NEW TEMP THRESHOLD:"));
Serial.print(threshold_BOX);
Serial.println("$");
state = defaultState;
break;
}
}
void play_sound()
{
tone(SOUND, 3000, 5000);
delay(1000);
tone(SOUND, 1000, 5000);
delay(1000);
tone(SOUND, 2000, 5000);
delay(1000);
tone(SOUND, 500, 5000);
delay(1000);
noTone(SOUND);
delay(1000);
//tone(piezoPin, 1000, 500);
//delay(1000);
}
void setPin(int outputPin)
{
// This selects the multiplexer state on all multiplexers. Since we use the
// multiplexer only for reading the sensors, this picks the input to the
// multiplexer from channel 0 through channel 15 (inclusive) to be output on
// the output pin of the multiplexer.
PORTL = controlLPins[outputPin];
}
void Read_Assay()
{
//set the PIDs to zero to boost LED power
// Output_MIDDLE = 0;
// Output_UPPER = 0;
// Output_EXTRA = 0;
// computePIDs();
// digitalWrite(22,HIGH);
//// analogWrite(4,125);
// delay(500);
const int N_SAMPLES = 10;
// take N_SAMPLES samples for measurement
for (int j = 0; j < N_SAMPLES; j++)
{
// Iterate through the channels on the multiplexer.
for (int i = 0; i < 16; i++)
{
setPin(i); // Choose an input pin all multiplexers.
digitalWrite(22, HIGH); // Turn on LEDs.
delayMicroseconds(500); // Wait.
ReadAssay(i); // Read all analog inputs, add into `sensorValues`.
digitalWrite(22, LOW); // Turn off LEDs.
delayMicroseconds(50); // Wait.
}
// delay(1000);
}
// digitalWrite(22,LOW);
// analogWrite(4,0);
//Calculate the average of the measurements for each sample
for (int k = 0; k < 8; k++)
{
for (int l = 0; l < 12; l++)
{
sensorValues[k][l] /= N_SAMPLES;
}
}
displayData();
//Empty the array
for (int k = 0; k < 8; k++)
{
for (int l = 0; l < 12; l++)
{
sensorValues[k][l] = 0;
}
}
//Empty possibly stored charge
//take 15 samples for measurement
for (int j = 0; j < 15; j++)
{
for (int i = 0; i < 16; i++)
{
setPin(i); // choose an input pin all multiplexers
for (int k = 0; k < 5; k++)
{
ReadAssayEmpty(i);
}
}
delay(10);
}
}
void ReadAssayEmpty(int i)
{
for (int j = 0; j < 6; j++)
{
switch (i)
{
case 0:
analogRead(MUL[j]);
break;
case 1:
analogRead(MUL[j]);
break;
case 2:
analogRead(MUL[j]);
break;
case 3:
analogRead(MUL[j]);
break;
case 4:
analogRead(MUL[j]);
break;
case 5:
analogRead(MUL[j]);
break;
case 6:
analogRead(MUL[j]);
break;
case 7:
analogRead(MUL[j]);
break;
case 8:
analogRead(MUL[j]);
break;
case 9:
analogRead(MUL[j]);
break;
case 10:
analogRead(MUL[j]);
break;
case 11:
analogRead(MUL[j]);
break;
case 12:
analogRead(MUL[j]);
break;
case 13:
analogRead(MUL[j]);
break;
case 14:
analogRead(MUL[j]);
break;
case 15:
analogRead(MUL[j]);
break;
}
}
}
// i is the input pin on all multiplexers.
void ReadAssay(int i)
{
//select the multiplexer
for (int j = 0; j < 6; j++)
{
switch (i)
{
case 0:
sensorValues[3][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 1:
sensorValues[2][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 2:
sensorValues[1][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 3:
sensorValues[0][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 4:
sensorValues[3][j * 2] += analogRead(MUL[j]);
break;
case 5:
sensorValues[2][j * 2] += analogRead(MUL[j]);
break;
case 6:
sensorValues[1][j * 2] += analogRead(MUL[j]);
break;
case 7:
sensorValues[0][j * 2] += analogRead(MUL[j]);
break;
case 8:
sensorValues[4][j * 2] += analogRead(MUL[j]);
break;
case 9:
sensorValues[5][j * 2] += analogRead(MUL[j]);
break;
case 10:
sensorValues[6][j * 2] += analogRead(MUL[j]);
break;
case 11:
sensorValues[7][j * 2] += analogRead(MUL[j]);
break;
case 12:
sensorValues[4][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 13:
sensorValues[5][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 14:
sensorValues[6][(j * 2) + 1] += analogRead(MUL[j]);
break;
case 15:
sensorValues[7][(j * 2) + 1] += analogRead(MUL[j]);
break;
}
}
}
// dumps captured data from array to serial monitor
void displayData()
{
Serial.println(F(""));
int j = 0;
String str = "";
for (j = 0; j < 8; j++)
{
for (int i = 0; i < 12; i++)
{
str = str + sensorValues[j][i] + ",";
}
}
Serial.print(str + "$");
Serial.println(F(""));
}
void update_heat_status()
{
if (heat_alarm)
{
if (Input_BOX >= threshold_BOX)
// if(Input_MIDDLE_2 >= threshold_BOX)
{
// Serial.print(F("Box temperature exceeded threshold:"));
// Serial.print(Input_BOX);
// Serial.println("$");
status_led.SwitchMode(LED_ON);
play_sound();
heat_alarm = false;
}
}
}
// }
// if (!status_led_busy)
// {
// if (!status_led_state_on and (Input_BOX >= threshold_BOX))
// {
// digitalWrite(STATUS_LED, HIGH);
// Serial.print(F("Box temperature above the threshold:"));
// Serial.print(Input_BOX);
// Serial.println("$");
// status_led_state_on=true;
// }
// if (status_led_state_on and (Input_BOX < threshold_BOX-0.5))
// {
// digitalWrite(STATUS_LED, LOW);
// Serial.print(F("Box temperature below the threshold:"));
// Serial.print(Input_BOX);
// Serial.println("$");
// status_led_state_on=false;
// }
// }
void Calculate_Heat()
{
ReaderSensorTemp();
SetTunings_PID();
computePIDs();
update_heat_status();
}
void Calculate_Melt()
{
ReaderSensorTemp();
SetTunings_PID_Melt();
computePIDs();
update_heat_status();
}
void computePIDs()
{
PID_MIDDLE.Compute();
PID_UPPER.Compute();
PID_EXTRA.Compute();
analogWrite(HEAT_MIDDLE, Output_MIDDLE);
analogWrite(HEAT_UPPER, Output_UPPER);
analogWrite(HEAT_EXTRA, Output_EXTRA);
/*
analogWrite(HEAT_MIDDLE,0);
analogWrite(HEAT_UPPER,0);
*/
}
void ReaderSensorTemp()
{
Input_UPPER = Temperature(TH_UPPERBED, T_CELSIUS, NCP18XH103F03RB, 10000.0f);
Input_BOX = Temperature(TH_BOX, T_CELSIUS, NCP18XH103F03RB, 10000.0f);
Input_MIDDLE_2 = Temperature(TH_MIDDLEBED_2, T_CELSIUS, NCP18XH103F03RB, 10000.0f);
Input_EXTRA = Temperature(TH_EXTRA, T_CELSIUS, NCP18XH103F03RB, 10000.0f);
}
void SetTunings_PID()
{
double gap1, gap2, gap3;
gap1 = abs(Setpoint_MIDDLE - Temperature(TH_MIDDLEBED_2, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
gap2 = abs(Setpoint_UPPER - Temperature(TH_UPPERBED, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
gap3 = abs(Setpoint_EXTRA - Temperature(TH_EXTRA, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
if (Temperature(TH_MIDDLEBED_2, T_CELSIUS, NCP18XH103F03RB, 10000.0f) > Setpoint_MIDDLE)
{ //we're close to setpoint, use conservative tuning parameters
PID_MIDDLE.SetTunings(consKp, consKi, consKd);
Output_MIDDLE = 0;
}
else
{
//we're far from setpoint, use aggressive tuning parameters
PID_MIDDLE.SetTunings(aggKp / 3, aggKi / 3, aggKd / 3);
}
if (Temperature(TH_UPPERBED, T_CELSIUS, NCP18XH103F03RB, 10000.0f) > Setpoint_UPPER)
{ //we're close to setpoint, use conservative tuning parameters
PID_UPPER.SetTunings(consKp, consKi, consKd);
Output_UPPER = 0;
}
else
{
//we're far from setpoint, use aggressive tuning parameters
PID_UPPER.SetTunings(aggKp, aggKi, aggKd);
}
if (Temperature(TH_EXTRA, T_CELSIUS, NCP18XH103F03RB, 10000.0f) > Setpoint_EXTRA)
{ //we're close to setpoint, use conservative tuning parameters
PID_EXTRA.SetTunings(consKp, consKi, consKd);
Output_EXTRA = 0;
}
else
{
//we're far from setpoint, use aggressive tuning parameters
PID_EXTRA.SetTunings(aggKp / 5, aggKi / 5, aggKd / 5);
}
}
void SetTunings_PID_Melt()
{
double gap1, gap2, gap3;
gap1 = abs(Setpoint_MIDDLE - Temperature(TH_MIDDLEBED_2, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
gap2 = abs(Setpoint_UPPER - Temperature(TH_UPPERBED, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
gap3 = abs(Setpoint_EXTRA - Temperature(TH_EXTRA, T_CELSIUS, NCP18XH103F03RB, 10000.0f)); //distance away from setpoint
PID_MIDDLE.SetTunings(consKp / 50, consKi / 50, consKd / 50);
PID_UPPER.SetTunings(consKp / 20, consKi / 20, consKd / 20);
PID_EXTRA.SetTunings(consKp / 50, consKi / 50, consKd / 50);
}
// check if it is a valid state for the state machine
boolean isState(int s)
{
boolean r = false;
for (int i = 0; i < sizeof(states); i++)
{
r = (states[i] == s);
if (r)
return r;
}
return r;
}
float Temperature(int AnalogInputNumber, int OutputUnit, float B, float T0, float R0, float R_Balance)
{
float R, T;
// R=1024.0f*R_Balance/float(analogRead(AnalogInputNumber)))-R_Balance;
R = R_Balance / (1024.0f / float(analogRead(AnalogInputNumber)) - 1);
T = 1.0f / (1.0f / T0 + (1.0f / B) * log(R / R0));
switch (OutputUnit)
{
case T_CELSIUS:
T -= 273.15f;
T = T * LR_CORR_SLOPE + LR_CORR_INTERCEPT; // linear correction
break;
case T_FAHRENHEIT:
T = 9.0f * (T - 273.15f) / 5.0f + 32.0f;
break;
default:
break;
};
return T;
}
boolean searchString(String str, String search)
{
for (int i = 0; i < str.length(); i++)
{
if (str.substring(i, i + search.length()) == search)
return true;
}
return false;
}
// serial event
void serialEvent()
{
if (Serial.available())
{
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n')
{
stringComplete = true;
}
}
}
}