#include <Keypad.h>
// #define RED_PIN 4
// #define GREEN_PIN 2
// #define BLUE_PIN 15
// Define pins for the RGB LED
#define RED_PIN 4
#define GREEN_PIN 2
#define BLUE_PIN 15
#define relay 5
// Define LEDC channels for each color
#define RED_CHANNEL 0
#define GREEN_CHANNEL 1
#define BLUE_CHANNEL 2
// Define the LEDC frequency (Hz)
#define LEDC_FREQ 10000
// Define the LEDC resolution
#define LEDC_RES 10
#define SENSOR 13
#define LCD_ADDR 0x27
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(LCD_ADDR, 20, 4);
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
float calibrationFactor = 5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned int usage_limit=5;
//button for lcd
int buttonPin = 35;
int buttonState;
int chk=1;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {32, 33, 25, 26}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {27, 14, 12, 13}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
boolean lcdOn = true;
void setup()
{
Serial.begin(115200);
pinMode(relay,OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Configure the LEDC timers
ledcSetup(RED_CHANNEL, LEDC_FREQ, LEDC_RES);
ledcSetup(GREEN_CHANNEL, LEDC_FREQ, LEDC_RES);
ledcSetup(BLUE_CHANNEL, LEDC_FREQ, LEDC_RES);
// Attach the LEDC channels to the pins
ledcAttachPin(RED_PIN, RED_CHANNEL);
ledcAttachPin(GREEN_PIN, GREEN_CHANNEL);
ledcAttachPin(BLUE_PIN, BLUE_CHANNEL);
// Initialize LCD
//lcd.init();
lcd.backlight();
//lcd.begin();
lcd.init();
pinMode(SENSOR, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
// pinMode(RED_PIN, OUTPUT);
// pinMode(GREEN_PIN, OUTPUT);
// pinMode(BLUE_PIN, OUTPUT);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && chk==0)
{
chk=1;
//lcd.clear();
lcd.noBacklight(); // Turn off the backlight
delay(10000);
Serial.println("lcd clear");
}
char customKey = customKeypad.getKey();
Serial.println(customKey);
if (customKey == '*')
{
lcd.clear();
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Enter new limit:");
lcd.setCursor(0, 2);
while (1)
{
char customKey = customKeypad.getKey();
if (customKey >= '0' && customKey <= '9')
{
lcd.print(customKey);
String keyString = String(customKey); // convert the character input to a string
usage_limit = atoi(keyString.c_str()); // convert the string to an integer and assign it to usage_limit
//usage_limit = atoi(customKeypad.getKey());
// usage_limit = customKey;
}
else if (customKey == '#')
{
lcd.clear();
break;
}
}
}
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
flowMilliLitres = (flowRate / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
// Print the flow rate for this second in litres / minute
if(flowRate>=2)
{
// ledcWrite(RED_CHANNEL, 0);
ledcWrite(GREEN_CHANNEL, 255);
ledcWrite(BLUE_CHANNEL, 0);
delay(1000);
}
else
{
// ledcWrite(RED_CHANNEL, 0);
ledcWrite(GREEN_CHANNEL, 0);
ledcWrite(BLUE_CHANNEL, 255);
delay(3000);
}
if(totalMilliLitres / 1000==usage_limit)
{
ledcWrite(RED_CHANNEL, 255);
ledcWrite(GREEN_CHANNEL, 0);
ledcWrite(BLUE_CHANNEL, 0);
delay(1000);
digitalWrite(relay, HIGH);
}
else
{
ledcWrite(RED_CHANNEL, 0);
delay(1000);
digitalWrite(relay, LOW);
}
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Total Usage: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalMilliLitres / 1000);
Serial.println("L");
if (buttonState == HIGH && chk==1)
{
chk=0;
lcd.setCursor(0, 0);
lcd.print("Water Meter");
lcd.setCursor(0, 1);
lcd.print("Flow Rate: ");
lcd.print(int(flowRate));
lcd.print(" L/min");
lcd.setCursor(0, 2);
lcd.print("Total Usage: ");
lcd.print(totalMilliLitres / 1000);
lcd.print(" L");
lcd.setCursor(0, 3);
lcd.print("Usage Limit: ");
lcd.print(usage_limit);
lcd.print(" L");
Serial.println("lcd On ");
delay(1000);
}
}
}