-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpHSensor.cpp
More file actions
127 lines (115 loc) · 4.28 KB
/
pHSensor.cpp
File metadata and controls
127 lines (115 loc) · 4.28 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
/**********************************************************************
* pHSensor library
* Arduino library to control a pH sensor usin an analog adapter
* version 0.2 ALPHA 15/09/2015
* Author: Jaime García @peninquen
* License: Apache License Version 2.0.
*
********************************************************************
*/
#include "pHSensor.h"
/***************************************************************************/
/*constructor*/
pHSensor::pHSensor() {
}
/***************************************************************************/
/*Setup variables and initialize interrupts*/
void pHSensor::begin(int pHPin, unsigned short interval, byte calAddress) {
_pHPin = pHPin;
pinMode(_pHPin, INPUT); // analog input pin
_flag = false;
for (int i = 0; i < BUFFERSIZE; i++) _rawData[i] = 0;
_index = 0;
EEPROM.get(calAddress, _cal1);
if (_cal1.magicNumber != MAGIC_NUMBER) calibrate(calAddress, _cal1);
calAddress += sizeof(calibration);
EEPROM.get(calAddress, _cal2);
if (_cal2.magicNumber != MAGIC_NUMBER) calibrate(calAddress, _cal2);
_slope = (float)(_cal2.reference - _cal1.reference) / (_cal2.rawData - _cal1.rawData);
_offset = _cal1.reference - _slope * _cal1.rawData;
_processTime = millis(); // start timer
_interval = interval; //interval during loop function
Serial.print("pH Pin:"); Serial.print(_pHPin);
Serial.print(" EEPROM address:"); Serial.println(calAddress);
}
/**************************************************************************/
/*calibrate sensor
first calculate average value of 64 median values;
second, use average to calculate standard deviation of the filtered and unfiltered readings*/
calibration &pHSensor::calibrate(byte calAddress, calibration &cal) {
unsigned short data;
unsigned short StDev = 0; //standard deviation of filtered data
unsigned short rawStDev = 0; //standard deviation of unfiltered data
unsigned long Dev = 0; // squared deviation of filtered data
unsigned long rawDev = 0; // squared deviation of unfiltered data
Serial.print("intro ref:");
cal.reference = Serial.parseInt();
for (int count = 64; count; --count) {
while (!available()); // wait until data[] is full
data += read();
}
cal.rawData = data >> 6; //average value of 64 median values
Serial.print("ADC value:");
Serial.println(cal.rawData);
for (int count = 64; count; --count) {
for (int index = BUFFERSIZE; index; --index) {
_rawData[index] = analogRead(_pHPin);
rawDev += (cal.rawData - _rawData[_index]) ^ 2;
}
for (int i = 0; i < BUFFERSIZE / 2; i++) {
for (int j = i; j < BUFFERSIZE; j++) {
if (_rawData[i] > _rawData[j]) {
data = _rawData[i];
_rawData[i] = _rawData[j];
_rawData[j] = data;
}
}
}
Dev += (cal.rawData - _rawData[BUFFERSIZE / 2]) ^ 2;
}
StDev = sqrt(Dev >> 6);
rawStDev = sqrt(rawDev / BUFFERSIZE / 64);
Serial.print("filtered standard deviation:");
Serial.println(StDev);
Serial.print("un-filtered standard deviation:");
Serial.println(rawStDev);
}
/***************************************************************************/
/*check interval and read data, interval must be greater than loop cycle*/
boolean pHSensor::refreshData() {
unsigned long nowTime = millis();
if (nowTime - _processTime >= _interval) {
_rawData[_index] = analogRead(_pHPin);
_processTime = nowTime; //stamp process time
if (++_index = BUFFERSIZE) {
_flag = true;
_index = 0;
}
return true;
}
return false;
}
/***************************************************************************/
/*read sensor value*/
// first, sort rawData array to get median value, rawData[BUFFERSIZE/2]
// next, make a linear transformation using calculated offset and slope
short pHSensor::read() {
unsigned short data;
for (int i = 0; i < BUFFERSIZE / 2; i++) {
for (int j = i; j < BUFFERSIZE; j++) {
if (_rawData[i] > _rawData[j]) {
data = _rawData[i];
_rawData[i] = _rawData[j];
_rawData[j] = data;
}
}
}
_flag = false;
return (_offset + _slope * _rawData[BUFFERSIZE / 2]);
}
/***************************************************************************/
/* Is new data available */
boolean pHSensor::available() {
refreshData();
return _flag;
}