A New way to use a flow sensor

I have looked all over for code or ideas on how to do this but I am coming up empty handed. So as far as I know this is a new way of using a flow sensor.

I am using a sea liquid flow sensor (model YF-S201) to measure the amount of liquid dispensed out of a backpack chemical sprayer. I need it to measure the amount of liquid that passes each time the trigger is pulled and I also want the overall total. I am using a button under the trigger to flag the beginning and the end of the spray event. I have other sensors that run as well but they record in the else if (). It is working but the event total (each time the trigger is pulled) and they are always event behind. So I get zeros the first time I release the button and from then on I get the last events totals. Does anyone know why that is and how I can fix it. I hope this was intelligible and didn’t seem like I was just rambling. This is the code if have right now. I'm not attached to any one way of doing this if you have a better way let me know. I'm fairly new at this (4 months in) and I don't have any previous coding experience. I do pick things up pretty quick though. Thank you for your help in advance.

// The hall-effect flow sensor in flowmeter outputs approximately 7.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 7.5;

volatile byte pulseCount;

int buttonPin = 3;
int ledPin = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte flowmeterPin  = 2;


float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long eventTotalMilliLitres;

boolean isSpraying = false;

unsigned long oldTime;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup()
{
  // Initialize a serial connection for reporting values to the host
  Serial.begin(38400);

  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input:
  pinMode(ledPin, OUTPUT);    // initialize the led pin as output


  pinMode(flowmeterPin, INPUT);
  digitalWrite(flowmeterPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres = 0;
  eventTotalMilliLitres  = 0;
  oldTime           = 0;


  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
}

void loop(){
  //Method for Flow Meter
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && isSpraying == false) {

    isSpraying = true;

    digitalWrite(ledPin, HIGH);

    if ((millis() - oldTime) > 1000) {

      detachInterrupt(sensorInterrupt);   // Disable the interrupt while calculating flow rate and sending the value to
      // the host


      flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;   // Because this loop may not complete in exactly 1 second intervals we calculate
      // the number of milliseconds that have passed since the last execution and use
      // that to scale the output. We also apply the calibrationFactor to scale the output
      // based on the number of pulses per second per units of measure (litres/minute in
      // this case) coming from the sensor.


      oldTime = millis(); // Note the time this processing pass was executed. Note that because we've
      // disabled interrupts the millis() function won't actually be incrementing right
      // at this point, but it will still return the value it was set to just before
      // interrupts went away.


      flowMilliLitres = (flowRate / 60) * 1000;  // Divide the flow rate in litres/minute by 60 to determine how many litres have
      // passed through the sensor in this 1 second interval, then multiply by 1000 to
      // convert to millilitres.





      attachInterrupt(sensorInterrupt, pulseCounter, FALLING);  // Enable the interrupt again now that we've finished sending output



      eventTotalMilliLitres += flowMilliLitres;  // Add the millilitres passed in this second to the cumulative total for single spray event

      flowMilliLitres = 0;                       // Reset the per second millilitres meassured so we can start measuring millilitres flowing in the next second

      pulseCount = 0;                            // Reset the pulse counter so we can start incrementing again  }
    }



  }

  else if (buttonState == LOW && isSpraying == true) {

    isSpraying = false;

    digitalWrite(ledPin, LOW);

    totalMilliLitres += eventTotalMilliLitres;  // Add the millilitres passed in this single spray event to the overall cumulative total

    Serial.print("ETML:  ");
    Serial.print(eventTotalMilliLitres);
    Serial.print("  ");
    Serial.print("TML:  ");
    Serial.println(totalMilliLitres);
    eventTotalMilliLitres = 0;
    return;
  }


}

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

I'd replace interrupt handling by polling. That's easier and safer to implement and debug.

I'm looking for a low volume flow sensor. I am currently using Sea Model: YF-S201 flow sensor, which says it measures 1-30 L/min. The project I'm working on is measuring the output from a solo backpack sprayer, which outputs 1.9 L/min. So it should work, but it doesn't. https://www.gemplers.com/docs/manual/SOLOBACKPK.pdf I was thinking of using a Microstream Model: OFZAT10-AO (https://www.aichitokei.co.jp/eng/products_and_services/micro_stream/zat.html). However it needs a 12v power supply. If you have any advice on other flow sensors or how to make the Microstream work I would appreciate it.

moderator: topics merged (please do not cross post)

Both topics are part of the same project. Information on the other part of the project can be important when answering the question.

Weedpharma

Okay thanks I'm new at all this stuff.

3 to 24 V DC

Ozark_Koala:
Microstream Model: OFZAT10-AO (https://www.aichitokei.co.jp/eng/products_and_services/micro_stream/zat.html). However it needs a 12v power supply. If you have any advice on other flow sensors or how to make the Microstream work I would appreciate it.

data sheet says 3 to 24 V DC

Ozark_Koala:
I was thinking of using a Microstream Model: OFZAT10-AO (https://www.aichitokei.co.jp/eng/products_and_services/micro_stream/zat.html).

However it needs a 12v power supply.

Datasheet (overview tab) states supply can be 3 to 24 V DC.
Datasheet (dimentions tab) gives 12volt as an example. Giving a 1volt/10volt output signal.
This sensor should be able to drive a digital input pin of a 5volt Arduino.
If not, post back for a solution.
Leo..

Thanks guys like I said I'm new to all of this. I must have read it wrong.

Please do not delete posts that make subsequent post nonsensical. IE the one between current #3 & 4.

You asked why they were merged. My post makes no sense without that.

You can edit any post if required. However please note what is an edit if it changes the post materially.

Weedpharma