how to store and compare decimal value from keypad and sensor value?

Respected all,
as of now i am having very little time to interface because of some urgent emergencies, I need help for my project regarding store data for compare with sensor value , and here i am using sensor as weightstone bridge for weighing machine using hx711 amplifier and also calibration done, so i want to check my sensor value with my input value which will come from my keypad and for display im using 16x2 lcd I2C connection with my arduino MEGA, here is my code: I have copied from the forum :How to use decimal places with keypad entry - Programming Questions - Arduino Forum

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <HX711.h>
LiquidCrystal lcd(2,3,4,5,6,7);

#define calibration_factor -9752.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  9
#define LOADCELL_SCK_PIN  8

HX711 scale;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 14, 15, 16, 17 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 18, 19, 20, 21 }; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0

  
}

void loop()
{
  float myValueFromKeypad1 = getKeypadFloat();
  float myValueFromKeypad2 = getKeypadFloat();
  Serial.println(" ");
  Serial.print("I received ");
  Serial.print(myValueFromKeypad1, 4);
  Serial.print(", and then I received ");
  Serial.println(myValueFromKeypad2, 4);
  float S_val = myValueFromKeypad1;
  lcd.setCursor(0, 0);
  lcd.print("S_val = "); //keypad value
  lcd.print(S_val,4);

  char M_val = (scale.get_units(), 4);
  lcd.setCursor(0, 1);
  lcd.print("M_Val = ");
  lcd.print(scale.get_units(), 4);
  Serial.print(" unit"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();  

  



}//loop

float getKeypadFloat()
{
  int debug = 1;
  float beforeDecimal = 0;                                // the number accumulator
  float afterDecimal = 0;
  byte howManyDecimals = 0;
  float finalValue;
  int keyval;                                     // the key press
  int isNum;
  int isStar;
  int isHash;


  Serial.println("Enter digits, * as decimal point, then decimals, # to end");
  if (debug)Serial.print("So far: ");

  // accumulate the part before the decimal
  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');         // is it a digit?
    if (isNum)
    {
      if (debug)Serial.print(keyval - '0');
      beforeDecimal = beforeDecimal * 10 + keyval - '0';               // accumulate the input number
    }

    isStar = (keyval == '*');
    if (debug)if (isStar)  Serial.print(".");

    //display but ignore illegal presses
   /* if (debug)
    {
      if (keyval == 'A' || keyval == 'B' || keyval == 'C' || keyval == 'D' || keyval == '#')
        Serial.print("?");
    }
*/
  } while (!isStar || !keyval);                          // until a * or while no key pressed

  // accumulate the part after the decimal

  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');         // is it a digit?
    if (isNum)
    {
      if (debug)Serial.print(keyval - '0');
      afterDecimal = afterDecimal * 10 + keyval - '0';                 // accumulate the input number
      howManyDecimals++; // increment for later use
    }

    isHash = (keyval == '#');

    //display but ignore illegal presses
    if (debug)
    {
      if (keyval == 'A' || keyval == 'B' || keyval == 'C' || keyval == 'D' || keyval == '*')
        Serial.print("?");
    }

  } while (!isHash || !keyval);                          // until # or while no key pressed

  finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));

  if (debug)Serial.println(" ");
  if (debug)Serial.print("Returning ");
  if (debug)Serial.println(finalValue, howManyDecimals);

  return finalValue;

}//getKeypadFloat

Here is my code but i need to compare S_val with M_val , here S_val will come from keypad entry and M_val will come from weighing bridge load cell sensor.
so when S_val will be manually typed from keypad , it will then compare with value of Measured value from sensor if the measured value from sensor is lower then S_val then the LED_pin 13 will go low or else it will go high but measured value will be comes around the S_val like plus minus 0.2 decimals then it is acceptable how to solve this kindly guide me as i am having less time to interface.
Thank you so much.

not clear to me what you asking to help with. i see code for reading values and displaying them. i don't see a comparison or any action based on it.

code to read a value from the keypad may easier if broken into parts. read an entire string. translate ascii digits to decimal and add to a sum after multiplying the sum by 10. keep track of the position of the decimal. multiply a decimal factor by 10 for each character read after the decimal. return a float by dividing the sum by the decimal factor. return an error if anything other than a digit or decimal is entered (see isdigit())

consider using sprintf() and having separate routines for updating the lcd display rather than having multiple lcd routine calls embedded with the rest of the code

Because of precision and rounding errors, comparing two floating point numbers is not a simple as using == .

Instead, you need find the absolute difference between the numbers and compare it with a very small number and if the difference is less than this number.

Google search on "compare two floating point numbers C++"

cattledog:
Because of precision and rounding errors, comparing two floating point numbers is not a simple as using == .

if (ABS(f1 - f2) < 0.1)

thank you sir,

if (float ABS = (f1 - f2) < 0.1)

Here I have attached modified code for the project:

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <HX711.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define calibration_factor -9752.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  9
#define LOADCELL_SCK_PIN  8

HX711 scale;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 14, 15, 16, 17 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 18, 19, 20, 21 }; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0


}

int counter = 0;
float W_val;

void loop()
{
  lcd.setCursor(0, 1);
  lcd.print("M_val:");
  lcd.print(scale.get_units(), 4);

  int keyval_1 = keypad.getKey();
  if (char Reset_Key = (keyval_1 == '#'))
  {

    Serial.print("reset");
    lcd.clear();
    counter = 1;
    lcd.setCursor(0, 0);
    lcd.print("Set Value:");
    lcd.setCursor(0, 1);
    lcd.print("M_val:");
    lcd.print(scale.get_units(), 4);
  }
  else if (counter == 1) {

    float myValueFromKeypad1 = getKeypadFloat();
    // float myValueFromKeypad2 = getKeypadFloat();
    Serial.println(" ");
    Serial.print("I received ");
    Serial.print(myValueFromKeypad1, 4);
    //Serial.print(", and then I received ");
    //Serial.println(myValueFromKeypad2, 4);
    float S_val = myValueFromKeypad1;
    lcd.setCursor(0, 0);
    lcd.print("S_val= "); //keypad value
    lcd.print(S_val, 4);

    char M_val = (scale.get_units(), 4);
    lcd.setCursor(0, 1);
    lcd.print("M_Val = ");
    lcd.print(M_val, 4);
    Serial.println();
    Serial.print("M_Val = ");
    Serial.print(scale.get_units(), 4);
    Serial.println(" unit"); //You can change this to kg but you'll need to refactor the calibration_factor
    Serial.println();
    // lcd.clear();

    W_val = atof (M_val);
    Serial.print("value: " );
    Serial.println(W_val, 4);


    counter = 0;

    if (float ABS = (S_val - W_val) < 0.1) //(float ABS(f1 - f2) < 0.1)
    {
      digitalWrite(13, HIGH);
    }
    else {
      digitalWrite(13, LOW);
    }

  }

}//loop


float getKeypadFloat()
{

  float beforeDecimal = 0;                                // the number accumulator
  float afterDecimal = 0;
  byte howManyDecimals = 0;
  float finalValue;
  int keyval;                                     // the key press
  int isNum;
  int isStar;
  int isHash;

  Serial.println("Set Value:");

  Serial.print("S_val: ") ;


  // accumulate the part before the decimal
  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');         // is it a digit?
    if (isNum)
    {
      lcd.setCursor(10, 0); lcd.print(keyval - '0'); //Serial.print(keyval - '0');
      beforeDecimal = beforeDecimal * 10 + keyval - '0';               // accumulate the input number
    }

    isStar = (keyval == '*');
    if (isStar) lcd.print("."); //Serial.print(".");


  } while (!isStar || !keyval);                          // until a * or while no key pressed

  // accumulate the part after the decimal

  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');         // is it a digit?
    if (isNum)
    {
      lcd.print(keyval - '0'); //Serial.print(keyval - '0');
      afterDecimal = afterDecimal * 10 + keyval - '0';                 // accumulate the input number
      howManyDecimals++; // increment for later use
    }




    isHash = (keyval == 'D');  //hit enter or push that number


  } while (!isHash || !keyval);                          // until D or while no key pressed

  finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));
  Serial.println(" ");
  Serial.print("Returning ");
  Serial.println(finalValue, howManyDecimals);
  lcd.clear();
  counter = 0;


  return finalValue;

}//getKeypadFloat

Here i have used compare function for turning on and off LED still in working progress, my other problem is i want to take reading from sensor which will be live display on lcd set cursor(0,1)
In all of the functions and loop how can i implement it sir, kindly guide me also using watchdog timer i will have to check if the actual weight is kept on the sensor or not then only it will trigger of showing live data on lcd. guide me to implement it sir.

The only function the watchdog timer has is to reset the processor if you don't kick it often enough.

Are you sure that's what you want?

TheMemberFormerlyKnownAsAWOL:
The only function the watchdog timer has is to reset the processor if you don't kick it often enough.

Are you sure that's what you want?

I guess the delay function should work fine in that manner no need for watchdog timer i guess will thinking of it and will see if its worth it or not, thank you sir.

I am unable to compare measured value and set value its giving me 10 as value in output on serial monitor, kindly guide me,
Here is my code:

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <HX711.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define calibration_factor 2657.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  9
#define LOADCELL_SCK_PIN  8

HX711 scale;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 14, 15, 16, 17 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 18, 19, 20, 21 }; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0


}

int counter = 0;
float W_val;

void loop()
{

  lcd.setCursor(0, 1);
  lcd.print("M_val:");
  lcd.print(scale.get_units(), 4);


  int keyval_1 = keypad.getKey();
  if (char Reset_Key = (keyval_1 == '#'))
  {

    Serial.print("reset");
    lcd.clear();
    counter = 1;
    lcd.setCursor(0, 0);
    lcd.print("Set Value:");
    lcd.setCursor(0, 1);
    lcd.print("M_val:");
    lcd.print(scale.get_units(), 4);
  }
  else if (counter == 1) {

    float myValueFromKeypad1 = getKeypadFloat();
    // float myValueFromKeypad2 = getKeypadFloat();
    Serial.println(" ");
    Serial.print("I received ");
    Serial.print(myValueFromKeypad1, 4);
    //Serial.print(", and then I received ");
    //Serial.println(myValueFromKeypad2, 4);
    float S_val = myValueFromKeypad1;
    lcd.setCursor(0, 0);
    lcd.print("S_val= "); //keypad value
    lcd.print(S_val, 4);

    char M_val = (scale.get_units(), 4);
    lcd.setCursor(0, 1);
    lcd.print("M_Val = ");
    lcd.print(M_val, 4);
    Serial.println();
    Serial.print("M_Val = ");
    Serial.print(M_val, 4);
    Serial.println(" unit"); //You can change this to kg but you'll need to refactor the calibration_factor
    Serial.println();
    // lcd.clear();


    //float W_val = myString.toFloat(M_val);
    Serial.print("value_M: " );
    Serial.println(M_val, 4);


    counter = 0;

    if (float ABS = (S_val - M_val) < 0.1) //(float ABS(f1 - f2) < 0.1)
    {
      digitalWrite(13, HIGH);
      Serial.print("ON!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
    else {
      digitalWrite(13, LOW);
      Serial.print("OFF!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
  }


}//loop

float getKeypadFloat()
{

  float beforeDecimal = 0;                                // the number accumulator
  float afterDecimal = 0;
  byte howManyDecimals = 0;
  float finalValue;
  int keyval;                                     // the key press
  int isNum;
  int isStar;
  int isHash;

  Serial.println("Set Value:");

  Serial.print("S_val: ") ;


  // accumulate the part before the decimal
  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');          // is it a digit?
    if (isNum)
    {
      lcd.setCursor(10, 0); lcd.print(keyval - '0');                   //Serial.print(keyval - '0');
      beforeDecimal = beforeDecimal * 10 + keyval - '0';               // accumulate the input number
    }

    isStar = (keyval == '*');
    if (isStar) lcd.print("."); //Serial.print(".");


  } while (!isStar || !keyval);                          // until a * or while no key pressed

  // accumulate the part after the decimal

  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');          // is it a digit?
    if (isNum)
    {
      lcd.print(keyval - '0'); //Serial.print(keyval - '0');
      afterDecimal = afterDecimal * 10 + keyval - '0';                 // accumulate the input number
      howManyDecimals++; // increment for later use
    }

    isHash = (keyval == 'D');  //hit enter or push that number


  } while (!isHash || !keyval);                          // until D or while no key pressed

  finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));
  Serial.println(" ");
  Serial.print("Returning ");
  Serial.println(finalValue, howManyDecimals);
  lcd.clear();
  counter = 0;


  return finalValue;

}

kindly suggest for

void loop()
{

  lcd.setCursor(0, 1);
  lcd.print("M_val:");
  lcd.print(scale.get_units(), 4);


  int keyval_1 = keypad.getKey();
  if (char Reset_Key = (keyval_1 == '#'))
  {

    Serial.print("reset");
    lcd.clear();
    counter = 1;
    lcd.setCursor(0, 0);
    lcd.print("Set Value:");
    lcd.setCursor(0, 1);
    lcd.print("M_val:");
    lcd.print(scale.get_units(), 4);
  }
  else if (counter == 1) {

    float myValueFromKeypad1 = getKeypadFloat();
    // float myValueFromKeypad2 = getKeypadFloat();
    Serial.println(" ");
    Serial.print("I received ");
    Serial.print(myValueFromKeypad1, 4);
    //Serial.print(", and then I received ");
    //Serial.println(myValueFromKeypad2, 4);
    float S_val = myValueFromKeypad1;
    lcd.setCursor(0, 0);
    lcd.print("S_val= "); //keypad value
    lcd.print(S_val, 4);

    char M_val = (scale.get_units(), 4);
    lcd.setCursor(0, 1);
    lcd.print("M_Val = ");
    lcd.print(M_val, 4);
    Serial.println();
    Serial.print("M_Val = ");
    Serial.print(M_val, 4);
    Serial.println(" unit"); //You can change this to kg but you'll need to refactor the calibration_factor
    Serial.println();
    // lcd.clear();


    //float W_val = myString.toFloat(M_val);
    Serial.print("value_M: " );
    Serial.println(M_val, 4);


    counter = 0;

    if (float ABS = (S_val - M_val) < 0.1) //(float ABS(f1 - f2) < 0.1)
    {
      digitalWrite(13, HIGH);
      Serial.print("ON!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
    else {
      digitalWrite(13, LOW);
      Serial.print("OFF!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
  }


}

this loop :
output:

15:05:54.005 -> resetSet Value:
15:05:54.174 -> S_val:  
15:06:08.251 -> Returning 0.2511
15:06:08.251 ->  
15:06:08.251 -> I received 0.2511
15:06:08.284 -> M_Val = 10 unit
15:06:08.284 -> 
15:06:08.284 -> value_M: 0.0000
15:06:08.318 -> ON!value: 1.0000

it showing me .0000 not exact value up to 4 decimal places, kindly guide me.

it may be easier to read a complete ascii string and then use atof() to convert it to a float.

char M_val = (scale.get_units(), 4); What's that ",4" doing there, please?

char M_val = (scale.get_units(), 4);

As well as answering AWOL's question you can answer this one as well
Why have you assigned it to a char?

scale.get_units() returns a float.

09:24:41.655 -> resetSet Value:
09:24:41.822 -> S_val:  
09:24:51.668 -> Returning 1.9521
09:24:51.701 ->  
09:24:51.701 -> I received 1.9521
09:24:51.735 -> M_Val = 4.0000 unit
09:24:51.735 -> 
09:24:51.735 -> value_M: 4.0000
09:24:51.769 -> ON!value: 1.0000

my value for measured
weight is around 0.1567 and it showing me 4.0000 unit, Here i have input number from keypad which is 1.9521 and also as mentioned i have turned it to float my mistake sir to make it as char.

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <HX711.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define calibration_factor 2657.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define LOADCELL_DOUT_PIN  9
#define LOADCELL_SCK_PIN  8

HX711 scale;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 14, 15, 16, 17 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 18, 19, 20, 21 }; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0


}

int counter = 0;
float W_val;

void loop()
{

  lcd.setCursor(0, 1);
  lcd.print("M_val:");
  lcd.print(scale.get_units(), 4);


  int keyval_1 = keypad.getKey();
  if (char Reset_Key = (keyval_1 == '#'))
  {

    Serial.print("reset");
    lcd.clear();
    counter = 1;
    lcd.setCursor(0, 0);
    lcd.print("Set Value:");
    lcd.setCursor(0, 1);
    lcd.print("M_val:");
    lcd.print(scale.get_units(), 4);
  }
  else if (counter == 1) {

    float myValueFromKeypad1 = getKeypadFloat();
    // float myValueFromKeypad2 = getKeypadFloat();
    Serial.println(" ");
    Serial.print("I received ");
    Serial.print(myValueFromKeypad1, 4);
    //Serial.print(", and then I received ");
    //Serial.println(myValueFromKeypad2, 4);
    float S_val = myValueFromKeypad1;
    lcd.setCursor(0, 0);
    lcd.print("S_val= "); //keypad value
    lcd.print(S_val, 4);

    float M_val = (scale.get_units(), 4);
    lcd.setCursor(0, 1);
    lcd.print("M_Val = ");
    lcd.print(M_val, 4);
    Serial.println();
    Serial.print("M_Val = ");
    Serial.print(M_val, 4);
    Serial.println(" unit"); //You can change this to kg but you'll need to refactor the calibration_factor
    Serial.println();
    // lcd.clear();


    //float W_val = myString.toFloat(M_val);
    Serial.print("value_M: " );
    Serial.println(M_val, 4);


    counter = 0;

    if (float ABS = (S_val - M_val) < 0.1) //(float ABS(f1 - f2) < 0.1)
    {
      digitalWrite(13, HIGH);
      Serial.print("ON!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
    else {
      digitalWrite(13, LOW);
      Serial.print("OFF!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
  }


}//loop

float getKeypadFloat()
{

  float beforeDecimal = 0;                                // the number accumulator
  float afterDecimal = 0;
  byte howManyDecimals = 0;
  float finalValue;
  int keyval;                                     // the key press
  int isNum;
  int isStar;
  int isHash;

  Serial.println("Set Value:");

  Serial.print("S_val: ") ;


  // accumulate the part before the decimal
  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');          // is it a digit?
    if (isNum)
    {
      lcd.setCursor(10, 0); lcd.print(keyval - '0');                   //Serial.print(keyval - '0');
      beforeDecimal = beforeDecimal * 10 + keyval - '0';               // accumulate the input number
    }

    isStar = (keyval == '*');
    if (isStar) lcd.print("."); //Serial.print(".");


  } while (!isStar || !keyval);                          // until a * or while no key pressed

  // accumulate the part after the decimal

  do
  {
    keyval = keypad.getKey();                          // input the key
    isNum = (keyval >= '0' && keyval <= '9');          // is it a digit?
    if (isNum)
    {
      lcd.print(keyval - '0'); //Serial.print(keyval - '0');
      afterDecimal = afterDecimal * 10 + keyval - '0';                 // accumulate the input number
      howManyDecimals++; // increment for later use
    }

    isHash = (keyval == 'D');  //hit enter or push that number


  } while (!isHash || !keyval);                          // until D or while no key pressed

  finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));
  Serial.println(" ");
  Serial.print("Returning ");
  Serial.println(finalValue, howManyDecimals);
  lcd.clear();
  counter = 0;


  return finalValue;

}

thank you sir help me to view weight when i put something on it that i have to consider as if statement to be include? or is there any other way?

 float M_val = (scale.get_units(), 4);

You do not have this correct.

From the library, this is the function prototype for scale.get_units()

// returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
// times = how many readings to do
		
float get_units(byte times = 1);

If you want to take 4 readings, then use

scale.get_units(4);

For a single reading

scale.get_units()

You have run the comma operator.

float M_val = (scale.get_units(), 4);

The expression you used reads the scale, throws away the result and then uses the value 4.

That is why you see

M_Val = 4.0000 unit

ok sir what i have thought that i have to consider upto 4 decimal point so the actual unit would look like "1.6789" it should show upto 4 decimal point so how can i do that as we can check on serial monitor that Serial.print(value, 4) so that it will show 4 decimal point. so how can i view upto 4 decimal point via this library sir (scale.get_units()) here i want proper reading upto 4 decimal point so if i send this data to variable as i have chose M_val so M_val should be having float number like 3.7645 or 2.6790 like that so how can i achieve that sir.
thank you sir.

float M_val = scale.get_units();
Serial.print(M_val,4);

You confusing the float value returned from the function with the display of the float value with 4 places to the right of the decimal point.

The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.

In your case, the float returned by the scale reading maybe valid to 1.234567 but you only want to display 1.2345.

yes sir i got the point but i require till total would be "12.3456" only 6 digits and upto 4 decimal point only.
How can i achieve that sir. what will be the way sir. kindly guide me

I'm sorry, but I do not understand the issue you are having.

The scale function will return weight with 6 or 7 digits which are meaningful.

You can display that number however you like, 10 digits if you want, but the last 3 or 4 will not be meaningful. You can display 3 digits which will all be meaningful.

You can compare it to another number looking for a small difference as previously explained.

sir, thank you i got it,
here is my output
its working sir

11:01:35.960 -> resetSet Value:
11:01:36.163 -> S_val:  
11:01:45.092 -> Returning 0.2541
11:01:45.125 ->  
11:01:45.125 -> I received 0.2541
11:01:45.158 -> M_Val = -0.0260 unit
11:01:45.158 -> 
11:01:45.158 -> value_M: -0.0260
11:01:45.192 -> OFF!value: 0.0000
11:02:27.690 -> resetSet Value:
11:02:27.862 -> S_val:  
11:02:34.091 -> Returning 1.5241
11:02:34.091 ->  
11:02:34.125 -> I received 1.5241
11:02:34.125 -> M_Val = -0.6790 unit
11:02:34.158 -> 
11:02:34.158 -> value_M: -0.6790
11:02:34.191 -> OFF!value: 0.0000

now i have to check the ABS which is difference from set value and measured value is not coming properly byte by byte. how can i achieve that sir?

if (float ABS = (S_val - M_val) < 0.1) //(float ABS(f1 - f2) < 0.1)
    {
      digitalWrite(13, HIGH);
      Serial.print("ON!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
    else {
      digitalWrite(13, LOW);
      Serial.print("OFF!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }

i also want to know the exact value in ABS variable that has been stored in it. kindly help me

The function is abs()

float ABS = abs(S_val - M_val);
if (ABS < 0.1)
    {
      digitalWrite(13, HIGH);
      Serial.print("ON!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }
    else {
      digitalWrite(13, LOW);
      Serial.print("OFF!");
      Serial.print("value: " );
      Serial.println(ABS, 4);
    }

thank you sir !!