Hi, I am having trouble getting my head around this issue.
We have a water tank 200CM high
I have a sonar distance detector in the top that returns CM from the water surface.
I can get the remaining water OK, but I'd like to know the level shift up (rain) or down (usage) and the daily total that gets cleared once a day. Here's where I am so far and the Delta and Daily are not making sense. I'll take care of error handling etc when I get it doing what I need with the levels.
Could someone please rearrange this to do what I need.
If your code fails to acquire a good sonar reading 5 times in a row, 'pingDistanceNow' will still be 0, yet you assign that value to 'pingDistanceLast' so the next time through the function you will get wild results if you get a good reading.
blh64:
If your code fails to acquire a good sonar reading 5 times in a row, 'pingDistanceNow' will still be 0, yet you assign that value to 'pingDistanceLast' so the next time through the function you will get wild results if you get a good reading.
Thanks, but as I said I will do the error handling once it works OK. It very rarely returns a zero. Probably one scan in 50 so it pretty much it always falls through the to the levels.
The first time you call checkLevel(), pingDistanceLast is 0, aka tank is full. If that is not the case, you need to set pingDistanceLast to actual tank level before using it.
For example, init pingDistanceLast to some invalid value, and use the first checkLevel() call to set it to the actual tank level, or call sonar.ping_cm() to set pingDistanceLast before calling checkLevel for the first time.
Did you ever initialize pingDistanceLast to the current distance in setup? If not, you are starting with 0 and will throw off the initial reading.
I would also save the last water level from the previous day, and calculate the daily change from that, instead of using the cumulative delta change throughout the day, that could lead to cumulative errors.
@David, thanks, no I figured we are OK with that first zero start and erroneous levels, as the levels do not shift much over a 24-hour period and figured it would stabilize after the first few run-throughs.
Am I correct in thinking that the Delta should be a minus value. As the water level drops we need to know how much was used as opposed to gain when it rains.
OK, problem solved. None of the above were even close. I made up a test rig to allow me to accurately move the sensor back and forth on the desk relative to a piece of steel plate for good reflection. By trial and error I have now sorted it out.