/* Project: Water flow meter YF- S201, 5V relay module, solenoid valve Function: Measure of Water Flow in ltrs/hour, close the solenoid valve when flow rate reach 10 ltrs/hour */ volatile int flow_frequency; // Measures flow sensor pulses unsigned int l_hour; // Calculated litres/hour unsigned char flowsensorPin = 2; //water flow sensor attached to digital pin 3 of Arduino Uno const int relayPin = 4; //5v relay module attached to digital pin 4 of Arduino Uno unsigned long currentTime; unsigned long cloopTime; void flow () // Interrupt function { flow_frequency++; } void setup() { pinMode(flowsensorPin, INPUT); //sets the flowsensorPin as an INPUT pinMode(relayPin, OUTPUT);//sets the relayPin as OUTPUT digitalWrite(flowsensorPin, HIGH); // Optional Internal Pull-Up digitalWrite(relayPin, HIGH); Serial.begin(9600); attachInterrupt(0, flow, RISING); // Setup Interrupt sei(); // Enable interrupts currentTime = millis(); cloopTime = currentTime; } void loop () { currentTime = millis(); // Every second, calculate and print litres/hour if (currentTime >= (cloopTime + 1000)) { cloopTime = currentTime; // Updates cloopTime l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5 Q = flowrate in L/hour flow_frequency = 0; // Reset Counter Serial.print(l_hour, DEC); // Print litres/hour Serial.println(" litres/hour"); if (l_hour >= 10) { digitalWrite(relayPin, LOW); } else { digitalWrite(relayPin, HIGH); } } }