Need to record data only when the sensor is triggered and output a final value

I have a setup that measures the volume of liquid using a flow meter. I am a beginner so would love some advice.

The volume is constantly being calculated and printed in the serial monitor. The passing fluid is intermittent so I intend on measuring the volume of liquid which has passed in one go.

I need to somehow make the arduino start recording once the sensor has been triggered, stop recording when no more liquid is passing through, and output the total volume to the serial monitor.

Any advice would be great! The code so far is below.

byte statusLed = 13;
byte sensorInterrupt = 0; // 0 = digital pin 2
byte sensorPin = 2;

float calibrationFactor = 8.65;

volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
float totalMilliLitres;
unsigned long oldTime;

void setup()
{

Serial.begin(115200);

pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH);

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

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

// 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)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop()
{

if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host

detachInterrupt(sensorInterrupt);

flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

oldTime = millis();

// 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.

flowMilliLitres = (flowRate / 60) * 1000;

// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;

// Print the cumulative total of litres flowed since starting

Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL");
Serial.print("\t"); // Print tab space

Serial.print("(");
Serial.print(totalMilliLitres/1000);
Serial.print("L");
Serial.println(")");

pulseCount = 0;

attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

delay(1000);
}

}

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

VolumeOutput.ino (2.04 KB)

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

...R

so I intend on measuring the volume of liquid which has passed in one go.

For what definition of "in one go"? When you can answer that, you will have no problems wring the code to implement that definition.

If your sensor is similar to this
YF-S201 Hall Effect Water Flow Meter / Sensor then each output pulse will correspond to a fixed volume of liquid which you are currently able to compute.

You will have to express your output in rate of change dQ/dt and monitor when the rate changes abruptly as it will not be instantaneous. So collect several readings volume and time and store this in an array of fixed size with each array component being a structured data type which includes volume and time. You can use a c struct. You flush out the data periodically from the array and replace with new input each time whilst monitoring after each sampling what the rate is. If there is flow in the line the rate of change of flowrate will fluctuate around 0. You will have to determine the cut off experimentally and then use this in your arduino code. This will signal when your liquid flow has stopped. You can apply the same strategy when water starts to flow in the line.