#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <HX711_ADC.h>
// Inisialisasi LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Inisialisasi keypad
const byte ROWS = 4; // 4 baris
const byte COLS = 4; // 4 kolom
char keys[ROWS][COLS] = {
{'1', '4', '7', '*'},
{'2', '5', '8', '0'},
{'3', '6', '9', '#'},
{'A', 'B', 'C', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; // Pin baris pada keypad
byte colPins[COLS] = {9, 8, 7, 6}; // Pin kolom pada keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
unsigned long memasukkanangka = 0; // Variabel untuk menyimpan angka yang diinput dari keypad
bool hapustampilan = false; // Variabel untuk menentukan apakah tampilan harus dihapus
bool hasilnya = false; // Variabel untuk menentukan apakah perlu melakukan perkalian
bool perhitungan = false; // Variabel untuk menandakan sedang dalam proses perhitungan
// Inisialisasi Load Cell
#define HX711_dout 10 //Mendefinisikan konstanta HX711_dout
#define HX711_sck 11 //Mendefinisikan konstanta HX711_sck
HX711_ADC LoadCell(HX711_dout, HX711_sck);
float beratbautperbuah = 0; // Variabel untuk menyimpan nilai berat baut perbuah
float beratTotal = 0;
int jumlahbaut = 0; // Variabel untuk menyimpan jumlah baut
float harga = 0; // Variabel untuk menyimpan harga per barang
void setup() {
Serial.begin(9600); // Menginisialisasi komunikasi serial dengan baud rate 9600
// Menginisialisasi LCD
lcd.init();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("PROJEK TIMBANGAN");
lcd.setCursor(1, 1);
lcd.print("ABSEN GANJIL TE 2C");
lcd.setCursor(1, 2);
lcd.print("POLITEKNIK NEGERI ");
lcd.setCursor(6, 3);
lcd.print("CILACAP ");
delay(5000);
lcd.clear();
LoadCell.begin(); // Memulai koneksi dengan sensor load cell
LoadCell.start(500); // Mengatur jeda antara setiap bacaan nilai dari sensor, dalam milidetik
LoadCell.setCalFactor(410.0); // Mengatur faktor kalibrasi sensor dengan nilai 1.00
LoadCell.tare();
Serial.println("Ready"); // Menampilkan pesan "Ready" melalui komunikasi serial
lcd.setCursor(0, 0);
lcd.print("SILAHKAN TEKAN");
lcd.setCursor(0, 1);
lcd.print("A = Berat Satuan");
lcd.setCursor(0, 2);
lcd.print("B = Berat Total");
lcd.setCursor(0, 3);
lcd.print("C = Masukan Harga");
}
void loop() {
char key = keypad.getKey(); // Membaca tombol yang ditekan pada keypad
if (key) {
Serial.println(key); // Menampilkan tombol yang ditekan melalui Serial Monitor
if (key == 'A') { // Jika tombol A ditekan
beratbautperbuah = read_sensor();
Serial.print("Berat satuan: ");
Serial.print(beratbautperbuah);
Serial.println(" gr");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Berat: ");
lcd.print(beratbautperbuah);
lcd.print(" g");
}
if (key == 'B') { // Jika tombol B ditekan
LoadCell.tareNoDelay(); // Tare without delay
delay(1000); // Wait for the scale to stabilize
while (!LoadCell.update()); // Ensure the load cell is ready
beratTotal = read_sensor();
Serial.print("Berat total: ");
Serial.print(beratTotal);
Serial.println(" gr");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Berat Total: ");
lcd.print(beratTotal);
lcd.print(" g");
}
if (key == 'C') { // Jika tombol C ditekan
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan harga:");
memasukkanangka = 0;
while (true) {
char numKey = keypad.getKey();
if (numKey) {
if (numKey == '#') break; // Selesai memasukkan angka
if (numKey == '*') { // Menghapus digit terakhir
memasukkanangka = memasukkanangka / 10;
lcd.setCursor(0, 1);
lcd.print("Harga: ");
lcd.print(memasukkanangka);
lcd.print(" "); // Menghapus karakter sebelumnya
} else if (isDigit(numKey)) { // Menambahkan digit baru
memasukkanangka = memasukkanangka * 10 + (numKey - '0');
lcd.setCursor(0, 1);
lcd.print("Harga: ");
lcd.print(memasukkanangka);
}
}
}
harga = memasukkanangka;
Serial.print("Harga: ");
Serial.println(harga);
}
if (key == 'D') { // Jika tombol D ditekan
int jumlahbaut = beratTotal / beratbautperbuah;
int totalHarga = harga * jumlahbaut;
Serial.print("Berat total: ");
Serial.print(beratTotal);
Serial.println(" gram");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Berat: ");
lcd.print(beratbautperbuah);
lcd.print(" g");
lcd.setCursor(0, 1);
lcd.print("Jumlah: ");
lcd.print(jumlahbaut);
lcd.setCursor(0, 2);
lcd.print("Harga Total: ");
lcd.print(totalHarga);
lcd.setCursor(0, 3);
lcd.print("Berat Total: ");
lcd.print(beratTotal);
}
}
LoadCell.update(); // Memperbarui nilai bacaan dari sensor load cell
}
float read_sensor() {
Serial.println("Mengukur berat...");
float totalberatbaut = 0;
int numReadings = 100;
for (int i = 0; i < 100; i++) { // Mengambil rata-rata dari 100 bacaan
LoadCell.update();
totalberatbaut += LoadCell.getData();
delay(10);
}
return totalberatbaut / numReadings;
}