Repository navigation
Expand file tree
/
Copy pathCode
More file actions
50 lines (40 loc) · 1.48 KB
/
Copy pathCode
File metadata and controls
50 lines (40 loc) · 1.48 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
// Define the input pin for the flow meter
const int flowMeterPin = 2; // Example pin, adjust as needed
// Variables to store flow rate and pulse count
volatile unsigned int pulseCount;
float flowRate;
// Timer variables
unsigned long startTime;
unsigned long currentTime;
void setup() {
Serial.begin(9600); // Initialize serial communication
// Attach interrupt for flow meter pulse
attachInterrupt(digitalPinToInterrupt(flowMeterPin), pulseCounter, FALLING);
// Initialize variables
pulseCount = 0;
flowRate = 0.0;
startTime = millis(); // Start time for flow rate calculation
}
void loop() {
// Calculate flow rate every second
currentTime = millis();
if (currentTime - startTime >= 1000) {
detachInterrupt(digitalPinToInterrupt(flowMeterPin)); // Disable interrupts during calculation
flowRate = pulseCount / 7.5; // Convert pulse count to flow rate (example conversion, adjust based on sensor specifications)
pulseCount = 0; // Reset pulse count
startTime = currentTime; // Update start time
// Display flow rate on serial monitor
Serial.print("Flow Rate: ");
Serial.print(flowRate);
Serial.println(" L/min");
// Display "CLEAN THE PIPE!" message if flow rate is zero
if (flowRate == 0.0) {
Serial.println("CLEAN THE PIPE!");
}
attachInterrupt(digitalPinToInterrupt(flowMeterPin), pulseCounter, FALLING); // Re-enable interrupts
}
}
// Interrupt service routine to count pulses
void pulseCounter() {
pulseCount++;
}