what is wrong with my code

(( sorry for my bad English :confused: ))

i want help in my code

my project make arduino read load cell and then print the sensor read in lcd (0, 0)
and then print largest val of load cell read in lcd (0, 1) and not clear till i rest arduino
let us say i put 100g on sensor
the arduino will write 100g in (0,0) and (0,1) in lcd
and when i remove the 100g
arduino will write in (0.0) 0g and in (0.1) 100g

but when i use my code arduino donot do that
i want know why where the wrong here

                         //code made by elsayed ayman
                        //:3
                      //lcd setup
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(7,8,9);
                    //hx711 setup
#include <HX711_ADC.h>
HX711_ADC LoadCell(A0, A1);
long t;                                        
                 //code
void setup() 
{
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("plz w8");
lcd.setCursor(0,1);
lcd.print("loading");
LoadCell.begin();
long stabilisingtime = 2000; 
LoadCell.start(stabilisingtime);
LoadCell.setCalFactor(696.0); 
}
void loop()
{                           
LoadCell.update();  
if (millis() > t + 250) 
{
float i = LoadCell.getData();
float ii = i ;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("sensor=");
lcd.print(i);
lcd.print("g");
t = millis(); 
if ( ii > i);
{
  lcd.setCursor(0, 1);
  lcd.print("LastVal=");
  lcd.print(ii);
  lcd.print("g");
}
}    
if (LoadCell.getTareStatus() == true) {
lcd.setCursor(0, 1);
lcd.print("Tare complete");  
}
}

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

elsayed_ayman:
but when i use my code arduino donot do that

What does it do?

when i put the 100g
arduino
write 100g in (0.0) and 100g in (0.1)
and when i remove the 100g
arduino
write in(0.0) 0g and (0.1) 0g

   float i = LoadCell.getData();
    float ii = i ;

You are declaring new variables to hold data each time through loop(). That is OK but each time you do it they will both be set to an unknown value and will not retain the previous one. If you want a variable to retain its previous value then either declare it as a global or declare it static.

To make your code clearer can I suggest that you give them clearer names, perhaps highestValue and currentValue. When the program start set highestValue to zero and update it is currentValue is greater than highestValue

Your method of using millis() by addition has flaws in it if the program is to run for a long time. Timing variables should be unsigned long (and the names could be clearer) and the calculations should be done by subtraction. Doing this will ensure that there are no problems when millis() rolls over to zero.

See Using millis() for timing. A beginners guide

thx for info master UKHeliBob
the guide is so good and help me alot (( english not my main so take alot of time to read and understand it )) :confused:
any way i think i did what you say and that is my new code

                         //code made by elsayed ayman
                        //:3
                      //lcd setup
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(7,8,9);
                    //hx711 setup
#include <HX711_ADC.h>
HX711_ADC LoadCell(A0, A1);      
                  //time setup
unsigned long startMillis; 
unsigned long currentMillis;
const unsigned long period = 250;                                
                 //code
void setup()
{
startMillis = millis();
float highestValue = 0 ;
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("plz w8");
lcd.setCursor(0,1);
lcd.print("loading");
LoadCell.begin();
LoadCell.start(currentMillis - startMillis >= period);
LoadCell.setCalFactor(696.0);

}
void loop()
{                          
LoadCell.update();
currentMillis = millis();   
if (currentMillis - startMillis >= period)
{
float currentValue = LoadCell.getData();
float highestValue = currentValue ;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("sensor=");
lcd.print(currentValue);
lcd.print("g");
startMillis = currentMillis;
if ( highestValue > currentValue);
{
  lcd.setCursor(0, 1);
  lcd.print("LastVal=");
  lcd.print(highestValue);
  lcd.print("g");
}
}    
if (LoadCell.getTareStatus() == true) {
lcd.setCursor(0, 1);
lcd.print("Tare complete");  
}
}

but :o
still the same problem
arduino print the Weight i add it on sensor but when i remove it arduino clear the result form lcd in (0.1)
what is wrong here :confused:
what i want from code is print the weight which i add on sensor in lcd (0.0) and (0.1) and when i remove the weight arduino print 0g in lcd (0.0) and the result not clear for lcd (0.1) :confused:

    float currentValue = LoadCell.getData();
    float highestValue = currentValue ;

No !

Stop declaring new variables when you want to retain the values previously assigned to them and don't copy the current value to the highest value without testing whether it is higher than the previously stored value

The problem is here:

float currentValue = LoadCell.getData();
float highestValue = currentValue ;

highestValue always gets overridden with currentValue, no matter what the values are. You should try maybe this:

float currentValue = LoadCell.getData();
if (currentValue > highestValue)
  highestValue = currentValue;

Notice that you'll have to either declare highestValue as a global variable (like at the top of the program, outside any functions) or as a static variable. Otherwise, it gets a new copy every time the loop routine is executed, and the old data would get thrown away.

6 simple steps

Declare your value variables as global at the top of the program
Set the highest value to zero
When you use the variables in the program do not put the type specifier in front of the name
Read the current value and compare it with the highest value
If the current value is greater than the highest value assign it to the highest value
Display the highest and current values when a new current value becomes available

the simple 6 steps

i think i did some :slight_smile:

and some i donot know what you mean!! :confused:

(ok) Declare your value variables as global at the top of the program
(!!) Set the highest value to zero → should i write (( float highestValue =0 ; )) not (( float highestValue ; )) only !!
(ok) When you use the variables in the program do not put the type specifier in front of the name
(??)Read the current value and compare it with the highest value i try to do it but i fail
i try to read about how i can do it but still i cannot do it
i read from
-1-https://forum.arduino.cc/index.php?topic=147972.0
-2-https://forum.arduino.cc/index.php?topic=458229.0
-3-analogread - Getting the highest value from analog reading? - Arduino Stack Exchange
(!!)If the current value is greater than the highest value assign it to the highest value
you mean that

if(currentValue < highestValue);
{ 
lcd.setCursor(0, 1);
lcd.print("LastVal=");
lcd.print(highestValue);
lcd.print("g");
}

(??)Display the highest and current values when a new current value becomes available

my new code :o

                         //code made by elsayed ayman
                        //:3
                       //lcd setup
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(7,8,9);
                    //hx711 setup
#include <HX711_ADC.h>
HX711_ADC LoadCell(A0, A1);      
                  //time setup
unsigned long startMillis; 
unsigned long currentMillis;
const unsigned long period = 250;                               
                // global
float highestValue ; 
float currentValue ;                           
              //code
void setup()
{
startMillis = millis();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("plz w8");
lcd.setCursor(0,1);
lcd.print("loading");
LoadCell.begin();
LoadCell.start(currentMillis - startMillis >= period);
LoadCell.setCalFactor(696.0);
}
void loop()
{ 
currentMillis = millis();                         
LoadCell.update();   
if(currentMillis - startMillis >= period)
{
currentValue = LoadCell.getData();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("sensor=");
lcd.print(currentValue);
lcd.print("g");
startMillis = currentMillis;
if(currentValue < highestValue);
{ 
lcd.setCursor(0, 1);
lcd.print("LastVal=");
lcd.print(highestValue);
lcd.print("g");
}
}    
if(LoadCell.getTareStatus() == true) {
lcd.setCursor(0, 1);
lcd.print("Tare complete");  
}
}

the new code make arduino write sensor reading but { last val } -lcd-(0.1) = 0 and not change
so what wrong i did it here !! :confused:

pert:
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

(ok) Declare your value variables as global at the top of the program

(!!) Set the highest value to zero -> should i write (( float highestValue =0 ; )) not (( float highestValue ; )) only !!
Actually either will do because delaring global variables sets them to zero

(ok) When you use the variables in the program do not put the type specifier in front of the name

(??)Read the current value and compare it with the highest value i try to do it but i fail
(!!)If the current value is greater than the highest value assign it to the highest value

if (currentValue > highestValue)
  {
    highestValue = current value;  //saved new highest
  }

(??)Display the highest and current values when a new current value becomes available
Use you display code which you already have working

oh i think now i did every thing you say it in my code :slight_smile:

//code made by elsayed ayman
//:3
//lcd setup
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(7, 8, 9);
//hx711 setup
#include <HX711_ADC.h>
HX711_ADC LoadCell(A0, A1);
//time setup
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 250;
// global
float highestValue ;
float currentValue ;
//code
void setup()
{
  startMillis = millis();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("plz w8");
  lcd.setCursor(0, 1);
  lcd.print("loading");
  LoadCell.begin();
  LoadCell.start(currentMillis - startMillis >= period);
  LoadCell.setCalFactor(696.0);
}
void loop()
{
  currentMillis = millis();
  LoadCell.update();
  if (currentMillis - startMillis >= period)
  {
    currentValue = LoadCell.getData();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("sensor=");
    lcd.print(currentValue);
    lcd.print("g");
    startMillis = currentMillis;
    if (currentValue > highestValue);
    {
      highestValue = currentValue;
      lcd.setCursor(0, 1);
      lcd.print("LastVal=");
      lcd.print(highestValue);
      lcd.print("g");
    }
  }
  if (LoadCell.getTareStatus() == true) {
    lcd.setCursor(0, 1);
    lcd.print("Tare complete");
  }
}

but :confused:
still same problem :o when i put Weight on sensor and arduino read it give me the result in sensor lcd line (0.0) and last val lcd line (0.1) and when i remove the Weight arduino write in last val lcd line 0g not the last result befor i remove the Weight !! :confused:

Go to sleep. Wake up and look at it again. 99% of the time you will see the problem immediately.

i will add that video here so you can see my problem

and then i will go to sleep hope that help me

  LoadCell.start(currentMillis - startMillis >= period);Looks wrong

From one of the examples with the library

  long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
  LoadCell.start(stabilisingtime);

i Realize that too and i did like that i wan to know if that right or wrong ??
i write in global

const unsigned long period2 = 2000;

and in void setup()

LoadCell.start(currentMillis - startMillis >= period2);
currentMillis - startMillis >= period2;

Will either be false, ie 0, or true, ie 1

Will 0 or 1 millisecond satisfy

 //tare preciscion can be improved by adding a few seconds of stabilising time

?

O_o
oh oh
thanks for info

Note that this may not be the cause of your problem with displaying data but I think that you should put it right anyway.

hi hi
so :confused:
that my code

//code made by elsayed ayman
//:3
//lcd setup
#include <LiquidCrystal595.h>
LiquidCrystal595 lcd(7, 8, 9);
//hx711 setup
#include <HX711_ADC.h>
HX711_ADC LoadCell(A0, A1);
//time setup
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 250;
long stabilisingtime = 2000;
// global
float highestValue ;
float currentValue ;
//code
void setup()
{
  startMillis = millis();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("plz w8");
  lcd.setCursor(0, 1);
  lcd.print("loading");
  LoadCell.begin();
  LoadCell.start(stabilisingtime);
  LoadCell.setCalFactor(696.0);
}
void loop()
{
  currentMillis = millis();
  LoadCell.update();
  if (currentMillis - startMillis >= period)
  {
    currentValue = LoadCell.getData();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("sensor=");
    lcd.print(currentValue);
    lcd.print("g");
    startMillis = currentMillis;
  }
  if (currentValue > highestValue);
  {
    highestValue = currentValue;
    lcd.setCursor(0, 1);
    lcd.print("LastVal=");
    lcd.print(highestValue);
    lcd.print("g");
  }

  if (LoadCell.getTareStatus() == true) {
    lcd.setCursor(0, 1);
    lcd.print("Tare complete");
  }
}

Unfortunately even after i slept and read it for more than 6 h :o still donot know where i wrong here ?? so hope you can give me hint :confused: