-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowSensor.cpp
More file actions
82 lines (69 loc) · 2.24 KB
/
FlowSensor.cpp
File metadata and controls
82 lines (69 loc) · 2.24 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
82
#include "FlowSensor.h"
FlowSensor::FlowSensor(byte pin) {
this->pin = pin;
}
void FlowSensor::begin(void (*isr)(void)) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
attachInterrupt(digitalPinToInterrupt(pin), isr, FALLING);
}
void FlowSensor::isr() {
pulses++;
}
boolean FlowSensor::read(unsigned long period) {
noInterrupts();
lastPulses = pulses;
pulses = 0;
interrupts();
lastPeriodMillis = period;
if (lastPulses > 0) {
currentFlowPulses += lastPulses;
currentFlowTimeMillis += period;
// if we've crossed the minimum pulses to detect a flow
if (currentFlowPulses >= FLOW_SENSOR_MIN_PULSES_FOR_FLOW_DETECTION) {
// if this is the first time we've detected this flow
if (!currentFlowDetected) {
currentFlowDetected = true;
totalFlowCount++; // count this flow
totalPulses += currentFlowPulses; // record the accumulated pulses for the current flow
totalFlowTimeMillis += currentFlowTimeMillis; // record the accumulated time for the current flow
} else {
totalPulses += lastPulses; // record the last pulses reported
totalFlowTimeMillis += period; // record the last period
}
}
} else { // there is no flow
// reset all accumulated stats for the current flow
currentFlowPulses = 0;
currentFlowTimeMillis = 0;
currentFlowDetected = false;
}
return currentFlowDetected;
}
byte FlowSensor::getLastPulses() {
return lastPulses;
}
float FlowSensor::getLastGpm() {
return lastPulses == 0 || lastPeriodMillis == 0? 0.0 : pulsesToGallons(lastPulses * (60000.0f / float(lastPeriodMillis)));
}
boolean FlowSensor::isFlowing() {
return currentFlowDetected;
}
unsigned long FlowSensor::getCurrentFlowPulses() {
return currentFlowPulses;
}
unsigned long FlowSensor::getCurrentFlowTimeMillis() {
return currentFlowTimeMillis;
}
unsigned long FlowSensor::getTotalFlowPulses() {
return totalPulses;
}
unsigned long FlowSensor::getTotalFlowMillis() {
return totalFlowTimeMillis;
}
unsigned long FlowSensor::getTotalFlowCount() {
return totalFlowCount;
}
float FlowSensor::pulsesToGallons(unsigned long pulses) {
return pulses / float(FLOW_SENSOR_PULSES_PER_GALLON);
}