#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int flowSensorPin = A0; // analog pin for the flow sensor
float flowRate; // flow rate in liters per minute
float totalFlow; // total flow in liters
unsigned long timeElapsed; // time elapsed since last flow calculation
unsigned long oldTime; // old time in milliseconds
bool wifiConnected = false;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Waiting for");
lcd.setCursor(0, 1);
lcd.print("Wi-Fi connection");
// Simulating Wi-Fi connection delay
delay(4000);
lcd.setCursor(0,0);
lcd.print("Connected to ");
lcd.setCursor(0,1);
lcd.print("wifi");
delay(2000);
// Simulating successful Wi-Fi connection
wifiConnected = true;
// Initialize flow calculation variables
timeElapsed = 0;
oldTime = millis();
}
void loop() {
if (wifiConnected) {
int rawFlow = analogRead(flowSensorPin); // read the analog input from the flow sensor
flowRate = (rawFlow / 1024.0) * 5.0 / 2.25; // convert the raw flow reading to liters per minute
totalFlow += flowRate * timeElapsed / 60000.0; // calculate the total flow based on the flow rate and time elapsed
timeElapsed = millis() - oldTime; // calculate the time elapsed since the last flow calculation
oldTime = millis(); // store the current time for the next flow calculation
lcd.setCursor(0, 0);
lcd.print("Flow rate: ");
lcd.print(flowRate, 2); // display the flow rate value on the LCD display with two decimal places
lcd.print(" L/min");
lcd.setCursor(0, 1);
lcd.print("Total flow: ");
lcd.print(totalFlow, 2); // display the total flow value on the LCD display with two decimal places
lcd.print(" L");
}
else {
lcd.setCursor(0, 0);
lcd.print("Waiting for");
lcd.setCursor(0, 1);
lcd.print("Wi-Fi connection");
}
delay(500);
}