OpenSprinkler / OpenSprinkler-Firmware

OpenSprinkler Unified Firmware for OpenSprinkler, OpenSprinkler Pi, and OpenSprinkler Beagle.
http://www.opensprinkler.com
GNU General Public License v3.0
472 stars 283 forks source link

Flow Sensor not resetting for stations without flow meter #248

Open bb09961 opened 1 year ago

bb09961 commented 1 year ago

Only a few of my stations are plumbed in to my flow meter. When a station without the flow meter runs, the previous station's flow rate is stored in the logs. It should store a 0. The attached screenshot shows the entries in red that should be 0. The values in green are correct. This issue shows up in my case, but also would be present if a station did not water at all due to a valve not opening.

Flow_Rate_Not_Resetting

Based on the comments in flow_poll() function, this IF statement jumps out of the function if the sensor never drops from HIGH to LOW, OR it would also jump out if the sensor never changes at all. This is the case for my stations without a flow meter.

void flow_poll() {  
...  
    if(!(prev_flow_state==HIGH && curr_flow_state==LOW)) { // only record on falling edge  
        prev_flow_state = curr_flow_state;  
        return;  
    }

If the sensor never changes, then it will never get to execute any of the following initialization code inside flow_poll() :

    /* RAH implementation of flow sensor */
    if (flow_start==0) { flow_gallons=0; flow_start=curr;} // if first pulse, record time
    if ((curr-flow_start)<90000) { flow_gallons=0; } // wait 90 seconds before recording flow_begin
    else {  if (flow_gallons==1)    {  flow_begin = curr;}}
    flow_stop = curr; // get time in ms for stop
    flow_gallons++;  // increment gallon count for each poll
    /* End of RAH implementation of flow sensor */

Would it work to move or copy the "flow_gallons=0" from flow_poll() to the turn_on_station() function where the flow_start variable is also reset?

void turn_on_station(byte sid, ulong duration) {
    // RAH implementation of flow sensor
    flow_start=0;
    //Move or copy flow_gallons reset here?//
    flow_gallons=0

If flow_gallons=0 is moved, then the existing code in the turn_off_station() function should work properly to log a 0 for flow_last_gpm:

    // RAH implementation of flow sensor
    if (flow_gallons > 1) {
        if(flow_stop <= flow_begin) flow_last_gpm = 0;
        else flow_last_gpm = (float) 60000 / (float)((flow_stop-flow_begin) / (flow_gallons - 1));
    }// RAH calculate GPM, 1 pulse per gallon
    else {flow_last_gpm = 0;}  // RAH if not one gallon (two pulses) measured then record 0 gpm
bb09961 commented 1 year ago

After testing, I've submitted Pull Request #251 with the suggested change from above.