#include <HX711_ADC.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define LOADCELL_DOUT_PIN 2
#define LOADCELL_SCK_PIN 3
#define SERVO_PIN 9
#define RELAY_PIN 10
const float calibration_factor = 0.42; // Replace with your calibration factor
const float weight_thresholds[] = {100.0, 200.0, 300.0};
const uint8_t num_thresholds = sizeof(weight_thresholds) / sizeof(weight_thresholds[0]);
HX711_ADC loadCell(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
loadCell.begin();
loadCell.start(2000);
loadCell.setCalFactor(calibration_factor);
servo.attach(SERVO_PIN);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
static bool servo_on = false;
static bool relay_on = false;
loadCell.update();
float weight = loadCell.getData();
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(weight);
lcd.print(" g ");
if (!servo_on && !relay_on) {
for (uint8_t i = 0; i < num_thresholds; ++i) {
if (weight >= weight_thresholds[i]) {
servo_on = true;
relay_on = true;
servo.write(0); // Modify the angle as required
digitalWrite(RELAY_PIN, HIGH);
break;
}
}
}
delay(100);
if (servo_on || relay_on) {
bool below_all_thresholds = true;
for (uint8_t i = 0; i < num_thresholds; ++i) {
if (weight >= weight_thresholds[i]) {
below_all_thresholds = false;
break;
}
}
if (below_all_thresholds) {
servo_on = false;
relay_on = false;
servo.write(90); // Modify the angle as required to turn off the servo
digitalWrite(RELAY_PIN, LOW);
}
}
delay(100);
}