#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "Keypad.h"
#include <HX711.h>
#include <Servo.h>
#define CALIBRATION_FACTOR 813.00 // ganti (813.00) dengan hasil yang didapat dari kalibrasi tadi
byte pinDt = 2; // DOUT
byte pinSck = 4; // SCK
HX711 scale;
const byte ROWS = 4; //empat rows
const byte COLS = 4; //empat columns
char hexaKeys[ROWS][COLS] =
{ {'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8,7,6,5}; //hubungkan ke row pinout keypad
byte colPins[COLS] = {12,11,10,9}; //hubungkan ke column pinout keypad
Servo motor;
int pos = 0;
int count = 0;
int grains = 0;
float Clear = 'A'; // tombol A untuk membersihkan input keypad
float Delete = 'B'; // tombol B untuk menghapus angka terakhir input keypad
float Tare = 'C'; // tombol C untuk membersihkan LCD
float Enter = 'D'; // tombol D untuk mengaktifkan motor servo
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16,2); // Set the LCD I2C address for mjkdz I2C module
void setup()
{
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.print("TIMBANGAN DIGITAL");
delay(1000);
lcd.clear();
motor.attach(3);
scale.begin(pinDt, pinSck);
scale.set_scale(CALIBRATION_FACTOR); // this value is obtained by calibrating the scale with known weights;
scale.tare();
}
void loop()
{
{
char key = customKeypad.getKey();
if (key)
{
lcd.setCursor (count,0);
lcd.print (key);
lcd.setCursor (0,0);
count ++;
if (key == Clear) // Membersihkan LCd
{
lcd.clear();
count = 0;
}
if (key == Delete) // Menghapus angka terakhir pada LCD
{
lcd.setCursor(count--, 0);
lcd.print(" ");
lcd.setCursor(count--, 0);
lcd.print(" ");
}
if (key == Tare) // scale tare ke 0
{
lcd.clear();
count = 0;
scale.tare();
}
if (key == Enter) // mengontrol servo jika berat sudah sesuai dengan input keypad
{
for (pos = 0; pos <= 180; pos += 1) { // servo brputar dari 0 derajat ke 180 derajat
// in steps of 1 degree
motor.write(pos);
delay(10);
}
for (pos = 180; pos >= 0; pos -= 1) { // lalu berputar kembali ke posisi awal
motor.write(pos);
}
}
}
}
{
lcd.setCursor(0, 1);
lcd.print(scale.get_units(2),0);
lcd.print(" Kilogram");
lcd.print(" done gasi ");
}
}