#include <Arduino.h>
#include "HX711.h"
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
// Define pin connections & motor's steps per revolution
const int dirPin = A0;
const int stepPin = A1;
const int stepsPerRevolution = 500;
int stepDelay = 1600;
// Load cell settings
const int LOADCELL_DOUT_PIN = A3;
const int LOADCELL_SCK_PIN = A2;
HX711 scale;
// LCD settings
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS); // Address 0x27 for 20x4 LCD
void setup() {
Serial.begin(57600);
Serial.println("HX711 Demo");
Serial.println("Initializing the scale");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println("Before setting up the scale:");
// Serial.print("read: \t\t");
// Serial.println(scale.read()); // print a raw reading from the ADC
// Serial.print("read average: \t\t");
// Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
// Serial.print("get value: \t\t");
// Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
// Serial.print("get units: \t\t");
// Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided by the SCALE parameter (not set yet)
scale.set_scale(-0.4);
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
// Serial.print("read: \t\t");
// Serial.println(scale.read()); // print a raw reading from the ADC
// Serial.print("read average: \t\t");
// Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
// Serial.print("get value: \t\t");
// Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
// Serial.print("get units: \t\t");
// Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// Serial.println("Readings:");
// Initialize LCD
lcd.init();
lcd.backlight();
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Counterclockwise rotation
digitalWrite(dirPin, LOW);
// Read weight from load cell
float weight = abs(scale.get_units(5)); // Get absolute value of the average of 5 readings
// Serial.print("one reading:\t");
// Serial.print(weight, 1);
// Display weight on LCD
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(weight, 1);
lcd.print(" g");
// Spin motor continuously if weight is less than or equal to 15
while (weight <= 15) { // Loop runs indefinitely
for (int x = 0; x < stepsPerRevolution; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
// Update weight reading
weight = abs(scale.get_units(5)); // Get absolute value of the average of 5 readings
// Serial.print("\tone reading:\t");
// Serial.println(weight, 1);
}
// Stop the motor if weight exceeds 15
digitalWrite(stepPin, LOW);
// Clear LCD after weight exceeds 15
lcd.clear();
delay(0);
}