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)