-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_sensor.cpp
More file actions
88 lines (71 loc) · 2.62 KB
/
flow_sensor.cpp
File metadata and controls
88 lines (71 loc) · 2.62 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
83
84
85
86
87
88
#include <Arduino.h>
// Arduino code that measures flow rate and volume of two flow rate sensors
// Constants
const int sensorPin1 = 2; // Pin connected to first flow sensor signal
const int sensorPin2 = 3; // Pin connected to second flow sensor signal
const float calibrationFactor = 1380.0; // Calibration factor using F = 23Q, specified on the sensor
// Variables
volatile unsigned long pulseCount1 = 0; // Pulse counter per second for first sensor
volatile unsigned long pulseCount2 = 0; // Pulse counter per second for second sensor
unsigned long lastMillis = 0; // Time tracker
float flowRate1 = 0.0; // L/min for first sensor
float flowRate2 = 0.0; // L/min for second sensor
float totalVolume1 = 0.0; // L for first sensor
float totalVolume2 = 0.0; // L for second sensor
// Increase first pulse counter when a pulse is detected
void increasePulses1() {
pulseCount1++;
}
// Increase second pulse counter when a pulse is detected
void increasePulses2() {
pulseCount2++;
}
// Initialization
void setup() {
Serial.begin(9600);
// Set signal pins as input with pullup resistor
pinMode(sensorPin1, INPUT_PULLUP);
pinMode(sensorPin2, INPUT_PULLUP);
// When sensor detects increase in voltage, interrupt the main program and increase pulse counter
attachInterrupt(digitalPinToInterrupt(sensorPin1), increasePulses1, RISING);
attachInterrupt(digitalPinToInterrupt(sensorPin2), increasePulses2, RISING);
}
// Main loop
void loop() {
unsigned long currentMillis = millis();
// Run once every second
if (currentMillis - lastMillis >= 1000) {
lastMillis = currentMillis;
// Copy and reset counters safely
noInterrupts();
unsigned long pulses1 = pulseCount1;
unsigned long pulses2 = pulseCount2;
// Reset pulses for next second
pulseCount1 = 0;
pulseCount2 = 0;
interrupts();
// Calculate flow rate (L/min)
flowRate1 = (pulses1 / calibrationFactor) * 60.0;
flowRate2 = (pulses2 / calibrationFactor) * 60.0;
// Calculate total volume (L)
totalVolume1 += (pulses1 / calibrationFactor);
totalVolume2 += (pulses2 / calibrationFactor);
// Output
Serial.print("Sensor 1 (before nozzle) | ");
Serial.print("Pulses: ");
Serial.print(pulses1);
Serial.print(" | Flow Rate: ");
Serial.print(flowRate1);
Serial.print(" L/min | Volume: ");
Serial.print(totalVolume1);
Serial.println(" L");
Serial.print("Sensor 2 (after nozzle) | ");
Serial.print("Pulses: ");
Serial.print(pulses2);
Serial.print(" | Flow Rate: ");
Serial.print(flowRate2);
Serial.print(" L/min | Volume: ");
Serial.print(totalVolume2);
Serial.println(" L");
}
}