-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathGravityEc.cpp
129 lines (111 loc) · 4.03 KB
/
GravityEc.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
/*********************************************************************
* GravityEc.cpp
*
* Copyright (C) 2017 [DFRobot](http://www.dfrobot.com),
* GitHub Link :https://github.com/DFRobot/watermonitor
* This Library 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.
*
* Description:Monitoring water quality parameters Conductivity
*
* Product Links:http://www.dfrobot.com.cn/goods-882.html
*
* Sensor driver pin:A1 (ecSensorPin(A1))
*
* author : Jason(jason.ling@dfrobot.com)
* version : V1.0
* date : 2017-04-17
**********************************************************************/
#include "GravityEc.h"
#include "Arduino.h"
GravityEc::GravityEc(ISensor* temp) :ecSensorPin(A1), ECcurrent(0), index(0), AnalogAverage(0),
AnalogValueTotal(0), averageVoltage(0), AnalogSampleTime(0), printTime(0),sum(0),
tempSampleTime(0), AnalogSampleInterval(25),printInterval(700)
{
this->ecTemperature = temp;
}
GravityEc::~GravityEc()
{
}
//********************************************************************************************
// function name: setup ()
// Function Description: Initializes the sensor
//********************************************************************************************
void GravityEc::setup()
{
pinMode(ecSensorPin, INPUT);
for (byte thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
//********************************************************************************************
// function name: update ()
// Function Description: Update the sensor value
//********************************************************************************************
void GravityEc::update()
{
calculateAnalogAverage();
calculateEc();
}
//********************************************************************************************
// function name: getValue ()
// Function Description: Returns the sensor data
//********************************************************************************************
double GravityEc::getValue()
{
return ECcurrent;
}
//********************************************************************************************
// function name: calculateAnalogAverage ()
// Function Description: Calculates the average voltage
//********************************************************************************************
void GravityEc::calculateAnalogAverage()
{
if (millis() - AnalogSampleTime >= AnalogSampleInterval)
{
AnalogSampleTime = millis();
readings[index++] = analogRead(ecSensorPin);
if (index == numReadings)
{
index = 0;
for (int i = 0; i < numReadings; i++)
this->sum += readings[i];
AnalogAverage = this->sum / numReadings;
this->sum = 0;
}
}
}
//********************************************************************************************
// function name: calculateAnalogAverage ()
// Function Description: Calculate the conductivity
//********************************************************************************************
void GravityEc::calculateEc()
{
if (millis() - printTime >= printInterval)
{
printTime = millis();
averageVoltage = AnalogAverage*5000.0 / 1024.0;
double TempCoefficient = 1.0 + 0.0185*(this->ecTemperature->getValue() - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.0185*(fTP-25.0));
double CoefficientVolatge = (double)averageVoltage / TempCoefficient;
if (CoefficientVolatge < 150) {
ECcurrent = 0;
return;
}
else if (CoefficientVolatge > 3300)
{
ECcurrent = 20;
return;
}
else
{
if (CoefficientVolatge <= 448)
ECcurrent = 6.84*CoefficientVolatge - 64.32; //1ms/cm<EC<=3ms/cm
else if (CoefficientVolatge <= 1457)
ECcurrent = 6.98*CoefficientVolatge - 127; //3ms/cm<EC<=10ms/cm
else
ECcurrent = 5.3*CoefficientVolatge + 2278; //10ms/cm<EC<20ms/cm
ECcurrent /= 1000; //convert us/cm to ms/cm
}
}
}