#include <HX711.h>
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Load Cell
#define DT A1
#define SCK A0
HX711 scale;
// Servo
Servo myServo;
int servoPin = 9;
// Buzzer & Limit Switch
const int buzzerPin = 10;
const int limitSwitchPin = 8;
// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad 4x4
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'}, // ← tombol 'C' sebagai HAPUS
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, A2, A3}; // Tambahan A3 untuk kolom ke-4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variabel Input
String inputBerat = "";
float targetBerat = 0;
void setup() {
Serial.begin(9600);
scale.begin(DT, SCK);
scale.set_scale();
scale.tare();
myServo.attach(servoPin);
myServo.write(0);
pinMode(buzzerPin, OUTPUT);
pinMode(limitSwitchPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Sistem Penakar");
lcd.setCursor(0, 1);
lcd.print("Teh Siap...");
delay(1500);
lcd.clear();
}
void loop() {
if (digitalRead(limitSwitchPin) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Teh Habis!");
lcd.setCursor(0, 1);
lcd.print("Isi Wadah Dulu");
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
return;
}
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
if (inputBerat.length() < 4) {
inputBerat += key;
lcd.setCursor(0, 0);
lcd.print("Input Berat:");
lcd.setCursor(0, 1);
lcd.print(inputBerat + "g ");
}
} else if (key == '*') { // Enter
if (inputBerat.length() > 0) {
targetBerat = inputBerat.toFloat();
inputBerat = ""; // reset input
scale.tare();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Menakar: ");
lcd.print(targetBerat, 0);
lcd.print("g");
Serial.print("Menakar: ");
Serial.print(targetBerat);
Serial.println(" gram");
bukaWadah();
while (scale.get_units() < targetBerat) {
float berat = scale.get_units();
Serial.print("Berat sekarang: ");
Serial.print(berat, 2);
Serial.println(" g");
lcd.setCursor(0, 1);
lcd.print("Berat: ");
lcd.print(berat, 2);
lcd.print("g ");
delay(300);
}
tutupWadah();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Penakaran");
lcd.setCursor(0, 1);
lcd.print("Selesai!");
Serial.println("Penakaran selesai!");
delay(1500);
lcd.clear();
}
} else if (key == '#') { // Tare manual
scale.tare();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timbangan");
lcd.setCursor(0, 1);
lcd.print("direset ke 0");
Serial.println("Timbangan direset ke 0");
delay(1500);
lcd.clear();
} else if (key == 'C') { // Hapus karakter terakhir
if (inputBerat.length() > 0) {
inputBerat.remove(inputBerat.length() - 1);
lcd.setCursor(0, 0);
lcd.print("Input Berat:");
lcd.setCursor(0, 1);
lcd.print(inputBerat + "g ");
}
}
}
}
void bukaWadah() {
myServo.write(90);
delay(500);
}
void tutupWadah() {
myServo.write(0);
delay(500);
}