#include <Wire.h>
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define I2C_ADDR 0x27 //27 heksadesimal
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Pin konfigurasi untuk HX711
const int loadCell1DoutPin = 23; // DOUT pin load cell pertama
const int loadCell1SckPin = 18; // SCK pin load cell pertama
const int loadCell2DoutPin = 4; // DOUT pin load cell kedua
const int loadCell2SckPin = 2; // SCK pin load cell kedua
// const int loadCell3DoutPin = 19; // DOUT pin load cell kedua
// const int loadCell3SckPin = 14; // SCK pin load cell kedua
const int buzzerPin = 17;
Servo myservo;
int potpin = 33;
int val;
// const int loadCell3DoutPin = 12; // DOUT pin load cell kedua
// const int loadCell3SckPin = 15;
// const int loadCell5DoutPin = 12; // DOUT pin load cell kedua
// const int loadCell5SckPin = 15;
// const int loadCell6DoutPin = 16; // DOUT pin load cell kedua
// const int loadCell6SckPin = 0;
// const int loadCell7DoutPin = 19; // DOUT pin load cell kedua
// const int loadCell7SckPin = 5;
// penaman objek
HX711 scale1;
HX711 scale2;
// int 13 = 13; // Pin untuk komponen merah
// int 25 = 25; // Pin untuk komponen hijau
// int 26 = 26;
// int 27 = 27;
// int 32 = 32;
// HX711 scale3;
// HX711 scale4;
// HX711 scale5;
// HX711 scale6;
// HX711 scale7;
void setup() {
// Inisiasi Warna LED
pinMode(13, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(32, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.init();
lcd.backlight();
// Memulai codingan
Serial.begin(115200);
lcd.setCursor(0, 0);
lcd.print("Welcome!!");
lcd.setCursor(0, 1);
lcd.print("C6 SMIKA");
delay(1000);
lcd.clear();
// Inisialisasi load cell
scale1.begin(loadCell1DoutPin, loadCell1SckPin);
scale2.begin(loadCell2DoutPin, loadCell2SckPin);
myservo.attach(33);
// scale3.begin(loadCell3DoutPin, loadCell3SckPin);
// lcd.setCursor();
// scale2.begin(loadCell3DoutPin, loadCell3SckPin);
// scale2.begin(loadCell4DoutPin, loadCell4SckPin);
// scale1.begin(loadCell5DoutPin, loadCell5SckPin);
// scale2.begin(loadCell6DoutPin, loadCell6SckPin);
// scale2.begin(loadCell7DoutPin, loadCell7SckPin);
}
void loop() {
// Baca nilai berat dari load cell
float weight1 = scale1.get_units(10); // 10 pembacaan untuk smoothing
float weight2 = scale2.get_units(10);
// float weight3 = scale3.get_units(10);
// float weight3 = scale3.get_units(10);
// float weight4 = scale4.get_units(10);
// float weight5 = scale5.get_units(10); // 10 pembacaan untuk smoothing
// float weight6 = scale6.get_units(10);
// float weight7 = scale7.get_units(10); // 10 pembacaan untuk smoothing
float res1 = weight1 * 50 / 21000;
float res2 = weight2 * 50 / 21000;
// float res3 = weight3 * 50 / 21000;
float res = (res1 + res2 + 250);
lcd.setCursor(0, 1);
lcd.println(res); lcd.setCursor(7, 1);lcd.println(" kg");
// float res3 = weight3 * 50 / 21000;
// float res4 = weight4*50/21000;
// float res5 = weight5*50/21000;
// float res6 = weight6*50/21000;
// float res7 = weight7*50/21000;
// float result = res1+res2+res3+res4+res5+res6+res7;
// Tampilkan hasil pada Serial Monitor
// Serial.print("Berat Load Cell: ");
// Serial.println(res);
// // Serial.println(res1+res2);
// Serial.println(" kg");
int delayTime = 1000;
digitalWrite(buzzerPin, HIGH); // Nyalakan LED
delay(delayTime); // Tunggu
digitalWrite(buzzerPin, LOW); // Matikan LED
delay(delayTime); // Tunggu
if (res <= 250) {
digitalWrite(13, HIGH);
digitalWrite(25, LOW);
digitalWrite(26, LOW);
digitalWrite(27, LOW);
digitalWrite(32, LOW);
lcd.setCursor(0, 0);
lcd.println("Sangat Aman");
myservo.write(90);
delay(1000);
myservo.write(0);
delay(1000);
}
else if(res > 250 && res <= 280 ) {
digitalWrite(13, LOW);
digitalWrite(25, HIGH);
digitalWrite(26, LOW);
digitalWrite(27, LOW);
digitalWrite(32, LOW);
lcd.setCursor(0, 0);
lcd.println("Aman");
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
}
else if(res > 280 && res <= 300 ) {
digitalWrite(13, LOW);
digitalWrite(25, LOW);
digitalWrite(26, HIGH);
delay(1000);
digitalWrite(26, LOW);
delay(1000);
digitalWrite(27, LOW);
digitalWrite(32, LOW);
lcd.setCursor(0, 0);
lcd.println("Kurang Aman");
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
if (millis() % 5000 == 0) {
if (delayTime == 1000) {
delayTime = 500; // Ubah kecepatan menjadi lebih cepat
} else {
delayTime = 1000; // Kembalikan ke kecepatan awal
}
}
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
}
else if(res > 300 && res <= 310 ) {
digitalWrite(13, LOW);
digitalWrite(25, LOW);
digitalWrite(26, LOW);
digitalWrite(27, HIGH);
delay(1000);
digitalWrite(27, HIGH);
delay(500);
digitalWrite(27, HIGH);
delay(1000);
digitalWrite(27, HIGH);
delay(1000);
digitalWrite(32, LOW);
lcd.setCursor(0, 0);
lcd.println("Bahaya");
digitalWrite(buzzerPin, HIGH);
delay(1000);
noTone(buzzerPin);
delay(1000);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
delay(1000);
myservo.write(0);
// delay(1000);
}
else {
digitalWrite(13, LOW);
digitalWrite(25, LOW);
digitalWrite(26, LOW);
digitalWrite(27, LOW);
digitalWrite(32, HIGH);
delay(1000);
digitalWrite(32, LOW);
delay(1000);
digitalWrite(buzzerPin, HIGH);
lcd.setCursor(0, 0);
lcd.println("Kurangi");
digitalWrite(buzzerPin, HIGH);
delay(120);
noTone(buzzerPin);
delay(120);
tone(buzzerPin, 1000);
delay(120);
noTone(buzzerPin);
delay(120);
tone(buzzerPin, 1000);
delay(120);
noTone(buzzerPin);
delay(120);
tone(buzzerPin, 1000);
delay(120);
noTone(buzzerPin);
delay(120);
tone(buzzerPin, 1000);
delay(120);
noTone(buzzerPin);
delay(120);
noTone(buzzerPin);
delay(120);
myservo.write(0);
// delay(1000);
}
delay(0);
}