2 similar functions, 2 diffrent rezults

Hi all, I have little question. In my code two similar functions I have, but result of this functions differ.

First function used as button touchpress event on nextion display, it tare scale weight to 0 (HX711 library).

void taraPushCallback(void *ptr)
{
  scale.read();
  scale.read_average(50);
  scale.get_value(50);
  scale.get_units(50);
  scale.set_scale(strojenie_temp);    
  scale.tare();

}

Second function not give me tare to 0 but I get result around 0.012 or more.

void tarowanie(){
  scale.read();
  scale.read_average(50);
  scale.get_value(50);
  scale.get_units(50);
  scale.set_scale(strojenie_temp);    
  scale.tare();
}

First function works great, second is not precise. I would use only one (first) function in code but I don't know how to call properly this function in loop. So I created second function and call it by

tarowanie();

but result isn't satisfied. What I'm doing wrong or how to call first function not by nextion event, just by myself?

Thx if somebody would read this and try to help me :wink:

can you post the full code ??

full code its over 600 lines so... I try short them and cut unnecessary things

#include <Nextion.h>
#include <EEPROM.h>
#include "HX711.h"
             
HX711 scale(A0, A1);

byte currentPage = 0;
float strojenie_temp = EEPROM.get(8, strojenie_temp);

NexButton tara = NexButton(1, 3, "tara");

NexTouch *nex_listen_list[] = {
  &tara,        
  NULL  // String terminated
  };

void taraPushCallback(void *ptr)
{
  scale.read();
  scale.read_average(20);
  scale.get_value(80);
  scale.get_units(20);
  scale.set_scale(strojenie_temp);    
  scale.tare();

}
void tarowanie(void){
  scale.read();
  scale.read_average(20);
  scale.get_value(80);
  scale.get_units(20);
  scale.set_scale(strojenie_temp);    
  scale.tare();
}

void setup() {
  Serial.begin(115200);
  tara.attachPush(taraPushCallback);
  tarowanie();
  }

void loop() {
  nexLoop(nex_listen_list);  // Check for any touch event
  if (currentPage == 1) {                       
      tarowanie();                   
      }
   }

Nope....

 float strojenie_temp = EEPROM.get(8, strojenie_temp);

See the EEPOM get reference.

I don't see any output in your cut down sketch. Can you add some and provide the results?

Your code doesn't use the ptr passed to taraPushCallback. If that's true in the full version too, you can just call it with NULL:

taraPushCallback(NULL);

Trayed use NULL, it works :wink: but result the same, mean I get minus 0.012-0.018 kg at empty weight.
Also tryed something like this:

void tarowanie(void){
  scale.read();
  scale.read_average(8);
  scale.get_value(150);
  scale.get_units(8);
  scale.set_scale(strojenie_temp);    
  scale.tare();
  
  
  for(int i=0; i<500; i++){
      scale.read();
      if(scale.get_units()<0){
     scale.set_offset(scale.read_average(2)+10);
      
  }
 }
}

but results are not perfect, I get around -0.005kg at empty weight should be 0.000kg

Could it be that you are calling your routine too frequently? I see that there is an is_ready function in the library - perhaps it needs time to settle.

Also, why are you calling set_scale every time? I would have expected that to occur once.

And you fixed the EEPROM.get call?

pcbbc:
Nope....

 float strojenie_temp = EEPROM.get(8, strojenie_temp);

@pcbbc, are you sure ?

This is the source: ArduinoCore-avr/libraries/EEPROM/src/EEPROM.h at master · arduino/ArduinoCore-avr · GitHub.

Here is a sketch for you to try:

#include <EEPROM.h>

void setup()
{
  float a, b;
  
  Serial.begin( 9600);
  Serial.println();
  Serial.println( "---- Test EEPROM.get ----");

  // The EEPROM.put() can be commented out, when the EEPROM is written.
  a = 123.45;
  EEPROM.put( 8, a);   // store 4 bytes at locations 8,9,10,11

  a = 0.0;
  EEPROM.get( 8, a);
  Serial.print( "get by reference parameter: ");
  Serial.println( a);

  a = 0.0;
  b = 0.0;
  a = EEPROM.get( 8, b);
  Serial.print( "get return value: ");
  Serial.print( a);
  Serial.print( ", reference parameter: ");
  Serial.println( b);

  a = 0.0;
  a = EEPROM.get( 8, a);
  Serial.print( "get by reference and return value are the same variable: ");
  Serial.println( a);
}

void loop(){}

@bartek87, on this forum some say: "the problem is in the part that you are not showing". There is also a website for that: https://snippets-r-us.com/. Even when there is one line missing, then I keep wondering what would be in that line and how it might affect the sketch :wink: