#include <HX711.h>
const int ld_cell = 27;
const int ld_sck = 28;
char flag;
char ans;
float Gain = 1;
HX711 scale;
void setup() {
Serial1.begin(115200);
scale.begin(ld_cell, ld_sck);
Serial1.println("");
Serial1.println("::: Force sensor :::");
Serial1.println("For offset compensation, press 'o' ;");
Serial1.println("For gain calibration, press 'g' ;");
Serial1.println("To start and stop real-time measurement, press 's' ;");
Serial1.println("::: end :::");
Serial1.println("");
}
void loop() {
scale.set_scale();
while (Serial1.available() == 0) {
}
flag = Serial1.read();
if (flag == 'o') {
OffsetComp();
}
if (flag == 'g') {
GainCalib();
}
if (flag == 's') {
StartStop();
}
delay(1); // this speeds up the simulation
}
void OffsetComp() {
Serial1.println("Offset compensation - remove wire and leave every static load (pulley, bracket...)");
delay(500);
Serial1.println("Press 'd' when done");
ans = 'a';
while (ans != 'd') {
if (Serial1.available() > 0) {
ans = Serial1.read();
}
}
scale.tare();
Serial1.println("Tare done");
}
/* void GainChoice() {
Serial1.println("Gain calibration...");
delay(500);
Serial1.println("Do offset compensation first ?");
Serial1.println("For 'yes', press 'y' ;");
Serial1.println("For 'no', press 'n' ;");
while (Serial.available()==0) {
}
ans = Serial1.read();
if(ans=='n') {
GainCalib();
}
if(ans=='y') {
OffsetComp();
GainCalib();
}
} */
void GainCalib() {
Serial1.println("Gain calibration - add wire with weight ;");
Serial1.println("When stabilized, enter the weight value in kg :");
int weight = 0;
while (weight == 0) {
if (Serial1.available() > 0) {
weight = Serial1.parseInt();
}
}
Serial1.print("Weight = "); Serial1.print(weight); Serial1.println(" kg");
Gain = scale.get_units(10) / weight;
Serial1.print("Gain = "); Serial1.println(Gain);
Serial1.println("Gain calibration done");
}
void StartStop() {
Serial1.println("Reading");
delay(500);
char stop = 'a';
while (stop != 's') {
long reading = scale.get_units(10);
Serial1.println(reading / Gain);
if (Serial1.available() > 0) {
stop = Serial1.read();
}
}
Serial1.println("Stop reading");
}