#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//Keypad
const byte ROW = 4;
const byte COL = 4;
char keys[ROW][COL] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const int PASS_LENGTH = 5;
const char PASSWORD[] = { '1', '2', '3' ,'A', 'B' };
const int MAX_ATTEMPT = 3;
byte rowP[ROW] = {9, 8, 7, 6};
byte colP[COL] = {5, 4, 3, 2};
Keypad key = Keypad(makeKeymap(keys), rowP, colP, ROW, COL);
const int PIN_BUZZER = 10;
//LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte sensorInt = 0;
byte flowsensor = 11;
float konstanta = 5.5;
float debit;
float liter;
float volume;
unsigned long oldTime;
unsigned int flowmlt;
volatile byte pulseCount;
const int relay = 12;
int i = 0, q = 0, j = 0, p = 0, pass;
long beli, harga, harga5 = 0;
char stringAngka[10];
char str[10];
char harga2[10];
long double harga3;
long harga4;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH);
pulseCount = 0;
debit = 0.0;
flowmlt = 0;
volume = 0.0;
oldTime = 0;
attachInterrupt(sensorInt, pulseCounter, FALLING);
pinMode(relay, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("POM MINI KINGS");
lcd.setCursor(0, 1);
lcd.print("TAMAN SEMANAN INDAH");
lcd.setCursor(0, 2);
lcd.print("HARGA : Rp.");
lcd.print(harga);
lcd.print(" /L");
}
void loop() {
digitalWrite(relay, HIGH);
char btn = key.getKey();
if (btn) {
String cstring = String(str);
str[q++] = btn;
if (btn == 'A') {
lcd.clear();
beli = beli + 10000;
lcd.setCursor(0, 0);
lcd.print("BELI BBM : ");
lcd.setCursor(0, 1);
lcd.print("Rp.");
lcd.print(beli);
}
else if (btn == 'B') {
lcd.clear();
beli = beli + 5000;
lcd.setCursor(0, 0);
lcd.print("BELI BBM : ");
lcd.setCursor(0, 1);
lcd.print("Rp.");
lcd.print(beli);
}
else if (btn == 'C' && beli != 0) {
for (j = 0; j <= 10; j++) {
stringAngka[j] = NULL;
}
lcd.clear();
beli = 0;
i = 0;
lcd.setCursor(0, 0);
lcd.print("BELI BBM : ");
lcd.setCursor(0, 1);
lcd.print("Rp.");
lcd.print(beli);
}
else if (btn == 'D') {
a:
for (j = 0; j <= 10; j++) {
stringAngka[j] = NULL;
harga2[j] = NULL;
}
lcd.clear();
harga5 = 0;
beli = 0;
pass = 0;
i = 0;
lcd.setCursor(0, 0);
lcd.print("POM MINI");
lcd.setCursor(0, 1);
lcd.print("HARGA : Rp.");
lcd.print(harga);
lcd.print(" /L");
}
else if (btn == '#') {
double hargaA = 0, beliA = 0;
hargaA = harga, beliA = beli;
double l = beliA / hargaA;
harga3 = hargaA;;
Serial.println(i);
if (beli == 0) {
lcd.clear();
lcd.setCursor(0, 1);
digitalWrite(relay, LOW);
lcd.print("===MULAI MENGISI===");
delay(2000);
char tmbl;
while (btn == '#') {
tmbl = key.getKey();
if (tmbl == 'D') {
liter = 0;
goto a;
}
Serial.print(tmbl);
bbm_flow();
}
}
else if (beli > 0) {
lcd.clear();
lcd.setCursor(0, 1);
digitalWrite(relay, LOW);
lcd.print("===MULAI MENGISI===");
delay(2000);
while (liter <= l) {
bbm_flow();
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("VOLUME : ");
lcd.print(l);
lcd.print(" L");
lcd.setCursor(0, 2);
lcd.print("Harga : Rp.");
lcd.print(beli);
harga4 = 0;
liter = 0;
}
}
else if (pass == 2199) {
if (btn == '*') {
pass = NULL;
beli = NULL;
goto a;
}
if (btn == 'D') {
pass = NULL;
beli = NULL;
goto a;
}
else {
beli = NULL;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("UBAH HARGA");
harga2[p] = btn;
harga5 = atol(harga2);
lcd.setCursor(0, 1);
lcd.print("RP. ");
lcd.print(harga5);
Serial.println(harga5);
Serial.println(pass);
p++;
}
}
else {
stringAngka[i++] = btn;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BELI BBM : ");
lcd.setCursor(0, 1);
lcd.print("Rp.");
beli = atol(stringAngka);
lcd.print(beli);
pass = beli;
}
}
}
void bbm_flow() {
if ((millis() - oldTime) > 1000) {
detachInterrupt(sensorInt);
debit = ((1000.0 / (millis() - oldTime)) * pulseCount) / konstanta;
oldTime = millis();
flowmlt = (debit / 60) * 1000;
volume += flowmlt;
liter = volume / 1000;
harga4 = liter * harga3;
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("VOLUME : ");
lcd.print(liter);
lcd.print(" L");
lcd.setCursor(0, 2);
lcd.print("Harga : Rp.");
lcd.print(harga4);
delay (100); pulseCount = 0;
attachInterrupt(sensorInt, pulseCounter, FALLING);
}
delay(2000);
}
void pulseCounter() {
pulseCount++;
}