#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD object with the I2C address, columns, and rows
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with the I2C address of your LCD
// Define pin assignments for first HX711
#define DT1 A0
#define SCK1 A1
// Define pin assignments for second HX711
#define DT2 A2
#define SCK2 A3
#define sw A4
// Declare global variables
long sample1 = 0; // Sample for load cell 1
long sample2 = 0; // Sample for load cell 2
float val1 = 0; // Calibration value for load cell 1
float val2 = 0; // Calibration value for load cell 2
long count1 = 0; // Raw count for load cell 1
long count2 = 0; // Raw count for load cell 2
// Function to read the load cell count for first HX711
unsigned long readCount1(void) {
unsigned long Count;
unsigned char i;
pinMode(DT1, OUTPUT);
digitalWrite(DT1, HIGH);
digitalWrite(SCK1, LOW);
Count = 0;
pinMode(DT1, INPUT);
// Wait until DT becomes LOW
while (digitalRead(DT1))
;
for (i = 0; i < 24; i++) {
digitalWrite(SCK1, HIGH);
Count = Count << 1;
digitalWrite(SCK1, LOW);
if (digitalRead(DT1))
Count++;
}
digitalWrite(SCK1, HIGH);
Count = Count ^ 0x800000;
digitalWrite(SCK1, LOW);
return (Count);
}
// Function to read the load cell count for second HX711
unsigned long readCount2(void) {
unsigned long Count;
unsigned char i;
pinMode(DT2, OUTPUT);
digitalWrite(DT2, HIGH);
digitalWrite(SCK2, LOW);
Count = 0;
pinMode(DT2, INPUT);
// Wait until DT becomes LOW
while (digitalRead(DT2))
;
for (i = 0; i < 24; i++) {
digitalWrite(SCK2, HIGH);
Count = Count << 1;
digitalWrite(SCK2, LOW);
if (digitalRead(DT2))
Count++;
}
digitalWrite(SCK2, HIGH);
Count = Count ^ 0x800000;
digitalWrite(SCK2, LOW);
return (Count);
}
// Function to calibrate the scale
void calibrate() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Place known weight");
lcd.setCursor(0, 1);
lcd.print("on the scale");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibrating...");
// Assuming you want to calibrate to 580 for both load cells
val1 = 580;
val2 = 580;
for (int i = 0; i < 10; i++) {
sample1 += readCount1();
sample2 += readCount2();
delay(500);
}
sample1 /= 10;
sample2 /= 10;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibration done");
delay(1000);
lcd.clear();
}
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // initialize the lcd
lcd.init();
lcd.backlight();
pinMode(SCK1, OUTPUT);
pinMode(SCK2, OUTPUT);
pinMode(sw, INPUT_PULLUP);
lcd.print(" Weight ");
lcd.setCursor(0, 1);
lcd.print(" Measurement ");
delay(1000);
lcd.clear();
calibrate();
}
void loop() {
count1 = readCount1();
count2 = readCount2();
int w1 = (((count1 - sample1) / val1));
int w2 = (((count2 - sample2) / val2));
Serial.print("weight1:");
Serial.print((int)w1);
Serial.print("g ");
Serial.print("weight2:");
Serial.print((int)w2);
Serial.println("g");
lcd.setCursor(0, 0);
lcd.print("Weight1 ");
lcd.setCursor(0, 1);
lcd.print(w1);
lcd.print("g ");
lcd.setCursor(8, 0);
lcd.print("Weight2 ");
lcd.setCursor(8, 1);
lcd.print(w2);
lcd.print("g ");
if (digitalRead(sw) == 0) {
// Capture current weight and add it to calibration
long zeroWeight1 = readCount1();
long zeroWeight2 = readCount2();
sample1 += zeroWeight1;
sample2 += zeroWeight2;
calibrate(); // recalibrate with zero added
}
}