-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirQuality2.cpp
146 lines (124 loc) · 2.8 KB
/
AirQuality2.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
/*********************************************************************
This is a library for the Grove Air Quality Sensor
based on ideas by the AirQuality library from Seeedstudio
Written by Michael Haugk
Version 1.0
Date Nov. 29th, 2014
BSD license, all text above must be included in any redistribution
*********************************************************************/
#include "AirQuality2.h"
#include "Arduino.h"
//Constructor
AirQuality2:: AirQuality2()
{
isInitialized = false;
}
/**
*
* returns -1 if initialization failed
* otherwise it returns the initialization value
*/
int AirQuality2::init(int Pin)
{
int i, _tmpVal[3];
isInitialized = false;
iArduinoPin = Pin;
pinMode(iArduinoPin,INPUT);
iReferenceValue = 0;
// read the first value after 5sec heat time
// and wait 5sec to read more values
for(int i=0;i<3;i++)
{
delay(5000);
_tmpVal[i] = analogRead(iArduinoPin);
iReferenceValue += _tmpVal[i];
}
iReferenceValue = iReferenceValue/3;
// define a set of rules to exclude bad initialization
// like too high or too low sensor values
if(iReferenceValue > BAD_AIR_THRESHOLD || iReferenceValue < 10)
{
return -1;
}
if( abs(_tmpVal[0] - iReferenceValue) > 15 ||
abs(_tmpVal[1] - iReferenceValue) > 15 ||
abs(_tmpVal[2] - iReferenceValue) > 15)
{
return -2;
}
isInitialized = true;
return iReferenceValue;
}
/**
*
* the following values will be returned
* -1: failed to read sensor values
* 0: good air quality
* 1: low pollution
* 2: high pollution
* 3: very high pollution
*
*/
int AirQuality2::evaluate()
{
int currVal, valDiff, quality;
if(!isInitialized) return -1;
quality = -1;
currVal = analogRead(iArduinoPin);
valDiff = currVal - iReferenceValue;
if(currVal > BAD_AIR_THRESHOLD)
{
quality = 3;
}
else if(valDiff > HIGH_POLLUTION_INDICATOR)
{
quality = 2;
}
else if(valDiff > LOW_POLLUTION_INDICATOR)
{
quality = 1;
}
else
{
quality = 0;
}
// Update reference value and last value
calcReference(currVal);
return quality;
}
/**
*
* Get the raw sensor reading data
*
*/
int AirQuality2::getRawData()
{
int currVal;
if(!isInitialized) return -1;
currVal = analogRead(iArduinoPin);
calcReference(currVal);
return (currVal);
}
/**
*
* Update the reference value in order to contribute to sensor "memory" effect
*
*/
void AirQuality2::calcReference(int sValue)
{
// Don't take the value into account for the baseline if it is higher than
// the low pollution indicator plus current reference
if(sValue < (LOW_POLLUTION_INDICATOR + iReferenceValue))
{
iReferenceValue = (iReferenceValue + sValue)/2;
}
}
/**
*
* return reference value
*
*/
int AirQuality2::getReferenceValue()
{
return iReferenceValue;
}