#include <HX711.h>
#include <LiquidCrystal.h>
#include <Servo.h>
// HX711 circuit wiring for the first load cell
const int LOADCELL1_DOUT_PIN = 3;
const int LOADCELL1_SCK_PIN = 2;
// HX711 circuit wiring for the second load cell
const int LOADCELL2_DOUT_PIN = A1; // Use a different analog pin for the second load cell
const int LOADCELL2_SCK_PIN = A0;
// Parameters for calibration
const long LOADCELL_OFFSET = 0;
const float LOADCELL_DIVIDER = 428.0; // Adjust this divider to calibrate the scale
// Alarm settings
const int BUZZER_PIN = 4;
const int ALARM_WEIGHT_THRESHOLD = 500; // Weight threshold for the alarm in grams
// LED settings
const int GREEN_LED_PIN = 12;
const int RED_LED_PIN = 13;
// LCD settings
const int LCD_RS = 5;
const int LCD_EN = 6;
const int LCD_D4 = 7;
const int LCD_D5 = 8;
const int LCD_D6 = 9;
const int LCD_D7 = 10;
// Servo settings
const int SERVO_PIN = 11;
const int SERVO_CLOSED_POSITION = 0;
const int SERVO_OPEN_POSITION = 90;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
HX711 scale1;
HX711 scale2; // Create a second instance for the second load cell
Servo gateServo;
void setup() {
Serial.begin(9600);
scale1.begin(LOADCELL1_DOUT_PIN, LOADCELL1_SCK_PIN);
scale2.begin(LOADCELL2_DOUT_PIN, LOADCELL2_SCK_PIN); // Initialize the second HX711
scale1.set_scale(LOADCELL_DIVIDER);
scale2.set_scale(LOADCELL_DIVIDER); // Set the scale for the second HX711
scale1.tare(); // Reset the first scale to zero
scale2.tare(); // Reset the second scale to zero
pinMode(BUZZER_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on the green LED by default
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
gateServo.attach(SERVO_PIN); // Attach the servo motor to its pin
gateServo.write(SERVO_OPEN_POSITION); // Start with the gate open
}
void loop() {
float weight1 = scale1.get_units(10); // Average 10 readings for stability from the first load cell
float weight2 = scale2.get_units(10); // Average 10 readings for stability from the second load cell
float totalWeight = (weight1 + weight2) * 1000; // Convert total weight to grams
if (totalWeight > ALARM_WEIGHT_THRESHOLD) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Overload!");
digitalWrite(BUZZER_PIN, HIGH); // Trigger the buzzer alarm
digitalWrite(GREEN_LED_PIN, LOW); // Turn off the green LED
digitalWrite(RED_LED_PIN, HIGH); // Turn on the red LED
gateServo.write(SERVO_CLOSED_POSITION); // Close the gate
} else {
Serial.print("Weight: ");
Serial.print(totalWeight);
Serial.println(" g"); // Display the unit as grams
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight:");
lcd.setCursor(0, 1);
lcd.print(totalWeight);
lcd.print(" g"); // Display the unit as grams
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
digitalWrite(RED_LED_PIN, LOW); // Turn off the red LED
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on the green LED
gateServo.write(SERVO_OPEN_POSITION); // Keep the gate open
}
delay(500); // Wait for a bit before reading again
}