#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const int ROW_NUM = 4; // jumlah baris keypad
const int COLUMN_NUM = 4; // jumlah kolom keypad
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','P1'},
{'4','5','6','P2'},
{'7','8','9','P3'},
{'*','0','#','P4'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // koneksi pin baris keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // koneksi pin kolom keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD
float hargaPerLiter = 10000; // Harga per liter bahan bakar
float liter = 0;
float totalHarga = 0;
void setup() {
lcd.begin(16,2);
lcd.print("Liter: 0.00");
lcd.setCursor(0,1);
lcd.print("Harga: 0.00");
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Tombol # untuk mereset counter
liter = 0;
totalHarga = 0;
} else if (key == '*') {
// Tombol * untuk mengurangkan satu liter
liter -= 1;
if (liter < 0) {
liter = 0;
}
totalHarga = liter * hargaPerLiter;
} else if (key == 'P1' || key == 'P2' || key == 'P3' || key == 'P4') {
// Pembelian paket
int paketSize = key - '0'; // Mendapatkan jumlah liter dari karakter terakhir
liter += paketSize;
totalHarga = liter * hargaPerLiter;
} else if (key >= '0' && key <= '9') {
// Menambah liter dan menghitung total harga
liter = liter * 10 + (key - '0');
totalHarga = liter * hargaPerLiter;
}
// Update tampilan LCD
lcd.setCursor(7, 0);
lcd.print(liter, 2);
lcd.setCursor(7, 1);
lcd.print(totalHarga, 2);
}
}