#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
SoftwareSerial smsSerial(2, 3); // RX, TX
const int flowSensorPin = 2; // Pin for flow sensor
const int relayPin = 4; // Pin for controlling water gate
const int buzzerPin = 5; // Pin for buzzer
volatile int pulseCount = 0;
float flowRate = 0.0;
float totalLiters = 0.0;
float remainingTokens = 100.0; // Initial tokens
unsigned long oldTime = 0;
void setup() {
lcd.begin(16, 2);
pinMode(flowSensorPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Open the gate
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
smsSerial.begin(9600);
Serial.begin(9600);
lcd.print("Water Dispenser");
delay(2000);
lcd.clear();
}
void loop() {
if ((millis() - oldTime) > 1000) { // Process every second
detachInterrupt(digitalPinToInterrupt(flowSensorPin));
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / 7.5;
oldTime = millis();
totalLiters += (flowRate / 60);
pulseCount = 0;
remainingTokens -= (flowRate / 60); // Decrease tokens based on flow
lcd.setCursor(0, 0);
lcd.print("Tokens: ");
lcd.print(remainingTokens, 2);
lcd.setCursor(0, 1);
lcd.print("Flow: ");
lcd.print(flowRate);
lcd.print(" L/min");
if (remainingTokens <= 10) {
digitalWrite(buzzerPin, HIGH); // Alert user when tokens are low
sendSMS("Warning: Low water tokens!");
}
if (remainingTokens <= 0) {
digitalWrite(relayPin, LOW); // Close the gate
sendSMS("Water supply stopped. Recharge tokens.");
lcd.clear();
lcd.print("No water left!");
while (remainingTokens <= 0) {
// Wait until tokens are recharged
}
digitalWrite(relayPin, HIGH); // Open the gate
lcd.clear();
}
attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
}
}
void pulseCounter() {
pulseCount++;
}
void sendSMS(String message) {
smsSerial.print("AT+CMGF=1\r");
delay(100);
smsSerial.print("AT+CMGS=\"+1234567890\"\r"); // Replace with the user's phone number
delay(100);
smsSerial.print(message);
delay(100);
smsSerial.write(26);
}