// SMART LOAD SAFETY - ESP32, 2 lantai
// Library: HX711 (bogde), LiquidCrystal_I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
// ===================== PIN =====================
#define PIN_BUZZER 13
#define PIN_BUTTON 22
#define LCD_SDA 2
#define LCD_SCL 15
#define LCD_ADDR 0x27
// Level saat tombol ditekan (HIGH jika tombol ke 3V3 + pull-down, LOW jika ke GND + pull-up)
#define BUTTON_PRESSED_LEVEL HIGH
// ===================== RENTANG BEBAN (kg) - EDIT DI SINI =====================
// Aman : x < kuning
// Waspada: kuning <= x < merah
// Bahaya : x >= merah
// kuning merah batas tampil
const float LT1_KUNING = 1.2, LT1_MERAH = 1.6, LT1_MAX = 2.0; // Lantai 1 (load cell kanan)
const float LT2_KUNING = 0.6, LT2_MERAH = 0.8, LT2_MAX = 1.0; // Lantai 2 (load cell kiri)
// ===================== KALIBRASI LOAD CELL =====================
// Isi dengan hasil kalibrasi masing-masing load cell
const float CAL_KANAN = 420.0;
const float CAL_KIRI = 420.0;
// ===================== STRUKTUR LANTAI =====================
struct Lantai {
HX711 scale;
uint8_t dt, sck;
uint8_t ledG, ledY, ledR;
float kuning, merah, maxKg;
float calFactor;
float berat = 0;
uint8_t status = 0; // 0 aman, 1 waspada, 2 bahaya
};
Lantai lt[2]; // lt[0] = lantai 1 (kanan), lt[1] = lantai 2 (kiri)
const char* STATUS_TXT[] = {"AMAN", "WASPADA", "BAHAYA"};
LiquidCrystal_I2C lcd(LCD_ADDR, 20, 4);
uint8_t page = 0;
void setupLantai(Lantai &l, uint8_t dt, uint8_t sck, uint8_t g, uint8_t y, uint8_t r,
float kuning, float merah, float maxKg, float cal) {
l.dt = dt; l.sck = sck; l.ledG = g; l.ledY = y; l.ledR = r;
l.kuning = kuning; l.merah = merah; l.maxKg = maxKg; l.calFactor = cal;
pinMode(g, OUTPUT); pinMode(y, OUTPUT); pinMode(r, OUTPUT);
l.scale.begin(dt, sck);
l.scale.set_scale(cal);
l.scale.tare();
}
void updateLantai(Lantai &l) {
if (l.scale.is_ready()) {
float w = l.scale.get_units(3);
l.berat = (w < 0) ? 0 : w;
}
l.status = (l.berat >= l.merah) ? 2 : (l.berat >= l.kuning) ? 1 : 0;
digitalWrite(l.ledG, l.status == 0);
digitalWrite(l.ledY, l.status == 1);
digitalWrite(l.ledR, l.status == 2);
}
void tampilLCD() {
Lantai &l = lt[page];
char buf[21];
lcd.setCursor(0, 0); lcd.print(F(" SMART LOAD SAFETY "));
snprintf(buf, sizeof(buf), "Lantai %d/2 ", page + 1);
lcd.setCursor(0, 1); lcd.print(buf);
char w[8];
dtostrf(l.berat, 4, 2, w);
snprintf(buf, sizeof(buf), "Beban %s/%.0fkg ", w, l.maxKg);
lcd.setCursor(0, 2); lcd.print(buf);
snprintf(buf, sizeof(buf), "Status: %-12s", STATUS_TXT[l.status]);
lcd.setCursor(0, 3); lcd.print(buf);
}
void cekTombol() {
static bool lastState = !BUTTON_PRESSED_LEVEL;
static uint32_t lastChange = 0;
bool s = digitalRead(PIN_BUTTON);
if (s != lastState && millis() - lastChange > 50) { // debounce
lastChange = millis();
lastState = s;
if (s == BUTTON_PRESSED_LEVEL) {
page = !page;
lcd.clear();
tampilLCD();
}
}
}
void setup() {
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
Wire.begin(LCD_SDA, LCD_SCL);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 1); lcd.print(F(" Kalibrasi/Tare... "));
// DT SCK G Y R
setupLantai(lt[0], 27, 14, 17, 16, 4, LT1_KUNING, LT1_MERAH, LT1_MAX, CAL_KANAN); // kanan = lantai 1
setupLantai(lt[1], 25, 26, 19, 18, 5, LT2_KUNING, LT2_MERAH, LT2_MAX, CAL_KIRI); // kiri = lantai 2
lcd.clear();
}
void loop() {
static uint32_t lastLcd = 0;
cekTombol();
for (auto &l : lt) updateLantai(l);
// Buzzer nyala jika salah satu lampu merah menyala
digitalWrite(PIN_BUZZER, (lt[0].status == 2 || lt[1].status == 2));
if (millis() - lastLcd >= 300) {
lastLcd = millis();
tampilLCD();
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4