#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd2(0x27, 16, 2); // Set the LCD I2C address to 0x27 for a 16x2 display
unsigned long previousMillisStairsCountDown = 0;
unsigned long previousMillisGarageCountDown = 0;
int globalFlowRatePrev;
globalFlowRatePrev=distance; //at the start in setup
bool JustBootedInitValueForFlowRate;
globalFlowRatePrev=true;
void setup() {
lcd2.init(); // Initialize lcd2
lcd2.backlight(); // Turn on backlight
//lcd2.print("Countdown:"); // Display initial message
delay(2000); // Delay for 2 seconds
}
void loop() {
CountDowninLoopStairs();
CountDowninLoopGarage();
}
void CountDowninLoopStairs(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillisStairsCountDown >= 1000) {
previousMillisStairsCountDown = currentMillis;
static int remainingTimeStairs = 600; // 10 minutes in seconds
if (remainingTimeStairs > 0) {
remainingTimeStairs--;
UpdateLCD2Stairs(remainingTimeStairs);
}
}
}
void CountDowninLoopGarage(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillisGarageCountDown >= 1000) {
previousMillisGarageCountDown = currentMillis;
static int remainingTimeGarage = 600; // 10 minutes in seconds
if (remainingTimeGarage > 0) {
remainingTimeGarage--;
UpdateLCD2Garge(remainingTimeGarage);
}
}
}
void UpdateLCD2Stairs(int remainingTimeStairs) {
lcd2.setCursor(9, 0);
lcd2.print(" ");
lcd2.print(remainingTimeStairs / 60); // Convert seconds to minutes
lcd2.print(":");
if (remainingTimeStairs % 60 < 10) {
lcd2.print("0");
}
lcd2.print(remainingTimeStairs % 60); // Remainder is seconds
}
void UpdateLCD2Garge(int remainingTimeGarage) {
lcd2.setCursor(9, 1);
lcd2.print(" ");
lcd2.print(remainingTimeGarage/ 60); // Convert seconds to minutes
lcd2.print(":");
if (remainingTimeGarage % 60 < 10) {
lcd2.print("0");
}
lcd2.print(remainingTimeGarage % 60); // Remainder is seconds
}
void checkFlowRate(){
//static double FlowRateRoofLocal;
unsigned long currentMillisForcheckFlowRate= millis(); //check if the water increasing or decrasing
static int FLowRatePrev;
if (JustBootedInitValueForFlowRate==true){
FLowRatePrev=globalFlowRatePrev;
JustBootedInitValueForFlowRate=false;
}
if (currentMillisForcheckFlowRate - previousMillisForcheckFlowRate>= intervalForcheckFlowRate) {
int FlowRateCurrent=distance;
// save the last time you blinked the LED
previousMillisForcheckFlowRate = currentMillisForcheckFlowRate;
//if ( (arrFlowRate[I_FlowRate-1] != arrFlowRate[I_FlowRate] ) )
if (FlowRatePrev != FlowRateCurrent )
{
FlowRateRoof= (FlowRatePrev-FlowRateCurrent) / 5.00 ; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min //commented
//FlowRateRoofLocal= (arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) / 5.00 ;
Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
Serial.println("Flowrate is + or - ");
FlowRatePrev=FlowRateCurrent;
}
else if (FlowRatePrev == FlowRateCurrent ) {
// else {
Serial.println("Flowrate is 0 ");
FlowRateRoof= 0.00; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min //commented
// FlowRateRoofLocal=0; //commented x
//Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("a[") +String (I_FlowRate-1) + String ("]= ")+ String(arrFlowRate[I_FlowRate-1]) +String(" / a[") +String (I_FlowRate) + String ("]= ")+ String(arrFlowRate[I_FlowRate]) + String(" /Subtract= ")+ String(arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
FlowRatePrev=FlowRateCurrent;
}
//if negative it increrasing if positive it is increasing
//Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
//FlowRatePrev=2;
}
Serial.println("end of Flowrate Func");
}
void checkFlowRate(){
//static double FlowRateRoofLocal;
static int FlowRatePrev;
if (JustBootedInitValueForFlowRate==false){
FlowRatePrev=distance;
JustBootedInitValueForFlowRate=true;
}
unsigned long currentMillisForcheckFlowRate= millis(); //check if the water increasing or decrasing
if (currentMillisForcheckFlowRate - previousMillisForcheckFlowRate>= intervalForcheckFlowRate) {
int FlowRateCurrent=distance;
// save the last time you blinked the LED
previousMillisForcheckFlowRate = currentMillisForcheckFlowRate;
//if ( (arrFlowRate[I_FlowRate-1] != arrFlowRate[I_FlowRate] ) )
//**** Original CheckFlowRate Func*****************
if (FlowRatePrev != FlowRateCurrent )
{
FlowRateRoof= (FlowRatePrev-FlowRateCurrent) / 5.00 ; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min //commented
//FlowRateRoofLocal= (arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) / 5.00 ;
Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
Serial.println("Flowrate is + or - ");
IsRoofIncreasing=1;
//***************************************IsRoofIncreasing setup
/* if (FlowRatePrev > FlowRateCurrent ) {
IsRoofIncreasing=1; //increasing
}
else if (FlowRatePrev < FlowRateCurrent ) {
IsRoofIncreasing=-1; //decreasing
}*/
}
else if (FlowRatePrev == FlowRateCurrent ) {
// else {
Serial.println("Flowrate is 0 ");
FlowRateRoof= 0.00; //calculate flow rate new reaading - last reading devided by 5 mins the whole ionterbal this calc the rate per min //commented
// FlowRateRoofLocal=0; //commented x
//Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("a[") +String (I_FlowRate-1) + String ("]= ")+ String(arrFlowRate[I_FlowRate-1]) +String(" / a[") +String (I_FlowRate) + String ("]= ")+ String(arrFlowRate[I_FlowRate]) + String(" /Subtract= ")+ String(arrFlowRate[I_FlowRate-1]-arrFlowRate[I_FlowRate]) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
IsRoofIncreasing=0; //roof is not
}
// ********************* Integrated Check_Roof_DRN_FLN_Change() *************************** Put her before the original FUnc cuz, in the FlowRate Func we change the value of the FlowRatePrev
if (FlowRatePrev > FlowRateCurrent && distance!=0 && mode!=0) //Right Tank is Filling and Sensor is connected (!=0) and the mode is not Filling (!=0) //I_CheckBoth-1 ==> the first reading I_CheckBoth is the latest reading
{
mode=0; //change the mode to filling
Blynk.virtualWrite(V2, mode); //update blynk app button status to the new one
Serial.println( "Roof Tank Level is Increasing"); //latest distance arr[1] < less than Previous distance arr[0]
//Serial.printf("a[%d]=%d || a[%d]=%d ")
}
else if (FlowRatePrev < FlowRateCurrent && distance!=0 && mode!=1) //Right Tank is Draining and Sensor is connected (!=0) and the mode is not Draining (!=1)
{
mode=1; //change the mode to Draining
Blynk.virtualWrite(V2, mode); //update blynk app button status to the new one
Serial.println( "Roof Tank Level is Decreasing"); //latest distance arr[1] > Larger than Previous distance arr[0]
}
//if negative it increrasing if positive it is increasing
//Firebase.RTDB.setString(&fbdo, "Roof/ArrFlow/",String("FlowRatePrev= ") + String(FlowRatePrev) +String(" / FlowRateCurrent= ") +String(FlowRateCurrent) + String(" /Subtract= ")+ String(FlowRatePrev-FlowRateCurrent) + String(" /Frate= ") +String(FlowRateRoof) + String (" @ ")+TimeNoSpace ); //write Roof Distance Recv from Firebase
//FlowRatePrev=2;
FlowRatePrev=FlowRateCurrent;
}
Serial.println("end of Flowrate Func");
}