-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnalogPHMeter.cpp
131 lines (104 loc) · 4.12 KB
/
AnalogPHMeter.cpp
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
#include <Arduino.h>
#include "AnalogPHMeter.h"
int AnalogPHMeter::readADC(int oversampling) {
long pHADCTotal = 0;
for (int i = 0; i < oversampling; i++) {
pHADCTotal += analogRead(pin);
}
return (int)(pHADCTotal / oversampling);
}
void AnalogPHMeter::inputValue(float value) {
sumOfDeltaValue -= deltaValueBuffer[index];
deltaValueBuffer[index] = value - valueBefore;
sumOfDeltaValue += deltaValueBuffer[index];
valueBefore = value;
if (++index == 10) index = 0;
if (-precision < sumOfDeltaValue && sumOfDeltaValue < precision)
stableCount++;
else
stableCount = 0;
stable = (stableCount > 10);
if (stable) stableCount = 10;
}
AnalogPHMeter::AnalogPHMeter(unsigned int pin, float defaultSlope, int defaultAdcAt7) {
this->pin = pin;
this->pH = 0.00f;
this->stable = false;
this->stableCount = 0;
this->index = 0;
this->valueBefore = 0.00f;
for (size_t i = 0; i < sizeof(deltaValueBuffer) / sizeof(float); i++)
this->deltaValueBuffer[i] = 0.00f;
this->sumOfDeltaValue = 0.00f;
this->deltaValue = 0.00f;
this->precision = 0.050f;
this->defaultSlope = defaultSlope;
this->defaultAdcAt7 = defaultAdcAt7;
}
AnalogPHMeter &AnalogPHMeter::initialize(struct PHCalibrationValue calibrationValue) {
this->calibrationValue = calibrationValue;
if (this->calibrationValue.point < '0' || '2' < this->calibrationValue.point ||
this->calibrationValue.adc[0] == 0 || this->calibrationValue.adc[1] == 0) {
this->calibrationClear();
}
return *this;
}
AnalogPHMeter &AnalogPHMeter::singleReading(void) {
pH = calibrationValue.slope * (readADC() - calibrationValue.adcOffset);
inputValue(pH);
return *this;
}
AnalogPHMeter &AnalogPHMeter::temperatureCompensation(float temperature) {
this->temperature = temperature;
return *this;
}
AnalogPHMeter &AnalogPHMeter::calibration(void) {
return *this;
}
AnalogPHMeter &AnalogPHMeter::calibrationClear(void) {
calibrationValue.point = '0';
calibrationValue.value[0] = 7.000f;
calibrationValue.adc[0] = defaultAdcAt7;
calibrationValue.value[1] = 4.000f;
calibrationValue.adc[1] = (int) (defaultAdcAt7 + ((calibrationValue.value[1] - calibrationValue.value[0]) / defaultSlope));
calibrationValue.slope = defaultSlope;
calibrationValue.adcOffset = calibrationValue.adc[0] - (int)(calibrationValue.value[0] / calibrationValue.slope);
return *this;
}
AnalogPHMeter &AnalogPHMeter::calibrationMid(float mid) {
calibrationValue.point = '1';
calibrationValue.value[0] = mid;
calibrationValue.adc[0] = readADC();
calibrationValue.value[1] = 4.000f;
calibrationValue.adc[1] = (int) (calibrationValue.adc[0] + ((calibrationValue.value[1] - calibrationValue.value[0]) / defaultSlope));
calibrationValue.slope = (calibrationValue.value[1] - calibrationValue.value[0]) /
(calibrationValue.adc[1] - calibrationValue.adc[0]);
calibrationValue.adcOffset = calibrationValue.adc[0] - (int)(calibrationValue.value[0] / calibrationValue.slope);
return *this;
}
AnalogPHMeter &AnalogPHMeter::calibrationLow(float low) {
calibrationValue.point = '2';
calibrationValue.value[1] = low;
calibrationValue.adc[1] = readADC();
calibrationValue.slope = (calibrationValue.value[1] - calibrationValue.value[0]) /
(calibrationValue.adc[1] - calibrationValue.adc[0]);
calibrationValue.adcOffset = calibrationValue.adc[0] - (int)(calibrationValue.value[0] / calibrationValue.slope);
return *this;
}
AnalogPHMeter &AnalogPHMeter::calibrationHigh(float high) {
if (calibrationValue.point == '1') {
calibrationValue.value[1] = calibrationValue.value[0];
calibrationValue.adc[1] = calibrationValue.adc[0];
}
calibrationValue.point = '2';
calibrationValue.value[0] = high;
calibrationValue.adc[0] = readADC();
calibrationValue.slope = (calibrationValue.value[1] - calibrationValue.value[0]) /
(calibrationValue.adc[1] - calibrationValue.adc[0]);
calibrationValue.adcOffset = calibrationValue.adc[0] - (int)(calibrationValue.value[0] / calibrationValue.slope);
return *this;
}
AnalogPHMeter &AnalogPHMeter::factoryReset(void) {
calibrationClear();
return *this;
}