/* ESP32 WiFi Scanning example */
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
HX711 scale;
float calibration_factor;
void setup() {
Serial.begin(9600);
Serial.println("Initializing the scale");
scale.begin(2, 4);
scale.tare();
delay(5000);
}
float read_weight() {
scale.set_scale(84);
return scale.get_units(10); // Read averaged value over 10 samples
}
void compare_weights(float measured_weight, float input_weight) {
const float tolerance = 0.1; // Define a tolerance level
if (abs(measured_weight - input_weight) <= tolerance) {
Serial.println("Measured weight is approximately equal to the input weight.");
} else {
Serial.println("Measured weight does not match the input weight.");
}
}
void loop() {
static bool input_received = false;
static float input_weight = 0.0;
if (!input_received) {
Serial.print("Enter the input weight: ");
while (!Serial.available()) {
}
input_weight = Serial.parseFloat();
input_received = true;
// Read weight and compare
float measured_weight = read_weight();
compare_weights(measured_weight, input_weight);
Serial.println("Input weight has been compared. Enter a new weight:");
}
Serial.print("Measured weight: ");
Serial.println(read_weight());
input_received = false;
delay(5000);
}