-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHumiditySensor.cpp
70 lines (54 loc) · 1.94 KB
/
HumiditySensor.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
/* Copyright (C) 2015 Willi Menapace <willi.menapace@gmail.com>, Simone Lorengo <simone@lorengo.org> - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Simone Lorengo <simone@lorengo.org>
*/
#include "GlobalDefines.h"
#ifdef ONBOARD
#include <Arduino.h>
#include "HumiditySensor.h"
#include "GroundHumidity.cpp"
SensorType HumiditySensor::getType() {
return type;
}
long HumiditySensor::readValue() {
long humidity = ((getRaw() + offset) * 100) / 1023;
return humidity;
}
GroundHumidity HumiditySensor::readRange() {
long humidity = getRaw() + offset;
const int MAX = 1023 + offset,
MIN = 0 + offset;
if (humidity >= MIN && humidity < DRY) return COMPLETELY_DRY;
else if (humidity >= DRY && humidity < QUITE_HUMID) return DRY;
else if (humidity >= QUITE_HUMID && humidity < HUMID) return QUITE_HUMID;
else if (humidity >= HUMID && humidity < QUITE_WET) return HUMID;
else if (humidity >= QUITE_WET && humidity < WET) return QUITE_WET;
else if (humidity >= WET && humidity < MAX) return WET;
else return ERROR;
}
long HumiditySensor::getRaw() {
return analogRead(sensorPin);
}
void HumiditySensor::setOffset(long calibOffset){
offset = calibOffset;
}
bool HumiditySensor::check() {
const int READINGS = 10; //Numero di letture da eseguire
int values[READINGS], //Array di valori letti
deltaValues = 0; //Differenza tra un valore e un altro
values[0] = analogRead(sensorPin);
for (int i = 1; i < READINGS; i++) {
values[i] = analogRead(sensorPin);
deltaValues += values[i - 1] - values[i];
delay(200);
}
if (deltaValues >(READINGS / 2)) return false;
else return true;
}
HumiditySensor::HumiditySensor(int sensorAnalogPin) {
type = HUMIDITY;
sensorPin = sensorAnalogPin;
offset = 0;
}
#endif //ONBAORD