I would like to improve my code! I need to classify a bunch of number into range

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();
  
}

I think you need to describe what you code is doing more clearly and how this varies from what you want. What is a 3/4 string?

Where does previousMeasurement get updated as the code progresses?

Right! My code works in that way:

I used one load cell to detect the power applied to the bag when I hit it, and through the load cell I'm able to record the power on it calculate in kg.
Then, the code shows up to me strings into serial monitor and each of it has the value that is came out in kg.
The problem is that just with a punch given to the bag I get 3 strings of values on the serial monitor instead of just one showing me the highest value.

this is what I receive:

Mmmh... I think I wrong here, how do you suggest to do ?

Not only. The startedRecording variable also doesn't change anywhere, and I don't see what it's for at all. The whole program is weird to me, or maybe I just don't understand what the OP wants.

Power is measured in Watts or KiloWatts, not kilogrammes. Perhaps you should explain what is going on in your project is about.

I don’t understand what your code is attempting (and failing) to achieve.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.