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++;
}