volatile int flow_frequency; // Measures flow sensor pulses
// Calculated litres/hour
float vol = 0.0, l_minute;
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

void flow () // Interrupt function
{
  flow_frequency++;
}
void setup()
{
  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // Setup Interrupt
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Water Flow Meter");
  lcd.setCursor(0, 1);
  lcd.print("FRIENDTECHBD");
  currentTime = millis();
  cloopTime = currentTime;
}
void loop ()
{
  currentTime = millis();
  // Every second, calculate and print litres/hour
  if (currentTime >= (cloopTime + 1000))
  {
    cloopTime = currentTime; // Updates cloopTime
    if (flow_frequency != 0) {
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_minute = (flow_frequency / 5.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      //lcd.clear();
      lcd.setCursor(0, 2);
      lcd.print("Speed: ");
      lcd.print(l_minute);
      lcd.print(" L/M");
      l_minute = l_minute / 60;
      lcd.setCursor(0, 3);
      vol = vol + l_minute;
      lcd.print("Total Water:");
      lcd.print(vol);
      lcd.print(" L");
      flow_frequency = 0; // Reset Counter
      Serial.print(l_minute, DEC); // Print litres/hour
      Serial.println(" L/Sec");
    }
    else {
      Serial.println(" flow rate = 0 ");
      // lcd.clear();
      lcd.setCursor(0, 2);
      lcd.print("Speed: ");
      lcd.print( flow_frequency );
      lcd.print(" L/M");
      lcd.setCursor(0, 3);
      lcd.print("Total Water:");
      lcd.print(vol);
      lcd.print(" L");
    }
  }
}