#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Inisialisasi keypad
const byte ROWS = 4; //jumlah baris pada keypad
const byte COLS = 4; //jumlah kolom pada keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; //sesuaikan pinnya sesuai dengan koneksi Anda
byte colPins[COLS] = {26, 25, 33, 32}; //sesuaikan pinnya sesuai dengan koneksi Anda
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Inisialisasi LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C default untuk LCD 16x2 adalah 0x27
int sensorInterrupt = 0;
int sensorPin = 2;
int solenoidValve = 5;
unsigned int SetPoint = 400;
String code = "";
float calibrationFactor = 90;
volatile byte pulseCount = 0;
float flowRate = 0.0;
unsigned int flowMilliLitres = 0;
unsigned long totalMilliLitres = 0, volume = 0;
unsigned long oldTime;
const int relay_moteur = 35;
void setup() {
totalMilliLitres = 0;
pinMode(relay_moteur, OUTPUT);
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Set Volume:");
Serial.begin(9600);
pinMode(solenoidValve, OUTPUT);
digitalWrite(solenoidValve, HIGH);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
}
void loop() {
char key = keypad.getKey();
if (key) {
code += key;
lcd.setCursor(0, 1);
lcd.print(code);
delay(100);
}
if (key == 'D') {
if (code.toInt() <= 1500) {
volume = code.toInt();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Volume:");
}
code = "";
}
if (totalMilliLitres < volume) {
digitalWrite(relay_moteur, HIGH);
if ((millis() - oldTime) > 1000) {
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
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;
}
} else {
digitalWrite(relay_moteur, LOW);
volume = 0;
}
}
void pulseCounter() {
pulseCount++;
}
void SetSolinoidValve() {
digitalWrite(solenoidValve, LOW);
}