#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi objek LCD I2C dengan alamat 0x27, 16 kolom, dan 2 baris
const byte ROWS = 4; // Jumlah baris keypad
const byte COLS = 4; // Jumlah kolom keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // Hubungkan pin baris keypad ke pin Arduino (2, 3, 4, 5)
byte colPins[COLS] = {6, 7, 8, 9}; // Hubungkan pin kolom keypad ke pin Arduino (6, 7, 8, 9)
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
long rupiah = 0;
long hargaPerKg = 15000;
void setup() {
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Nyalakan backlight LCD
lcd.clear();
showStartupMessage();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Rupiah:");
}
void loop() {
inputRupiah();
}
void showStartupMessage() {
lcd.setCursor(4, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(0, 1);
String message = " Beras Kiloan";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
String inputRupiah() {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
rupiah = rupiah * 10 + (key - '0'); // Menggunakan digit yang dimasukkan untuk membangun jumlah rupiah
lcd.setCursor(0, 1);
lcd.print(" "); // Hapus teks sebelumnya
lcd.setCursor(0, 1);
lcd.print(rupiah);
} else if (key == 'D') {
// Hitung jumlah kilogram berdasarkan jumlah rupiah
float kilogram = float(rupiah) / hargaPerKg;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rupiah: ");
lcd.print(rupiah);
lcd.setCursor(0, 1);
lcd.print("Kilogram: ");
lcd.print(kilogram);
delay(5000); // Tampilkan hasil selama 5 detik
lcd.clear();
showStartupMessage();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Rupiah:");
} else if (key == '*') {
rupiah = rupiah / 10; // Hapus digit terakhir
lcd.setCursor(0, 1);
lcd.print(" "); // Hapus digit terakhir dari tampilan LCD
lcd.setCursor(0, 1);
lcd.print(rupiah);
}
}
}