int flowPin = 2; // This is the input pin on the Arduino
double flowRate; // This is the value we intend to calculate.
volatile int count; // This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
const int buzzer = 9; // Buzzer connected to Arduino pin 9
void setup() {
pinMode(flowPin, INPUT); // Sets the pin as an input
pinMode(buzzer, OUTPUT); // Set buzzer pin 9 as output
attachInterrupt(digitalPinToInterrupt(flowPin), Flow, RISING); // Configures interrupt on flowPin to run the function "Flow"
Serial.begin(9600); // Start Serial
}
void loop() {
// Calculate flow rate
flowRate = calculateFlowRate();
// Print flow rate to serial monitor
Serial.print("Flow Rate: ");
Serial.println(flowRate, 2); // Print with 2 decimal places
// Check flow rate conditions and control buzzer
if (flowRate > 2.30) {
digitalWrite(buzzer, HIGH);
delay(4000);
digitalWrite(buzzer, LOW);
} else if (flowRate < 2.30 && flowRate > 1.80) {
digitalWrite(buzzer, HIGH);
delay(6000);
digitalWrite(buzzer, LOW);
}
// Delay before next iteration
delay(1000); // Adjust as needed
}
double calculateFlowRate() {
// Perform flow rate calculation based on count variable
double flowRate = analogRead(flowPin); // Convert count to flow rate in Liters per minute
return flowRate;
}
void Flow() {
count++; // Every time this function is called, increment "count" by 1
}