Hi everyone !
As I said in the title I would like to improve my code, actually it works but not pretty well because it runs the recording but for one punch to the load cell I receive 3/4 string of load detected, I wish it had the possibility to read all these strings and then draws only the higher value that is the one it will show up in the serial monitor and even the one it takes into account for classifying the power in the ranges which are in the next step.
Some of you knows how to improve this code base on what I described above ?
#include<Keyboard.h>
#include <HX711.h>
#define calibration_factor -9850
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
// Dichiarazione delle variabili
HX711 scale;
float measurement;
float previousMeasurement;
float highMeasurement;
bool startedRecording;
void setup() {
Serial.begin(9600);
Keyboard.begin();
while(!Serial);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
measurement = 0;
previousMeasurement = 1;
highMeasurement = 0;
startedRecording = true;
Serial.println("Waiting for Highest Measurement...");
}
void loop() {
measurement = scale.get_units();
delay(100);
highMeasurement = measurement;
if (( measurement > previousMeasurement ) && (measurement < 10.9)) {
if (measurement < 4.9) {
Keyboard.press('w');
Keyboard.release('w');
Keyboard.end();
}
else if (measurement < 7.9) {
Keyboard.press('w');
delay(900);
Keyboard.release('w');
Keyboard.end();
}
else if (measurement < 10.9) {
Keyboard.press('w');
delay(1500);
Keyboard.release('w');
Keyboard.end();
}
if (startedRecording){
finish();
}
}
}
void finish(){
Serial.print("Highest Measurement: ");
Serial.print(highMeasurement, 1);
Serial.print("Kg");
Serial.println();
}
