-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_temp_reg.ino
116 lines (100 loc) · 3.83 KB
/
03_temp_reg.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
/* -------------------------------------------------------------------------- */
void regulateTemperature(struct SolderingStation &station) {
if (station.mode < MODE_INACTIVE) {
regulateTemperatureActiveModes(station);
}
else {
regulateTemperatureInactive(station);
}
getTempTrend(station);
regulateIron(station);
}
/* -------------------------------------------------------------------------- */
void regulateTemperatureActiveModes(struct SolderingStation &station) {
station.timerRegulate.currentMillis = millis();
boolean tick = station.timerRegulate.currentMillis -
station.timerRegulate.previousMillis >=
station.timerRegulate.time;
if (tick == false) return;
station.tempSensor = analogRead(PIN_T);
station.tempMeasured = mapSensToTemp(station.tempSensor, station.currentSensor);
station.tempMeasured += station.userCalibration * station.userCalibrationStep;
#ifdef SAMPLE_SMOOTHING
sampleSmoothingQueue(station);
#else
sampleSmoothingSimple(station);
#endif
station.previousTempDisplay = station.tempDisplay;
station.previousTempMeasured = station.tempMeasured;
station.timerRegulate.previousMillis = station.timerRegulate.currentMillis;
}
/* -------------------------------------------------------------------------- */
void regulateTemperatureInactive(struct SolderingStation &station) {
station.tempMeasured = TEMP_INACTIVE;
station.tempDisplay = TEMP_INACTIVE;
}
/* -------------------------------------------------------------------------- */
#ifdef SAMPLE_SMOOTHING
void sampleSmoothingQueue(struct SolderingStation &station) {
// station.queue.sum -= station.queue.last->val;
addQueueNode(&station.queue, station.tempMeasured);
station.tempDisplay = station.queue.sum / station.queue.size;
}
#else
void sampleSmoothingSimple(struct SolderingStation &station) {
station.tempDisplay += station.tempMeasured;
station.tempDisplay /= 2;
}
#endif
/* -------------------------------------------------------------------------- */
void regulateIron(struct SolderingStation &station) {
#ifdef IRON_DEBUG
if (station.tempMeasured < station.tempSet) {
// digitalWrite(PIN_LED, HIGH);
PORTB = PORTB | MASK_PIN_LED;
}
else {
// digitalWrite(PIN_LED, LOW);
PORTB = PORTB & ~MASK_PIN_LED;
}
#endif
#ifdef IRON_LIVE
if (station.tempTrend > 0 && station.tempMeasured > station.tempSet + station.tempToleranceUp) {
// digitalWrite(PIN_LED, HIGH);
PORTB = PORTB | MASK_PIN_LED;
return;
}
if (station.tempTrend < 0 && station.tempMeasured < station.tempSet - station.tempToleranceDown) {
// digitalWrite(PIN_LED, LOW);
PORTB = PORTB & ~MASK_PIN_LED;
return;
}
#endif
}
/* -------------------------------------------------------------------------- */
int limitSetTemp(int position, int limit_1, int limit_2) {
if (position < limit_1) {
return limit_1;
}
if (position > limit_2) {
return limit_2;
}
return position;
}
/* -------------------------------------------------------------------------- */
int mapSensToTemp(float sensVal, struct SensorInfo *sensor) {
return (int) (sensor->tempRef + (sensVal - sensor->adcRef) * sensor->slope + sensor->calibration);
}
/* -------------------------------------------------------------------------- */
void getTempTrend(struct SolderingStation &station) {
if (station.tempMeasured > station.previousTempMeasured) {
station.tempTrend = 1;
return;
}
if (station.tempMeasured < station.previousTempMeasured) {
station.tempTrend = -1;
return;
}
station.tempTrend = 0;
}
/* -------------------------------------------------------------------------- */