-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoilMoistureSensor.h
83 lines (69 loc) · 1.65 KB
/
SoilMoistureSensor.h
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
template < size_t iBUFFER_SIZE >
class SoilMoistureSensor
{
private:
int m_aiSensorRawData[iBUFFER_SIZE];
int m_iSensorPin;
float m_fSensorValue;
int m_bSensorHasValue;
int m_iCurrentSamplingIndex;
public:
// Must set pin separately
SoilMoistureSensor ()
{
m_iSensorPin = 0;
m_fSensorValue = 0.0f;
m_bSensorHasValue = false;
m_iCurrentSamplingIndex = 0;
}
// Takes an analog input pin.
SoilMoistureSensor (int iSensorPortNumber)
{
m_iSensorPin = iSensorPortNumber;
m_fSensorValue = 0.0f;
m_bSensorHasValue = false;
m_iCurrentSamplingIndex = 0;
}
void SetPin(int iPin)
{
m_iSensorPin = iPin;
}
boolean DoesSensorHaveValue()
{
return m_bSensorHasValue;
}
void Sample()
{
m_aiSensorRawData[m_iCurrentSamplingIndex] = analogRead(m_iSensorPin);
m_iCurrentSamplingIndex++;
if (m_iCurrentSamplingIndex == iBUFFER_SIZE)
{
m_fSensorValue = 0;
for (int i = 0; i < iBUFFER_SIZE; i++)
{
m_fSensorValue += m_aiSensorRawData[i];
}
m_fSensorValue /= iBUFFER_SIZE;
m_bSensorHasValue = true;
}
m_iCurrentSamplingIndex %= iBUFFER_SIZE;
}
float GetValue()
{
return m_fSensorValue;
}
int GetIntValue()
{
return m_fSensorValue;
}
float GetNormalisedValue(float fScale = 1024.0f)
{
return m_fSensorValue/fScale;
}
void ClearStoredValue()
{
m_bSensorHasValue = false;
m_fSensorValue = 0.0f;
m_iCurrentSamplingIndex = 0;
}
};