forked from DFRobot/DFRobot_PH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFRobot_PH.cpp
319 lines (280 loc) · 10.3 KB
/
DFRobot_PH.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
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
/*
* file DFRobot_PH.cpp * @ https://github.com/DFRobot/DFRobot_PH
*
* Arduino library for Gravity: Analog pH Sensor / Meter Kit V2, SKU: SEN0161-V2
*
* Copyright [DFRobot](http://www.dfrobot.com), 2018
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-04
*
* version V1.1
* date 2020-04
* Changes the memory addressing to allow multiple PH sensors
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "DFRobot_PH.h"
#include <EEPROM.h>
#define EEPROM_write(address, value) {int i = 0; byte *pp = (byte*)&(value);for(; i < sizeof(value); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, value) {int i = 0; byte *pp = (byte*)&(value);for(; i < sizeof(value); i++) pp[i]=EEPROM.read(address+i);}
// The start address of the pH calibration parameters stored in the EEPROM
#define PHVALUEADDR 0
// Maps the pin (A0,A1..A11) to a number 0-11 so the address can be determined
uint8_t mapPHPin(uint8_t phPin)
{
int addressMap = 0;
if (phPin == A0){
Serial.print(" pin A0 ");
addressMap = 0;
} else if (phPin == A1){
Serial.print(" pin A1 ");
addressMap = 1;
} else if (phPin == A2){
Serial.print(" pin A2 ");
addressMap = 2;
} else if (phPin == A3){
Serial.print(" pin A3 ");
addressMap = 3;
} else if (phPin == A4){
Serial.print(" pin A4 ");
addressMap = 4;
} else if (phPin == A5){
Serial.print(" pin A5 ");
addressMap = 5;
} else if (phPin == A6){
Serial.print(" pin A6 ");
addressMap = 6;
} else if (phPin == A7){
Serial.print(" pin A7 ");
addressMap = 7;
} else if (phPin == A8){
Serial.print(" pin A8 ");
addressMap = 8;
} else if (phPin == A9){
Serial.print(" pin A9 ");
addressMap = 9;
} else if (phPin == A10){
Serial.print(" pin A10 ");
addressMap = 10;
} else if (phPin == A11){
Serial.print(" pin A11 ");
addressMap = 11;
}
return addressMap;
}
// Default construtor to ensure backwards compatibility
DFRobot_PH::DFRobot_PH()
{
// As no pin was provided we will use the data for pin A0
this->_pin = A0;
// Set the address
// For each PH sensor we need to store 2 floats (pH 4.0 and pH 7.0)
// This will be 8byte per sensor and the largest arduino has 12 analogue ports
// So let's start at address 0 and go up by 8b for each analogue port. This will use upto 96b
// For the EC library we will start after PH addresses
this->_address = PHVALUEADDR + (sizeof(float) * 2 * mapPHPin(this->_pin));
// Buffer solution 4.0 at 25C
this->_acidVoltage = 2032.44;
// Buffer solution 7.0 at 25C
this->_neutralVoltage = 1500.0;
// Initialise the rest of the values with an initial starting value
this->_voltage = 1500.0;
this->_temperature = 25.0;
this->_phValue = 7.0;
}
// Updated construtor to allow multiple pH sensors
DFRobot_PH::DFRobot_PH(uint8_t phPin)
{
// Set the pin to the supplied value
this->_pin = phPin;
// Set the address
// For each PH sensor we need to store 2 floats (pH 4.0 and pH 7.0)
// This will be 8byte per sensor and the largest arduino has 12 analogue ports
// So let's start at address 0 and go up by 8b for each analogue port. This will use upto 96b
// For the EC library we will start after PH addresses
this->_address = PHVALUEADDR + (sizeof(float) * 2 * mapPHPin(this->_pin));
// Buffer solution 4.0 at 25C
this->_acidVoltage = 2032.44;
// Buffer solution 7.0 at 25C
this->_neutralVoltage = 1500.0;
// Initialise the rest of the values with an initial starting value
this->_voltage = 1500.0;
this->_temperature = 25.0;
this->_phValue = 7.0;
}
// Default destructor
DFRobot_PH::~DFRobot_PH()
{
}
// Initialiser
void DFRobot_PH::begin()
{
Serial.print("_pin:");
Serial.println(this->_pin);
Serial.print("mapped:");
Serial.println(mapPHPin(this->_pin));
Serial.print("_address:");
Serial.println(this->_address);
// Load the neutral (pH = 7.0) voltage of the pH board from the EEPROM
EEPROM_read(this->_address, this->_neutralVoltage);
Serial.print("_neutralVoltage:");
Serial.println(this->_neutralVoltage);
// If the values are all 255 then write a default value in
if(EEPROM.read(this->_address)==0xFF && EEPROM.read(this->_address+1)==0xFF && EEPROM.read(this->_address+2)==0xFF && EEPROM.read(this->_address+3)==0xFF){
// new EEPROM, write typical voltage for pH = 7.0
this->_neutralVoltage = 1500.0;
EEPROM_write(this->_address, this->_neutralVoltage);
}
// Load the acid (pH = 4.0) voltage of the pH board from the EEPROM
EEPROM_read(this->_address+4, this->_acidVoltage);
Serial.print("_acidVoltage:");
Serial.println(this->_acidVoltage);
// If the values are all 255 then write a default value in
if(EEPROM.read(this->_address+4)==0xFF && EEPROM.read(this->_address+5)==0xFF && EEPROM.read(this->_address+6)==0xFF && EEPROM.read(this->_address+7)==0xFF){
// new EEPROM, write typical voltage for pH = 4.0
this->_acidVoltage = 2032.44;
EEPROM_write(this->_address+4, this->_acidVoltage);
}
}
// Function to read the pH
float DFRobot_PH::readPH(float voltage, float temperature)
{
// From the two point calibration: (_neutralVoltage, 7.0), (_acidVoltage, 4.0)
float slope = (7.0 - 4.0)/((this->_neutralVoltage - 1500.0) / 3.0 - (this->_acidVoltage - 1500.0) / 3.0);
float intercept = 7.0 - slope*(this->_neutralVoltage - 1500.0) / 3.0;
// Linear response y = k*x + b
this->_phValue = slope * (voltage - 1500.0) / 3.0 + intercept;
return _phValue;
}
void DFRobot_PH::calibration(float voltage, float temperature, char* cmd)
{
this->_voltage = voltage;
this->_temperature = temperature;
strupr(cmd);
// If received Serial CMD from the serial monitor, enter into the calibration mode
phCalibration(cmdParse(cmd));
}
void DFRobot_PH::calibration(float voltage, float temperature)
{
this->_voltage = voltage;
this->_temperature = temperature;
// If received Serial CMD from the serial monitor, enter into the calibration mode
if(cmdSerialDataAvailable() > 0){
phCalibration(cmdParse());
}
}
boolean DFRobot_PH::cmdSerialDataAvailable()
{
char cmdReceivedChar;
static unsigned long cmdReceivedTimeOut = millis();
while(Serial.available()>0){
if(millis() - cmdReceivedTimeOut > 500U){
this->_cmdReceivedBufferIndex = 0;
memset(this->_cmdReceivedBuffer,0,(ReceivedBufferLength));
}
cmdReceivedTimeOut = millis();
cmdReceivedChar = Serial.read();
if (cmdReceivedChar == '\n' || this->_cmdReceivedBufferIndex==ReceivedBufferLength-1){
this->_cmdReceivedBufferIndex = 0;
strupr(this->_cmdReceivedBuffer);
return true;
}else{
this->_cmdReceivedBuffer[this->_cmdReceivedBufferIndex] = cmdReceivedChar;
this->_cmdReceivedBufferIndex++;
}
}
return false;
}
byte DFRobot_PH::cmdParse(const char* cmd)
{
byte modeIndex = 0;
if(strstr(cmd, "ENTERPH") != NULL){
modeIndex = 1;
}else if(strstr(cmd, "EXITPH") != NULL){
modeIndex = 3;
}else if(strstr(cmd, "CALPH") != NULL){
modeIndex = 2;
}
return modeIndex;
}
byte DFRobot_PH::cmdParse()
{
byte modeIndex = 0;
if(strstr(this->_cmdReceivedBuffer, "ENTERPH") != NULL){
modeIndex = 1;
}else if(strstr(this->_cmdReceivedBuffer, "CALPH") != NULL){
modeIndex = 2;
}else if(strstr(this->_cmdReceivedBuffer, "EXITPH") != NULL){
modeIndex = 3;
}
return modeIndex;
}
void DFRobot_PH::phCalibration(byte mode)
{
char *receivedBufferPtr;
static boolean phCalibrationFinish = 0;
static boolean enterCalibrationFlag = 0;
switch(mode){
case 0:
if(enterCalibrationFlag){
Serial.println(F(">>>Command Error<<<"));
}
break;
case 1:
enterCalibrationFlag = 1;
phCalibrationFinish = 0;
Serial.println();
Serial.println(F(">>>Enter PH Calibration Mode<<<"));
Serial.println(F(">>>Please put the probe into the 4.0 or 7.0 standard buffer solution<<<"));
Serial.println();
break;
case 2:
if(enterCalibrationFlag){
if((this->_voltage>1322)&&(this->_voltage<1678)){ // buffer solution:7.0
Serial.println();
Serial.print(F(">>>Buffer Solution:7.0"));
this->_neutralVoltage = this->_voltage;
Serial.println(F(",Send EXITPH to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}else if((this->_voltage>1854)&&(this->_voltage<2210)){ //buffer solution:4.0
Serial.println();
Serial.print(F(">>>Buffer Solution:4.0"));
this->_acidVoltage = this->_voltage;
Serial.println(F(",Send EXITPH to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}else{
Serial.println();
Serial.print(F(">>>Buffer Solution Error Try Again<<<"));
Serial.println(); // not buffer solution or faulty operation
phCalibrationFinish = 0;
}
}
break;
case 3:
if(enterCalibrationFlag){
Serial.println();
if(phCalibrationFinish){
if((this->_voltage>1322)&&(this->_voltage<1678)){
EEPROM_write(this->_address, this->_neutralVoltage);
}else if((this->_voltage>1854)&&(this->_voltage<2210)){
EEPROM_write(this->_address+4, this->_acidVoltage);
}
Serial.print(F(">>>Calibration Successful"));
}else{
Serial.print(F(">>>Calibration Failed"));
}
Serial.println(F(",Exit PH Calibration Mode<<<"));
Serial.println();
phCalibrationFinish = 0;
enterCalibrationFlag = 0;
}
break;
}
}