How clearly define clusters that have created?

I've coded a system that allows me how to classify the power applied to load cell sensor, everything work but the only thing I want to improve is the classification of power detected within the right range. Actually, the system works but usually it makes a mess and instead of print me the right letter it prints me another letter of another cluster, but the interesting thing is that the number of letter is correct, only the letter is wrong sometimes.

Is it possible to set better the range or there is something to do in order to fix this problem ?

Thanks in advance for reply me

void loop() {

    measurement = scale.get_units();

    if ( measurement < previousMeasurement )
    {
         startedRecording = true;
         highMeasurement = measurement;
         previousMeasurement = measurement;
         
    }


    else if (( measurement >= previousMeasurement) && (previousMeasurement != -1))
    {

         previousMeasurement = measurement;
         Serial.print("Highest Measurement:  ");
         Serial.print(-highMeasurement, 1);
         Serial.print("Kg");
         Serial.println();
    
         

         //NOT STRONG
         if ((measurement < -2 ) && (measurement > -10))
         {
       
          
              if (( measurement < -2 ) && (measurement > -4)) {

                    while(1) {
                    Keyboard.begin();
                    Keyboard.press('w');
                    delay(1000);
                    Keyboard.release('w');
                    Keyboard.end();
                    break;
                    delay(1000);
                    }
              }


              else if (( measurement < -5 ) && (measurement > -7)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('w');
                    delay(2000);
                    Keyboard.release('w');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
                    
              }

              
              else if (( measurement < -8 ) && (measurement > -10)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('w');
                    delay(3000);
                    Keyboard.release('w');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
              }
            }



            


              // STRONG
              else if ( measurement < -11 && measurement > -19 ) 
              {

                   if (( measurement <= -11 ) && (measurement >= -13)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('s');
                    delay(1000);
                    Keyboard.release('s');
                    Keyboard.end();
                    break;
                    delay(1000);
                    }
              }


              else if (( measurement < -14 ) && (measurement > -16)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('s');
                    delay(2000);
                    Keyboard.release('s');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
              }

              
              else if (( measurement < -17 ) && (measurement > -19)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('s');
                    delay(3000);
                    Keyboard.release('s');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
                }
              }








              // VERY STRONG
              else if ( measurement < -20 && measurement > -30 ) 
              {

                   if (( measurement < -20 ) && (measurement > -23)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('a');
                    delay(1000);
                    Keyboard.release('a');
                    Keyboard.end();
                    break;
                    delay(1000);
                    }
              }


              else if (( measurement < -24 ) && (measurement > -26)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('a');
                    delay(2000);
                    Keyboard.release('a');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
              }

              
              else if (( measurement < -27 ) && (measurement > -30)) {

                    while(1) {
                    Keyboard.begin();  
                    Keyboard.press('a');
                    delay(3000);
                    Keyboard.release('a');
                    Keyboard.end();
                    break;
                    delay(1000); 
                    }
               }

             }

          }
       }   
  

Confused. Does it work, or doesn't work? What is the difference between "the system" and "the letter"?

The code has a lot of repetition which may be removed with an array. Also you are using while loops which have no function.

struct SCALE_RANGE
{
  int lowest, highest;
  char key;
  unsigned int key_delay;
};

const int NUM_RANGES = 2;

const SCALE_RANGE RANGES[NUM_RANGES] = {
  { -4, -2, 'w', 1000 }, //lowest, highest, key, key_delay
  { -7, -5, 'w', 2000 }
  //And so on..
};

void loop()
{
  for (int i = 0; i < NUM_RANGES; i++)
  {
    if ((measurement > RANGES[i].lowest) && (measurement < RANGES[i].highest))
    {
      keyboard.begin();
      keyboard.press(RANGES[i].key);
      delay(RANGES[i].key_delay);
      keyboard.release(RANGES[i].key);
      //delay(RANGES[i].key_delay);
      keyboard.end();
      break;
    }
  }
}

Code is untested but shows how to simplify the code with an array..

Work for half, whereas for system I mean the code behavior

I'll try, thank you !

I don't follow these double range tests. Usually classification is done by elimination, e.g.

if x>9
  do this
else if x>8
  do this
else if x>7
  do this
else if x>6
// when you reach this code section you know that x>6 and x<=7.
...

Can you see how single tests work with nested if-else? It is a lot easier and more foolproof (less chance of typos and logical errors).

The fact is that I'm working with a load cell that recording value in negative so more power is detected that means the value is more negative. About the ranges I need to classify power in real-time so I created three different ranges with a maximum and minimum accepted

There is no reason you can't do the same thing with negative values, if that is your objection. Also the classification system I showed, does work in real time.

if x< -9
  do this
else if x< -8
  do this
else if x< -7
  do this
else if x< -6
// when you reach this code section you know that x<-6 and x>=-7.
...

yes, that's understood

did you understand @anon57585045 comment?

how does the code ever exit from this this while (1) loop?
what are you trying to do?

break?

Then what does the 'while()' do? Leaving unanswered, the question, "what are you trying to do?".

this coding has gaps.
where are -4 and -5 recognized?

As already mentioned in #3, the while loop's have no function: While driving do brake.

when the value is 4.8 for example, Arduino print me " ww " it's like it is in the range of ```
(( measurement < -5 ) && (measurement > -7))

how about

    -29, weak   0, strong   0, veryStrong   1
    -33, weak   0, strong   0, veryStrong   2
    -13, weak   0, strong   1, veryStrong   2
     -4, weak   1, strong   1, veryStrong   2
    -12, weak   1, strong   2, veryStrong   2
     -8, weak   2, strong   2, veryStrong   2
    -18, weak   2, strong   3, veryStrong   2
    -10, weak   3, strong   3, veryStrong   2
    -13, weak   3, strong   4, veryStrong   2
     -3, weak   4, strong   4, veryStrong   2
    -24, weak   4, strong   4, veryStrong   3
     -5, weak   5, strong   4, veryStrong   3
    -32, weak   5, strong   4, veryStrong   4
    -30, weak   5, strong   4, veryStrong   5
     -9, weak   6, strong   4, veryStrong   5
    -13, weak   6, strong   5, veryStrong   5
    -29, weak   6, strong   5, veryStrong   6
    -21, weak   6, strong   5, veryStrong   7
    -32, weak   6, strong   5, veryStrong   8
    -34, weak   6, strong   5, veryStrong   9
    -35, weak   6, strong   5, veryStrong  10
    -33, weak   6, strong   5, veryStrong  11
    -19, weak   6, strong   6, veryStrong  11
    -35, weak   6, strong   6, veryStrong  12
     -4, weak   7, strong   6, veryStrong  12
    -23, weak   7, strong   6, veryStrong  13
     -9, weak   8, strong   6, veryStrong  13
    -34, weak   8, strong   6, veryStrong  14
    -28, weak   8, strong   6, veryStrong  15
    -39, weak   8, strong   6, veryStrong  16
    -37, weak   8, strong   6, veryStrong  17
    -34, weak   8, strong   6, veryStrong  18
    -20, weak   8, strong   7, veryStrong  18
    -35, weak   8, strong   7, veryStrong  19
    -40, weak   8, strong   7, veryStrong  20
    -25, weak   8, strong   7, veryStrong  21
    -39, weak   8, strong   7, veryStrong  22
    -13, weak   8, strong   8, veryStrong  22
    -11, weak   8, strong   9, veryStrong  22
    -11, weak   8, strong  10, veryStrong  22
    -31, weak   8, strong  10, veryStrong  23

int weak;
int strong;
int veryStrong;

char s [80];

// -----------------------------------------------------------------------------
void
check (
    int     measurement )
{
    static int previousMeasurement;

    if (measurement == previousMeasurement || -1 == previousMeasurement)
        return;

    if (-10 <= measurement)
        weak++;
    else if (-20 <= measurement)
        strong++;
    else
        veryStrong++;

    sprintf (s, " %6d, weak %3d, strong %3d, veryStrong %3d",
        measurement, weak, strong, veryStrong);
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);
    for (int m = -40; m <= 0; m += 1)  {
        check (random (-40, -2));
    }
}

void loop()
{
}

I put a while because I tried without and is less accurate, in fact when in the serial monitor I got 3 array of recording value that shows 3.5 power it prints me 3 times w instead one time, whereas with while it works a little bit better

The while loops does NOTHING, they cannot improve anything!

ok, got it !
anyway I tried the code you suggest me and works too but there are some problems even here.


#include<Keyboard.h>
#include <HX711.h>
#define calibration_factor   9850
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2

HX711 scale;

float measurement;
float previousMeasurement;
float highMeasurement;
bool startedRecording;



void setup() {

    Serial.begin(9600);
    while(!Serial);
 
    scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
    scale.set_scale(calibration_factor);
    scale.tare();

    measurement = 0;
    previousMeasurement = -1;
    highMeasurement = 0;
    startedRecording = false;
    Serial.println("Waiting for Highest Measurement...");

  
 }



struct SCALE_RANGE
{
  int lowest, highest;
  char key;
  unsigned int key_delay;
};


const int NUM_RANGES = 9;

const SCALE_RANGE RANGES[NUM_RANGES] = {
  { -4.9, -2, 'w', 1000 }, //lowest, highest, key, key_delay
  { -7.9, -5, 'w', 2000 },
  { -10.9, -6, 'w', 3000 } 

};

 

void loop()
{

  measurement = scale.get_units();

    if ( measurement < previousMeasurement )
    {
         startedRecording = true;
         highMeasurement = measurement;
         previousMeasurement = measurement;
         
    }


    else if (( measurement >= previousMeasurement) && (previousMeasurement != -1))
    { 
      previousMeasurement = measurement;
         Serial.print("Highest Measurement:  ");
         Serial.print(-highMeasurement, 1);
         Serial.print("Kg");
         Serial.println();
  
    for (int i = 0; i < NUM_RANGES; i++)
  {
    if ((measurement > RANGES[i].lowest) && (measurement < RANGES[i].highest))
    {
      Keyboard.begin();
      Keyboard.press(RANGES[i].key);
      delay(RANGES[i].key_delay);
      Keyboard.release(RANGES[i].key);
      //delay(RANGES[i].key_delay);
      Keyboard.end();
     }
   }
 }
}

Problems I'm speaking about are that some times the recording stuck and print more than it should and I've noticed that if it got power for example 14.5 it prints anyway but it shouldn't

Your brackets seems messed up, the for loop is inside an if statement. You are also comparing floats like this: previousMeasurement != -1 which is not working as you expect because of the float's limited precision. Normally you would use something like: sourceFloat - compareToFloat < equalThreshold.

In order to help you any further, you should in detail explain how the device is supposed to work because the code does not explain it.