#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DEFINE PIN Push Button
#define TOMBOL3_PIN 32
#define TOMBOL2_PIN 33
#define TOMBOL1_PIN 25
#define DEBOUNCE_TIME 20
// DEFINE PIN Relay
#define RELAY1_PIN 26
#define RELAY2_PIN 27
// DEFINE PIN FLOW Sensor
#define FLOW_PIN 18
LiquidCrystal_I2C lcd(0x27,16,2);
// Variabel Terkait Flow Sensor
volatile byte pulseCount;
unsigned long pulseCountTotal;
boolean flag_pengisian=0;
boolean flag_1Mins=0;
unsigned long time_pengisian_start=0;
byte pulse1Sec = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long timeStart=0;
int interval = 1000;
//float CF = 4.15;
//float flowRate;
//unsigned int flowMilliLitres;
//unsigned long totalMilliLitres, estVol;
// Variabel Terkait Tombol
int status_tombol1,status_tombol2,status_tombol3;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
pulseCountTotal++;
}
void cek_flow_sensor() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
previousMillis = millis();
// Print the flow rate for this second in litres / minute
Serial.print(F("Pulse per second: ")); Serial.print(pulse1Sec);
Serial.print("\t \t");
Serial.print(F("Pulse Total: ")); Serial.println(pulseCountTotal);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("PTot: "); lcd.print(pulseCountTotal);
}
}
void display_home() {
/*
* ================
* Tes FlowSensor
* (1Mins) (>) (X)
* ================
*/
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Tes FlowSensor ");
lcd.setCursor(0,1);
lcd.print("(1Mins) (>) (X)");
}
void setup() {
Serial.begin(115200);
pinMode(TOMBOL1_PIN, INPUT_PULLUP);
pinMode(TOMBOL2_PIN, INPUT_PULLUP);
pinMode(TOMBOL3_PIN, INPUT_PULLUP);
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(FLOW_PIN, INPUT_PULLUP);
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
pulseCount = 0;
pulseCountTotal = 0;
// flowMilliLitres = 0;
// totalMilliLitres = 0;
// estVol=0;
previousMillis = 0;
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
display_home();
Serial.println("ready");
attachInterrupt(digitalPinToInterrupt(FLOW_PIN), pulseCounter, FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
if(flag_pengisian) {
cek_flow_sensor();
if(flag_1Mins) {
if(millis() - time_pengisian_start >= 60000) {
flag_1Mins = false;
flag_pengisian = false;
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
Serial.println("Pengisian 1 menit Selesai");
Serial.print("Total Pulse : ");
Serial.print(pulseCountTotal);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("TotalPulse: ");
lcd.print(pulseCountTotal);
pulseCount = 0;
pulseCountTotal = 0;
delay(5000);
display_home();
}
}
}
else {
status_tombol1 = digitalRead(TOMBOL1_PIN);
status_tombol2 = digitalRead(TOMBOL2_PIN);
if(status_tombol1 == LOW) {
delay(20);
status_tombol1 = digitalRead(TOMBOL1_PIN);
if(status_tombol1 == LOW) {
if(!flag_pengisian) {
Serial.println("pengisian 1 menit");
lcd.setCursor(0,0);
lcd.print("Pengisian 1Menit");
flag_pengisian = true;
flag_1Mins = true;
digitalWrite(RELAY1_PIN, LOW);
digitalWrite(RELAY2_PIN, LOW);
time_pengisian_start = millis();
}
}
}
if(status_tombol2 == LOW) {
delay(20);
status_tombol2 = digitalRead(TOMBOL2_PIN);
if(status_tombol2 == LOW) {
if(!flag_pengisian){
Serial.println("Pengisian Aktif");
lcd.setCursor(0,0);
lcd.print("Pengisian Aktif ");
flag_pengisian=true;
digitalWrite(RELAY2_PIN, LOW);
digitalWrite(RELAY1_PIN, LOW);
timeStart=millis();
}
}
}
}
status_tombol3 = digitalRead(TOMBOL3_PIN);
if(status_tombol3 == LOW) {
delay(20);
status_tombol3 = digitalRead(TOMBOL3_PIN);
if(status_tombol3 == LOW) {
flag_pengisian=false;
flag_1Mins=false;
lcd.setCursor(0,0);
lcd.print("PengisianSelesai");
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
Serial.println("Pengisian Selesai");
Serial.print("Total Pulse : ");
Serial.println(pulseCountTotal);
Serial.print("Total Waktu : ");
Serial.println(millis()-timeStart);
delay(5000);
pulseCount=0;
pulseCountTotal=0;
display_home();
}
}
}