tatobari / hx711py

HX711 Python Library for Raspberry Pi.
Apache License 2.0
211 stars 153 forks source link

Getting the first weight value aside from tare value #33

Closed kumucho closed 7 months ago

kumucho commented 4 years ago

Hi sir tatobari,

Do you have some way to get the first initial mass from the code? like.. after the Tare where the value is 0, then you suddenly lumped in a 3kg you get to record and save the 3kg value sensed by the loadcell to be used in equation like these:

MC = (1-(initial mass - present mass)/initial mass) * 100

Ive tried this code and got myself a 1% because of lack of initial mass value: image

NeonSpork commented 4 years ago

So if I understand correctly, you're trying to first record a weight, then record a new weight, followed by calculating the % difference in weight. Correct?

Edit: Your code is different from your example, I think. In your example initial mass would be val, but in your code val2 is in the place of initial mass in your example. Do you mean (present mass - initial mass)/present mass? Also, if the value that is to the right of / becomes zero (as in the case of a Tare operation, or if you remove the weight and the scale goes to zero) your program will crash due to a divide-by-zero error unless you handle that error somehow, just FYI! :)

If I've understood your intent, then your method is likely not working because both val and val2 are in the same while True loop, and they value is being set essentially simultaneously. So if val = val2 your equation would be like the following: MC = (1 - 0) * 100 = 100 However in the code you took a screen shot of I can see that your equation is different and you don't have brackets around the 1 - ((val2-val)/val2) 100. This means that your ((val2-val)/val2) value is multiplied by 100 before it is subtracted from 1, aka 0 100 = 0. Which will always give you MC = 1 in your case, hence your reported value of 1%.

First of all you'd need the proper brackets, but if you want to compare values before and after I think you're going to have to modify the code to store a value outside the while True loop (which is run as fast as it can by the python script as long as it's running), and use that in stead of val. Or move val out of the while True loop somehow.

Does that make sense? Hope I understood what you want to achieve correctly. Good luck!

kumucho commented 4 years ago

Thankyou for your reply sir @NeonSpork. Yes, I am trying to record the first weight then use the present weight to achieve the percent ratio. Therefore, the formula will be (initial mass - present mass)/initial mass.

Ohh, right I really didnt put a bracket on the formula. Thats why I am getting a 1% output.

And one more question, What condition should I use if I'll set the val aside? I am thinking of using try but it bothers my mind that it may only get the tare weight because of it having to get or work on a moment of time.

Can I use "try" to get the value of weight ive put after getting the tare within .....lessay 5secs?

Once again, Thankyou for your help. You are a lifesaver

NeonSpork commented 4 years ago

Well I'm not sure what use case you have for this. Is this something that needs to be on a loop and run automatically? Or is this perhaps something you can run every time you want to check the change? Your use case will probably determine the best way to implement what you want to be honest.

Edit: If the first weight only ever needs to be recorded once and you just want the percentage difference on the loop, then it would be pretty simple to implement the first initial value since you can just place val before the while True loop.