-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensors.cpp
More file actions
81 lines (72 loc) · 2.37 KB
/
Sensors.cpp
File metadata and controls
81 lines (72 loc) · 2.37 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
#include "Sensors.h"
#include "Config.h"
#include "Moisture.h"
#include "Multiplexers.h"
#include "Time.h"
#include "Arduino.h"
void getSensorReads(int analogPin1, int readChannels[]) {
for(int i = 0; i < 8; i++) {
setSensorMuxChannel(i);
readChannels[i] = analogRead(analogPin1);
}
}
void getSensorReads(int analogPin1, int analogPin2, int readChannels[]) {
for(int i = 0; i < SENSORS_QTY; i++) {
setSensorMuxChannel(i);
if(i <= 8) {
readChannels[i] = analogRead(analogPin1);
}
else {
readChannels[i] = analogRead(analogPin2);
}
}
}
void printSensorReads(int readChannels[], int cycles[]) {
for(int i = 0; i < SENSORS_QTY; i++) {
Serial.print((String)" " + i + "(" + cycles[i] + ")" + ": " + readMoistureInPercent(readChannels[i]) + "%");
}
Serial.print("\n");
}
void printSensorReads(int readChannels[]) {
for(int i = 0; i < SENSORS_QTY; i++) {
Serial.print((String)" " + i + ": " + readMoistureInPercent(readChannels[i]) + "%" + " (" + readChannels[i] + ")");
}
Serial.print("\n");
}
bool isDry(int& read, const int moistureThresholdPercentage) {
if(map(read, DRY_THRESHOLD, WET_THRESHOLD, 0, 100) < moistureThresholdPercentage) {
return 1;
}
else {
return 0;
}
}
void collectFlags(int readChannels[], bool sensorFlags[]) {
for(int i = 0; i < SENSORS_QTY; i++) {
if(isDry(readChannels[i], MOISTURE_THRESHOLD_PERCENTAGE[i])) {
sensorFlags[i] = 1;
}
}
}
void clearFlags(bool sensorFlags[]) {
for(int i = 0; i < SENSORS_QTY; i++) {
sensorFlags[i] = 0;
}
}
void calibrateSensors() {
unsigned long t = millis();
int seconder = SENSOR_CALIBRATING_TIME;
Serial.println((String)"Calibrating sensors... " + "[" + (seconder) + "s left]");
do {
if(countSeconds(10, t)) {
Serial.println((String)"Calibrating sensors... " + "[" + (seconder) + "s left]");
seconder -= 10;
t = millis();
for(int i = 0; i < SENSORS_QTY; i++) {
setSensorMuxChannel(i);
analogRead(A0);
analogRead(A1); // After pumps interrupt sensor reads, further read values are going crazy. Reading the sensors seems to be necessary for stabilization.
}
}
}while(seconder > 0);
}