#include <I2CKeyPad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
I2CKeyPad keypad(0x38);
char keypad_layout[19] = "123A456B789C*0#DNF"; // N = NO_KEY, F = FAILED
int sensorInterrupt = 0; // interrupt 0
int sensorPin = 2; //Digital Pin 2
int relay_motor = 12; // Digital pin 5
unsigned int SetPoint = 400; //400 milileter
/*The hall-effect flow sensor outputs pulses per second per litre/minute of flow.*/
float calibrationFactor = 90; //You can change according to your datasheet
volatile byte pulseCount =0;
float flowRate = 0.0;
unsigned int flowMilliLitres =0;
unsigned long totalMilliLitres = 0, volume = 0;
unsigned long oldTime = 0;
//const int relay_motor = 10;
//const int mech_countr = 14;
const int roto = 11;
const int emergencyButtonPin = 13;
long code = 0 ;
int price = 10;
void setup() {
Serial.begin(115200);
if (!keypad.begin()) {
Serial.print("Cannot connect to I2C.\n");
while(1);
}
keypad.loadKeyMap(keypad_layout);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pinMode(relay_motor, OUTPUT);
//pinMode(mech_countr, OUTPUT);
pinMode(roto, INPUT_PULLUP);
pinMode(emergencyButtonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(emergencyButtonPin), emergencyStop, FALLING);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print( "MACHLAB" );
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Amount:");
// put your setup code here, to run once:
Serial.println("Hello, ESP32!");
attachInterrupt(sensorInterrupt, pulseCounter, FALLING); //you can use Rising or Falling
}
void loop() {
char key = keypad.getChar();
if (key>= '0' && key<= '9') {
//code += key;
code = code * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(code);
delay(100);
}
if (key == 'C')
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Amount:");
code= 0;
// inputIndex = 0; // Reset the input index
// memset(inputBuffer, 0, sizeof(inputBuffer));
delay(200);
//code = ;
}
if (key == 'D') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Amount: Rs.");
lcd.print(code);
volume = code * price;
lcd.setCursor(0, 1);
lcd.print("Volume = ");
lcd.print(volume);
lcd.print("ml");
delay(1500);
}
if (totalMilliLitres < volume) {
digitalWrite(relay_motor, HIGH);
// Check the state of the fuel dispensing switch
if (digitalRead(roto) == LOW) {
// solenoidLoop();
if ((millis() - oldTime) > 1000) {
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
Serial.print("Flow rate :-");
Serial.print(flowMilliLitres, DEC);
Serial.print("mL/Second");
Serial.print("\t");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed :");
lcd.print(flowMilliLitres);
lcd.print(" ml/s");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres, DEC);
Serial.println("mL");
Serial.print("\t");
lcd.setCursor(0, 1);
lcd.print("Filled:");
lcd.print(totalMilliLitres);
lcd.print(" ml");
// if (totalMilliLitres > 40) {
// SetSolinoidValve();
// }
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
else{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("please turn on ");
lcd.setCursor(0, 1);
lcd.print("roto switch");
delay(2000);
return;
}
}
else if (totalMilliLitres = volume) {
digitalWrite(relay_motor, LOW);
//saveToEEPROM(inputBuffer);
// if(key == 'A')
// printBill();
// inputIndex = 0; // Reset the input index
// memset(inputBuffer, 0, sizeof(inputBuffer));
volume = 0;
//delay(4000);
// else{
// inputIndex = 0; // Reset the input index
// memset(inputBuffer, 0, sizeof(inputBuffer));
// volume = 0;
// }
}
else {
digitalWrite(relay_motor, LOW);
volume = 0;
}
}
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
void emergencyStop() {
// Stop the liquid dispensing process and perform any necessary cleanup.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Emergency btn");
lcd.setCursor(0, 1);
lcd.print("pressed");
delay(1000);
code= 0;
// inputIndex = 0; // Reset the input index
// memset(inputBuffer, 0, sizeof(inputBuffer));
digitalWrite(relay_motor, HIGH);
// digitalWrite(solenoidValve1, HIGH);
// digitalWrite(solenoidValve2, HIGH);
exit(0);
}